1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

chore: adjust ToDo to use ScrollableWidget

This commit is contained in:
Omar 2019-10-20 13:02:14 +02:00
parent 9a1b350f4e
commit e7a42e05b5
No known key found for this signature in database
GPG Key ID: 6930C4457A3943A4
4 changed files with 94 additions and 120 deletions

View File

@ -48,9 +48,8 @@ func (list *Checklist) CheckedItems() []*ChecklistItem {
} }
// Delete removes the selected item from the checklist // Delete removes the selected item from the checklist
func (list *Checklist) Delete() { func (list *Checklist) Delete(selectedIndex int) {
list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...) list.Items = append(list.Items[:selectedIndex], list.Items[selectedIndex+1:]...)
list.Prev()
} }
// IsSelectable returns true if the checklist has selectable items, false if it does not // IsSelectable returns true if the checklist has selectable items, false if it does not
@ -76,35 +75,14 @@ func (list *Checklist) LongestLine() int {
return maxLen return maxLen
} }
func (list *Checklist) Selected() int { // IndexByItem returns the index of a giving item if found ,otherwise returns 0 with ok set to false
return list.selected func (list *Checklist) IndexByItem(selectableItem *ChecklistItem) (index int, ok bool) {
}
// SelectedItem returns the currently-selected checklist item or nil if no item is selected
func (list *Checklist) SelectedItem() *ChecklistItem {
if list.IsUnselectable() {
return nil
}
return list.Items[list.selected]
}
func (list *Checklist) SetSelectedByItem(selectableItem *ChecklistItem) {
for idx, item := range list.Items { for idx, item := range list.Items {
if item == selectableItem { if item == selectableItem {
list.selected = idx return idx, true
break
} }
} }
} return 0, false
// Toggle switches the checked state of the currently-selected item
func (list *Checklist) Toggle() {
if list.IsUnselectable() {
return
}
list.SelectedItem().Toggle()
} }
// UncheckedItems returns a slice of all the unchecked items // UncheckedItems returns a slice of all the unchecked items
@ -125,65 +103,6 @@ func (list *Checklist) Unselect() {
list.selected = -1 list.selected = -1
} }
// Update sets the text of the currently-selected item to the provided text
func (list *Checklist) Update(text string) {
item := list.SelectedItem()
if item == nil {
return
}
item.Text = text
}
/* -------------------- Item Movement -------------------- */
// Prev selects the previous item UP in the checklist
func (list *Checklist) Prev() {
list.selected--
if list.selected < 0 {
list.selected = len(list.Items) - 1
}
}
// Next selects the next item DOWN in the checklist
func (list *Checklist) Next() {
list.selected++
if list.selected >= len(list.Items) {
list.selected = 0
}
}
// Promote moves the selected item UP in the checklist
func (list *Checklist) Promote() {
if list.IsUnselectable() {
return
}
k := list.selected - 1
if k < 0 {
k = len(list.Items) - 1
}
list.Swap(list.selected, k)
list.selected = k
}
// Demote moves the selected item DOWN in the checklist
func (list *Checklist) Demote() {
if list.IsUnselectable() {
return
}
j := list.selected + 1
if j >= len(list.Items) {
j = 0
}
list.Swap(list.selected, j)
list.selected = j
}
/* -------------------- Sort Interface -------------------- */ /* -------------------- Sort Interface -------------------- */
func (list *Checklist) Len() int { func (list *Checklist) Len() int {

View File

@ -20,19 +20,22 @@ func (widget *Widget) content() (string, string, bool) {
) )
offset := 0 offset := 0
selectedItem := widget.SelectedItem()
for idx, item := range widget.list.UncheckedItems() { for idx, item := range widget.list.UncheckedItems() {
str += widget.formattedItemLine(idx, item, widget.list.SelectedItem(), widget.list.LongestLine()) str += widget.formattedItemLine(idx, item, selectedItem, widget.list.LongestLine())
newList.Items = append(newList.Items, item) newList.Items = append(newList.Items, item)
offset++ offset++
} }
for idx, item := range widget.list.CheckedItems() { for idx, item := range widget.list.CheckedItems() {
str += widget.formattedItemLine(idx+offset, item, widget.list.SelectedItem(), widget.list.LongestLine()) str += widget.formattedItemLine(idx+offset, item, selectedItem, widget.list.LongestLine())
newList.Items = append(newList.Items, item) newList.Items = append(newList.Items, item)
} }
newList.SetSelectedByItem(widget.list.SelectedItem()) if idx, ok := newList.IndexByItem(selectedItem); ok {
widget.Selected = idx
}
widget.SetList(newList) widget.SetList(newList)
return widget.CommonSettings().Title, str, false return widget.CommonSettings().Title, str, false

View File

@ -11,62 +11,85 @@ import (
func (widget *Widget) initializeKeyboardControls() { func (widget *Widget) initializeKeyboardControls() {
widget.InitializeCommonControls(widget.Refresh) widget.InitializeCommonControls(widget.Refresh)
widget.SetKeyboardChar("j", widget.displayNext, "Select next item") widget.SetKeyboardChar("j", widget.Next, "Select next item")
widget.SetKeyboardChar("k", widget.displayPrev, "Select previous item") widget.SetKeyboardChar("k", widget.Prev, "Select previous item")
widget.SetKeyboardChar(" ", widget.toggleChecked, "Toggle checkmark") widget.SetKeyboardChar(" ", widget.toggleChecked, "Toggle checkmark")
widget.SetKeyboardChar("n", widget.newItem, "Create new item") widget.SetKeyboardChar("n", widget.newItem, "Create new item")
widget.SetKeyboardChar("o", widget.openFile, "Open file") widget.SetKeyboardChar("o", widget.openFile, "Open file")
widget.SetKeyboardKey(tcell.KeyDown, widget.displayNext, "Select next item") widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
widget.SetKeyboardKey(tcell.KeyUp, widget.displayPrev, "Select previous item") widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect, "Clear selection") widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect, "Clear selection")
widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelected, "Delete item") widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelected, "Delete item")
widget.SetKeyboardKey(tcell.KeyCtrlJ, widget.demoteSelected, "Demote item") widget.SetKeyboardKey(tcell.KeyCtrlJ, widget.demoteSelected, "Demote item")
widget.SetKeyboardKey(tcell.KeyCtrlK, widget.promoteSelected, "Promote item") widget.SetKeyboardKey(tcell.KeyCtrlK, widget.promoteSelected, "Promote item")
widget.SetKeyboardKey(tcell.KeyEnter, widget.editSelected, "Edit item") widget.SetKeyboardKey(tcell.KeyEnter, widget.updateSelected, "Edit item")
} }
func (widget *Widget) deleteSelected() { func (widget *Widget) deleteSelected() {
widget.list.Delete()
if !widget.isItemSelected() {
return
}
widget.list.Delete(widget.Selected)
widget.ScrollableWidget.SetItemCount(len(widget.list.Items))
widget.Prev()
widget.persist() widget.persist()
widget.display() widget.display()
} }
func (widget *Widget) demoteSelected() { func (widget *Widget) demoteSelected() {
widget.list.Demote() if !widget.isItemSelected() {
return
}
j := widget.Selected + 1
if j >= len(widget.list.Items) {
j = 0
}
widget.list.Swap(widget.Selected, j)
widget.Selected = j
widget.persist() widget.persist()
widget.display() widget.display()
} }
func (widget *Widget) displayNext() {
widget.list.Next()
widget.display()
}
func (widget *Widget) displayPrev() {
widget.list.Prev()
widget.display()
}
func (widget *Widget) openFile() { func (widget *Widget) openFile() {
confDir, _ := cfg.WtfConfigDir() confDir, _ := cfg.WtfConfigDir()
utils.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath)) utils.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath))
} }
func (widget *Widget) promoteSelected() { func (widget *Widget) promoteSelected() {
widget.list.Promote() if !widget.isItemSelected() {
return
}
k := widget.Selected - 1
if k < 0 {
k = len(widget.list.Items) - 1
}
widget.list.Swap(widget.Selected, k)
widget.Selected = k
widget.persist() widget.persist()
widget.display() widget.display()
} }
func (widget *Widget) toggleChecked() { func (widget *Widget) toggleChecked() {
widget.list.Toggle() selectedItem := widget.SelectedItem()
if selectedItem == nil {
return
}
selectedItem.Toggle()
widget.persist() widget.persist()
widget.display() widget.display()
} }
func (widget *Widget) unselect() { func (widget *Widget) unselect() {
widget.list.Unselect() widget.Selected = -1
widget.display() widget.display()
} }

View File

@ -23,7 +23,7 @@ const (
// A Widget represents a Todo widget // A Widget represents a Todo widget
type Widget struct { type Widget struct {
view.KeyboardWidget view.KeyboardWidget
view.TextWidget view.ScrollableWidget
app *tview.Application app *tview.Application
settings *Settings settings *Settings
@ -35,8 +35,8 @@ type Widget struct {
// NewWidget creates a new instance of a widget // NewWidget creates a new instance of a widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common), KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
TextWidget: view.NewTextWidget(app, settings.common), ScrollableWidget: view.NewScrollableWidget(app, settings.common),
app: app, app: app,
settings: settings, settings: settings,
@ -54,6 +54,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
widget.View.SetScrollable(true) widget.View.SetScrollable(true)
widget.KeyboardWidget.SetView(widget.View) widget.KeyboardWidget.SetView(widget.View)
widget.SetRenderFunction(widget.display)
return &widget return &widget
} }
@ -75,18 +76,44 @@ func (widget *Widget) HelpText() string {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
// edit opens a modal dialog that permits editing the text of the currently-selected item // isItemSelected returns weather any item of the todo is selected or not
func (widget *Widget) editSelected() { func (widget *Widget) isItemSelected() bool {
if widget.list.SelectedItem() == nil {
return widget.Selected >= 0 && widget.Selected < len(widget.list.Items)
}
// SelectedItem returns the currently-selected checklist item or nil if no item is selected
func (widget *Widget) SelectedItem() *checklist.ChecklistItem {
var selectedItem *checklist.ChecklistItem
if widget.isItemSelected() {
selectedItem = widget.list.Items[widget.Selected]
}
return selectedItem
}
// updateSelectedItem update the text of the selected item.
func (widget *Widget) updateSelectedItem(text string) {
selectedItem := widget.SelectedItem()
if selectedItem == nil {
return return
} }
form := widget.modalForm("Edit:", widget.list.SelectedItem().Text) selectedItem.Text = text
}
// updateSelected sets the text of the currently-selected item to the provided text
func (widget *Widget) updateSelected() {
if !widget.isItemSelected() {
return
}
form := widget.modalForm("Edit:", widget.SelectedItem().Text)
saveFctn := func() { saveFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText() text := form.GetFormItem(0).(*tview.InputField).GetText()
widget.list.Update(text) widget.updateSelectedItem(text)
widget.persist() widget.persist()
widget.pages.RemovePage("modal") widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View) widget.app.SetFocus(widget.View)
@ -104,7 +131,7 @@ func (widget *Widget) init() {
} }
} }
// Loads the todo list from Yaml file // Loads the todo list from3 Yaml file
func (widget *Widget) load() { func (widget *Widget) load() {
confDir, _ := cfg.WtfConfigDir() confDir, _ := cfg.WtfConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
@ -113,6 +140,7 @@ func (widget *Widget) load() {
yaml.Unmarshal(fileData, &widget.list) yaml.Unmarshal(fileData, &widget.list)
widget.ScrollableWidget.SetItemCount(len(widget.list.Items))
widget.setItemChecks() widget.setItemChecks()
} }
@ -123,6 +151,7 @@ func (widget *Widget) newItem() {
text := form.GetFormItem(0).(*tview.InputField).GetText() text := form.GetFormItem(0).(*tview.InputField).GetText()
widget.list.Add(false, text) widget.list.Add(false, text)
widget.SetItemCount(len(widget.list.Items))
widget.persist() widget.persist()
widget.pages.RemovePage("modal") widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View) widget.app.SetFocus(widget.View)