kleinTodo/common/todo.go

109 lines
2.7 KiB
Go
Raw Normal View History

2025-07-26 23:31:00 +02:00
package common
import (
"encoding/json"
"fmt"
2026-01-12 20:48:52 +01:00
"hash/fnv"
2025-08-23 19:14:16 +02:00
"strings"
"time"
2025-07-26 23:31:00 +02:00
)
func (todo Todo) Store(store *BoltStore, user string) error {
if todo.Owner != user {
return fmt.Errorf("unauthorized user")
}
todo.LastModified = time.Now()
2025-07-26 23:31:00 +02:00
todoJson, err := json.Marshal(todo)
if err != nil {
return err
}
return store.SaveValueToBucket(user, todo.Name, string(todoJson))
}
func (todoRequest StoreTodoRequest) Store(store *BoltStore, user string) error {
todo := Todo{
Name: todoRequest.Name,
Description: todoRequest.Description,
Status: todoRequest.Status,
Owner: user,
LastModified: time.Now(),
2025-07-26 23:31:00 +02:00
}
todoJson, err := json.Marshal(todo)
if err != nil {
return err
}
return store.SaveValueToBucket(user, todo.Name, string(todoJson))
}
2025-08-23 19:14:16 +02:00
func (todoList TodoList) FindByName(name string) (Todo, bool) {
for _, todo := range todoList.Todos {
2025-07-26 23:31:00 +02:00
if todo.Name == name {
return todo, true
}
}
return Todo{}, false
}
2025-08-23 19:14:16 +02:00
func (todo Todo) PrintIndexed(index int) {
if todo.Deleted {
return
}
2025-08-23 19:14:16 +02:00
var statusColor string
// Select color based on the status (case-insensitive)
switch strings.ToLower(todo.Status) {
case Done:
statusColor = ColorGreen
case WIP:
statusColor = ColorYellow
2025-08-23 21:51:33 +02:00
case Pending, NotStarted:
2025-08-23 19:14:16 +02:00
statusColor = ColorBlue
case Blocked, Failed:
statusColor = ColorRed
default:
2026-01-12 20:48:52 +01:00
statusColor = ColorWhite // No color for unknown statuses
2025-08-23 19:14:16 +02:00
}
2026-01-12 20:48:52 +01:00
for _, label := range []string{"test", "example", "example-test", "High prio"} {
bg := getLabelColor(label)
fmt.Printf(" %s %s %s", bg, label, ColorReset)
}
fmt.Printf("\n%s%d)%s %s%-20s%s", ColorCyan, index, ColorReset, ColorWhite, todo.Name, ColorReset)
fmt.Printf(" %s %s%s\n", statusColor, strings.ToUpper(todo.Status), ColorReset)
2026-01-12 20:48:52 +01:00
// 5. Description (Indented and slightly dimmer if possible, or standard)
fmt.Printf(" %s\n", todo.Description)
2025-08-23 19:14:16 +02:00
2026-01-12 20:48:52 +01:00
// 6. Footer (Timestamp in Gray to de-emphasize)
lastMod := todo.LastModified.Format("2006-01-02 15:04")
fmt.Printf(" %sLast Modified: %s%s\n", ColorGray, lastMod, ColorReset)
// 7. Separator (Optional, but helps readability)
fmt.Printf(" %s----------------------------%s\n", ColorGray, ColorReset)
}
2026-01-12 20:48:52 +01:00
func getLabelColor(label string) string {
h := fnv.New32a()
h.Write([]byte(label))
idx := h.Sum32() % uint32(len(LabelColors))
return LabelColors[idx]
}
func (t Todo) IsEqual(other Todo) bool {
return t.Name == other.Name &&
t.Description == other.Description &&
t.Status == other.Status &&
t.Owner == other.Owner &&
t.Deleted == other.Deleted
2025-08-23 19:14:16 +02:00
}
2026-01-11 19:04:18 +01:00
func (t Todo) IsEqualIgnoringStatus(other Todo) bool {
return t.Name == other.Name &&
t.Description == other.Description &&
t.Owner == other.Owner &&
t.Deleted == other.Deleted
}