new start | different approach
This commit is contained in:
@@ -1,3 +1,11 @@
|
|||||||
module gitea.zacksolutions.dev/Zakaria/cli_todo
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -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=
|
||||||
@@ -1,4 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func 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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user