diff --git a/README.md b/README.md index e69de29..6f56d82 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +# TODO app in Go using ncurses +Very simple app diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f8c1a7f --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module todo-app + +go 1.21.3 + +require ( + golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e // indirect + seehuhn.de/go/ncurses v0.2.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7ce8b95 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e h1:XNp2Flc/1eWQGk5BLzqTAN7fQIwIbfyVTuVxXxZh73M= +golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +seehuhn.de/go/ncurses v0.2.0 h1:ZV256n0GIMVEHJnECliGMffzdFsEoT7krJqdfGoYD1E= +seehuhn.de/go/ncurses v0.2.0/go.mod h1:oAc9Y+UN0tflNV0iME++z0ij9uNmIjdxQFkpGoRMd2E= diff --git a/main.go b/main.go new file mode 100644 index 0000000..33b4706 --- /dev/null +++ b/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "seehuhn.de/go/ncurses" +) + +const MAX_TODOS = 3 + +type item struct { + desc string + done bool +} + +func make_x(isDone bool) string { + s := "" + switch isDone { + case true: + s = "x" + case false: + s = " " + } + return s +} + +func main() { + hasQuit := false + currTodo := 0 + + todos := []item{ + { + "Make TODO app", + false, + }, + { + "Make coffee", + true, + }, + { + "Shit the bed", + false, + }, + { + "Make a cup of tea", + false, + }, + } + + // Init window + win := ncurses.Init() + defer ncurses.EndWin() + ncurses.CursSet(ncurses.CursorOff) + + for !hasQuit { + win.Erase() + win.Println("TODO:") + win.Println("----------------------") + for index, item := range todos { + if currTodo == index { + win.AttrOn(ncurses.AttrStandout) + win.Printf("[%s]", make_x(item.done)) + win.Printf(" %s", item.desc) + win.Println("") + win.AttrOff(ncurses.AttrStandout) + } else { + win.AttrOn(ncurses.AttrNormal) + win.Printf("[%s]", make_x(item.done)) + win.Printf(" %s", item.desc) + win.Println("") + win.AttrOff(ncurses.AttrNormal) + } + } + + ch := win.GetCh() + switch ch { + + case ncurses.KeyUp: + if currTodo == 0 { + currTodo = 0 + } else { + currTodo -= 1 + } + + case ncurses.KeyDown: + if currTodo >= len(todos)-1 { + currTodo = len(todos) - 1 + } else { + currTodo += 1 + } + + case ' ': + todos[currTodo].done = !todos[currTodo].done + + case 'q': + hasQuit = !hasQuit + } + } + + win.Refresh() +} diff --git a/todo-app b/todo-app new file mode 100755 index 0000000..1367c91 Binary files /dev/null and b/todo-app differ