From b85c4c805a8bfd5b486aa7d2a7563e535330081a Mon Sep 17 00:00:00 2001 From: Zakaria Date: Fri, 11 Sep 2026 20:22:03 -0400 Subject: [PATCH] new start | different approach --- go.mod | 10 +++++- go.sum | 10 ++++++ main.go | 10 ++++++ todo.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 todo.go diff --git a/go.mod b/go.mod index 41b32e8..cfde420 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module gitea.zacksolutions.dev/Zakaria/cli_todo -go 1.27.0 +go 1.27.1 + +require ( + github.com/aquasecurity/table v1.11.0 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect + golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8875143 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/aquasecurity/table v1.11.0 h1:SzgCAv7dZcv/gyAyzxorS6OgEk7w/WU5iT2pStIkpl4= +github.com/aquasecurity/table v1.11.0/go.mod h1:eqOmvjjB7AhXFgFqpJUEE/ietg7RrMSJZXyTN8E/wZw= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM= +golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/main.go b/main.go index da29a2c..0c8a96f 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,14 @@ package main func main() { + todos := Todos{} + + todos.add("Learn more Go") + todos.add("You have to understand the code you are writing") + todos.add("You have to understand the code you are writing") + + todos.toggle(0) + + todos.delete(2) + todos.print() } diff --git a/todo.go b/todo.go new file mode 100644 index 0000000..df9f617 --- /dev/null +++ b/todo.go @@ -0,0 +1,94 @@ +package main + +import ( + "errors" + "os" + "strconv" + "time" + + "github.com/aquasecurity/table" +) + +type Todo struct { + Title string + Completed bool + CreatedAt time.Time + CompletedAt *time.Time +} + +type Todos []Todo + +func (todos *Todos) add(title string) { + todo := Todo{ + Title: title, + Completed: false, + CompletedAt: nil, + CreatedAt: time.Now(), + } + + *todos = append(*todos, todo) +} + +func (todos *Todos) validateIndex(index int) error { + if index < 0 || index >= len(*todos) { + err := errors.New("invalid index") + return err + } + + return nil +} + +func (todos *Todos) delete(index int) error { + t := *todos + + if err := t.validateIndex(index); err != nil { + return err + } + *todos = append(t[:index], t[index+1:]...) + return nil +} + +func (todos *Todos) toggle(index int) error { + t := *todos + + if err := t.validateIndex(index); err != nil { + return err + } + + isCompeleted := t[index].Completed + if !isCompeleted { + completionTime := time.Now() + t[index].CompletedAt = &completionTime + } + t[index].Completed = !isCompeleted + return nil +} + +func (todos *Todos) edit(index int, newTitle string) error { + t := *todos + + if err := t.validateIndex(index); err != nil { + return err + } + t[index].Title = newTitle + return nil +} + +func (todos *Todos) print() { + table := table.New(os.Stdout) + table.SetRowLines(false) + table.SetHeaders("Index", "Titile", "Completed", "Created at", "Completed at") + for index, t := range *todos { + completed := "❌" + completedAt := "" + + if t.Completed { + completed = "✅" + if t.CompletedAt != nil { + completedAt = t.CompletedAt.Format(time.RFC1123) + } + } + table.AddRow(strconv.Itoa(index), t.Title, completed, t.CreatedAt.Format(time.RFC1123), completedAt) + } + table.Render() +}