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

     1  package checklist
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Checklist is a module for creating generic checklist implementations
     8  // See 'Todo' for an implementation example
     9  type Checklist struct {
    10  	Items []*ChecklistItem
    11  
    12  	checkedIcon   string
    13  	selected      int
    14  	uncheckedIcon string
    15  }
    16  
    17  func NewChecklist(checkedIcon, uncheckedIcon string) Checklist {
    18  	list := Checklist{
    19  		checkedIcon:   checkedIcon,
    20  		selected:      -1,
    21  		uncheckedIcon: uncheckedIcon,
    22  	}
    23  
    24  	return list
    25  }
    26  
    27  /* -------------------- Exported Functions -------------------- */
    28  
    29  // Add creates a new checklist item and adds it to the list
    30  // The new one is at the start or end of the list, based on newPos
    31  func (list *Checklist) Add(checked bool, date *time.Time, tags []string, text string, newPos ...string) {
    32  	item := NewChecklistItem(
    33  		checked,
    34  		date,
    35  		tags,
    36  		text,
    37  		list.checkedIcon,
    38  		list.uncheckedIcon,
    39  	)
    40  
    41  	if len(newPos) == 0 || newPos[0] == "first" {
    42  		list.Items = append([]*ChecklistItem{item}, list.Items...)
    43  	} else if newPos[0] == "last" {
    44  		list.Items = append(list.Items, []*ChecklistItem{item}...)
    45  	}
    46  }
    47  
    48  // CheckedItems returns a slice of all the checked items
    49  func (list *Checklist) CheckedItems() []*ChecklistItem {
    50  	items := []*ChecklistItem{}
    51  
    52  	for _, item := range list.Items {
    53  		if item.Checked {
    54  			items = append(items, item)
    55  		}
    56  	}
    57  
    58  	return items
    59  }
    60  
    61  // Delete removes the selected item from the checklist
    62  func (list *Checklist) Delete(selectedIndex int) {
    63  	if selectedIndex >= 0 && selectedIndex < len(list.Items) {
    64  		list.Items = append(list.Items[:selectedIndex], list.Items[selectedIndex+1:]...)
    65  	}
    66  }
    67  
    68  // IsSelectable returns true if the checklist has selectable items, false if it does not
    69  func (list *Checklist) IsSelectable() bool {
    70  	return list.selected >= 0 && list.selected < len(list.Items)
    71  }
    72  
    73  // IsUnselectable returns true if the checklist has no selectable items, false if it does
    74  func (list *Checklist) IsUnselectable() bool {
    75  	return !list.IsSelectable()
    76  }
    77  
    78  // LongestLine returns the length of the longest checklist item's text
    79  func (list *Checklist) LongestLine() int {
    80  	maxLen := 0
    81  
    82  	for _, item := range list.Items {
    83  		if len(item.Text) > maxLen {
    84  			maxLen = len(item.Text)
    85  		}
    86  	}
    87  
    88  	return maxLen
    89  }
    90  
    91  // IndexByItem returns the index of a giving item if found, otherwise returns 0 with ok set to false
    92  func (list *Checklist) IndexByItem(selectableItem *ChecklistItem) (index int, ok bool) {
    93  	for idx, item := range list.Items {
    94  		if item == selectableItem {
    95  			return idx, true
    96  		}
    97  	}
    98  	return 0, false
    99  }
   100  
   101  // UncheckedItems returns a slice of all the unchecked items
   102  func (list *Checklist) UncheckedItems() []*ChecklistItem {
   103  	items := []*ChecklistItem{}
   104  
   105  	for _, item := range list.Items {
   106  		if !item.Checked {
   107  			items = append(items, item)
   108  		}
   109  	}
   110  
   111  	return items
   112  }
   113  
   114  // Unselect removes the current select such that no item is selected
   115  func (list *Checklist) Unselect() {
   116  	list.selected = -1
   117  }
   118  
   119  /* -------------------- Sort Interface -------------------- */
   120  
   121  func (list *Checklist) Len() int {
   122  	return len(list.Items)
   123  }
   124  
   125  func (list *Checklist) Less(i, j int) bool {
   126  	return list.Items[i].Text < list.Items[j].Text
   127  }
   128  
   129  func (list *Checklist) Swap(i, j int) {
   130  	list.Items[i], list.Items[j] = list.Items[j], list.Items[i]
   131  }