-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
181 lines (159 loc) · 3.59 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"strconv"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh/forms"
"github.com/charmbracelet/huh/lists"
)
type Todo struct {
task string
isDone bool
}
var todos []Todo
// Model struct representing the state of the UI
type model struct {
choice int
inputMode bool
form forms.Model
list lists.Model
cursor int
}
// Initial model
func initialModel() model {
// Create a new form for adding TODOs
form := forms.New()
form.Input("task", "Enter your task")
// Create an initial empty list
list := lists.New()
return model{
choice: 0,
inputMode: false,
form: form,
list: list,
cursor: 0,
}
}
// Init method for the tea.Model interface
func (m model) Init() tea.Cmd {
// No initial command
return nil
}
// Update method to handle key inputs and update the model state
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
// Quit the program
case "q", "ctrl+c":
return m, tea.Quit
// Navigate the menu with up/down arrow keys
case "up":
if m.cursor > 0 {
m.cursor--
}
case "down":
if m.cursor < 4 {
m.cursor++
}
// Handle menu selection with Enter key
case "enter":
switch m.cursor {
case 0: // Add Todo
m.inputMode = true
case 1: // List Todos
m.updateListView()
case 2: // Mark Todo as Done
m.markTodoAsDone()
case 3: // Delete Todo
m.deleteTodo()
case 4: // Exit
return m, tea.Quit
}
// Handle form input for adding a new TODO task
default:
if m.inputMode {
var doneCmd tea.Cmd
m.form, doneCmd = m.form.Update(msg)
if doneCmd != nil {
m.addTodo()
m.inputMode = false
}
}
}
}
return m, nil
}
// View method renders the UI
func (m model) View() string {
if m.inputMode {
// Render form if in input mode
return m.form.View()
}
// Render menu options
menu := []string{"Add Todo", "List Todos", "Mark Todo as Done", "Delete Todo", "Exit"}
var menuView string
for i, option := range menu {
cursor := " " // No cursor by default
if m.cursor == i {
cursor = ">"
}
menuView += fmt.Sprintf("%s %s\n", cursor, option)
}
if len(todos) > 0 {
// Render TODO list if available
menuView += "\n" + m.list.View()
} else {
menuView += "\nNo todos to show."
}
return menuView
}
// Add a new todo to the list
func (m *model) addTodo() {
task := m.form.Values()["task"]
if task != "" {
todos = append(todos, Todo{task: task, isDone: false})
m.form.Clear()
m.updateListView()
}
}
// Update the list view with todos
func (m *model) updateListView() {
items := make([]lists.Item, len(todos))
for i, todo := range todos {
status := "[ ]"
if todo.isDone {
status = "[x]"
}
items[i] = lists.NewItem(fmt.Sprintf("%d. %s %s", i+1, status, todo.task))
}
m.list.SetItems(items)
}
// Mark a todo as done
func (m *model) markTodoAsDone() {
var input string
fmt.Print("Enter the number of the todo to mark as done: ")
fmt.Scanln(&input)
index, err := strconv.Atoi(input)
if err != nil || index < 1 || index > len(todos) {
fmt.Println("Invalid number.")
return
}
todos[index-1].isDone = true
m.updateListView()
fmt.Println("Todo marked as done!")
}
// Delete a todo
func (m *model) deleteTodo() {
var input string
fmt.Print("Enter the number of the todo to delete: ")
fmt.Scanln(&input)
index, err := strconv.Atoi(input)
if err != nil || index < 1 || index > len(todos) {
fmt.Println("Invalid number.")
return
}
todos = append(todos[:index-1], todos[index:]...)
m.updateListView()
fmt.Println("Todo deleted!")
}