-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (73 loc) · 1.67 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const FILEPATH = "ToDoFile"
func main() {
todos, err := readFile()
if err != nil {
fmt.Println(err)
}
app := tview.NewApplication()
listView := tview.NewTextView()
listView.SetTitle("ToDo").SetBorder(true)
listView.SetText(todos)
textBox := tview.NewInputField()
textBox.SetLabel("ToDo: ").SetTitle("New ToDo").SetBorder(true)
textBox.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter:
todoText := textBox.GetText()
if todoText == "" {
return nil
}
if err := writeFile(todoText); err != nil {
fmt.Println(err)
}
listView.SetText(listView.GetText(true) + todoText + "\n")
textBox.SetText("")
return nil
}
return event
})
flex := tview.NewFlex()
flex.SetDirection(tview.FlexRow).
AddItem(textBox, 4, 0, true).
AddItem(listView, 0, 1, false)
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil {
panic(err)
}
}
// readFile reads ToDo list from the file.
func readFile() (string, error) {
if _, err := os.Stat(FILEPATH); os.IsNotExist(err) {
return "", nil
}
f, err := os.OpenFile(FILEPATH, os.O_RDONLY, 0666)
if err != nil {
return "", err
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
return "", err
}
todos := string(buf)
return todos, nil
}
// writeFile writes ToDo to the file.
func writeFile(todo string) error {
f, err := os.OpenFile(FILEPATH, (os.O_RDWR | os.O_CREATE | os.O_APPEND), 0666)
if err != nil {
return err
}
defer f.Close()
if _, err := fmt.Fprintf(f, todo+"\n"); err != nil {
return err
}
return nil
}