new start | different approach

This commit is contained in:
2026-09-11 20:22:03 -04:00
parent f36de5de5a
commit b85c4c805a
4 changed files with 123 additions and 1 deletions
+9 -1
View File
@@ -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
)
+10
View File
@@ -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=
+10
View File
@@ -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()
}
+94
View File
@@ -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()
}