all repos — todo.txt-go @ 79fc5d605316916243adc3f0e252a2ec5a1a314e

✨ Save to file on task update
Tim Izzo tim@5ika.ch
Wed, 02 Apr 2025 09:15:46 +0200
commit

79fc5d605316916243adc3f0e252a2ec5a1a314e

parent

7def563420741ab6ef1577c7e3b04807f980eeb2

4 files changed, 20 insertions(+), 15 deletions(-)

jump to
M README.mdREADME.md

@@ -2,8 +2,11 @@ # TODO.txt in Go

## TODO -- [x] Use *todo.txt* file targeted by todo-txt.sh config +- [x] Use _todo.txt_ file targeted by todo-txt.sh config - [x] Implement archive mecanism +- [ ] Allow task edition +- [x] Allow to remove priority on task +- [ ] Circular navigation on tasks ## Ressources
M config.goconfig.go

@@ -38,4 +38,4 @@ todoDir: todoDir,

taskFilePath: taskFilePath, doneFilePath: doneFilePath, } -} +}
M keymap.gokeymap.go

@@ -8,7 +8,6 @@ Down key.Binding

Priority key.Binding ClearPriority key.Binding Help key.Binding - SaveQuit key.Binding Quit key.Binding Check key.Binding Clean key.Binding

@@ -20,7 +19,7 @@

// ShortHelp returns keybindings to be shown in the mini help view. It's part // of the key.Map interface. func (k keyMap) ShortHelp() []key.Binding { - return []key.Binding{k.Check, k.Add, k.SortByPriority, k.SaveQuit, k.Help} + return []key.Binding{k.Check, k.Add, k.SortByPriority, k.Help} } // FullHelp returns keybindings for the expanded help view. It's part of the

@@ -29,7 +28,7 @@ func (k keyMap) FullHelp() [][]key.Binding {

return [][]key.Binding{ {k.Check, k.Priority, k.Up}, {k.Add, k.ClearPriority, k.Clean, k.Down}, - {k.SortByPriority, k.SortByDate, k.SaveQuit, k.Quit}, + {k.SortByPriority, k.SortByDate, k.Quit}, } }

@@ -74,12 +73,8 @@ Clean: key.NewBinding(

key.WithKeys("u"), key.WithHelp("u", "Clean completed tasks"), ), - SaveQuit: key.NewBinding( - key.WithKeys("q", "esc"), - key.WithHelp("q", "Save & quit"), - ), Quit: key.NewBinding( - key.WithKeys("ctrl+c"), - key.WithHelp("ctrl+c", "Cancel & quit"), + key.WithKeys("ctrl+c", "q"), + key.WithHelp("q", "Quit"), ), }
M main.gomain.go

@@ -80,6 +80,10 @@ func (m model) Init() tea.Cmd {

return textinput.Blink } +func (m model) SaveTasks() { + m.tasks.WriteToPath(m.config.taskFilePath) +} + func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd

@@ -112,9 +116,11 @@

// Tasks management case key.Matches(msg, m.keys.Priority): m.tasks[m.cursor].Priority = strings.ToUpper(msg.String()) + m.SaveTasks() case key.Matches(msg, m.keys.ClearPriority): m.tasks[m.cursor].Priority = "" + m.SaveTasks() case key.Matches(msg, m.keys.Check): _, ok := m.selected[m.cursor]

@@ -126,6 +132,8 @@ } else {

m.selected[m.cursor] = struct{}{} m.tasks[m.cursor].Complete() } + + m.SaveTasks() case key.Matches(msg, m.keys.SortByPriority): m.tasks.Sort(todo.SortCompletedDateAsc, todo.SortPriorityAsc)

@@ -156,6 +164,7 @@ defer doneFile.Close()

if writeErr := doneTasks.WriteToFile(doneFile); writeErr != nil { log.Fatal(writeErr) } + m.SaveTasks() m.tasks = m.tasks.Filter(todo.FilterNotCompleted) m.selected = make(map[int]struct{}) for i, t := range m.tasks {

@@ -168,11 +177,8 @@ // Interface

case key.Matches(msg, m.keys.Add): m.interfaceState = Add - case key.Matches(msg, m.keys.SaveQuit): - m.tasks.WriteToPath(m.config.taskFilePath) - return m, tea.Quit - case key.Matches(msg, m.keys.Quit): + m.SaveTasks() return m, tea.Quit case key.Matches(msg, m.keys.Help):

@@ -191,6 +197,7 @@ inputValue := m.textInput.Value()

if inputValue != "" { newTask, _ := todo.ParseTask(inputValue) m.tasks.AddTask(newTask) + m.SaveTasks() } m.textInput.Reset() m.interfaceState = List