github.com/wtfutil/wtf@v0.43.0/checklist/checklist_item.go (about)

     1  package checklist
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // ChecklistItem is a module for creating generic checklist implementations
     9  // See 'Todo' for an implementation example
    10  type ChecklistItem struct {
    11  	Checked       bool
    12  	CheckedIcon   string
    13  	Date          *time.Time
    14  	Tags          []string
    15  	Text          string
    16  	UncheckedIcon string
    17  }
    18  
    19  func NewChecklistItem(checked bool, date *time.Time, tags []string, text string, checkedIcon, uncheckedIcon string) *ChecklistItem {
    20  	item := &ChecklistItem{
    21  		Checked:       checked,
    22  		CheckedIcon:   checkedIcon,
    23  		Date:          date,
    24  		Tags:          tags,
    25  		Text:          text,
    26  		UncheckedIcon: uncheckedIcon,
    27  	}
    28  
    29  	return item
    30  }
    31  
    32  // CheckMark returns the string used to indicate a ChecklistItem is checked or unchecked
    33  func (item *ChecklistItem) CheckMark() string {
    34  	item.ensureItemIcons()
    35  
    36  	if item.Checked {
    37  		return item.CheckedIcon
    38  	}
    39  
    40  	return item.UncheckedIcon
    41  }
    42  
    43  // EditText returns the content of the edit todo form, so includes formatted date and tags
    44  func (item *ChecklistItem) EditText() string {
    45  	datePrefix := ""
    46  	if item.Date != nil {
    47  		datePrefix = fmt.Sprintf("%d-%02d-%02d", item.Date.Year(), item.Date.Month(), item.Date.Day()) + " "
    48  	}
    49  
    50  	tagsPrefix := item.TagString()
    51  
    52  	return datePrefix + tagsPrefix + item.Text
    53  }
    54  
    55  func (item *ChecklistItem) TagString() string {
    56  	if len(item.Tags) == 0 {
    57  		return ""
    58  	}
    59  
    60  	s := ""
    61  	for _, tag := range item.Tags {
    62  		s += "#" + tag + " "
    63  	}
    64  
    65  	return s
    66  }
    67  
    68  // Toggle changes the checked state of the ChecklistItem
    69  // If checked, it is unchecked. If unchecked, it is checked
    70  func (item *ChecklistItem) Toggle() {
    71  	item.Checked = !item.Checked
    72  }
    73  
    74  /* -------------------- Unexported Functions -------------------- */
    75  
    76  func (item *ChecklistItem) ensureItemIcons() {
    77  	if item.CheckedIcon == "" {
    78  		item.CheckedIcon = "x"
    79  	}
    80  
    81  	if item.UncheckedIcon == "" {
    82  		item.UncheckedIcon = " "
    83  	}
    84  }