109 lines
2.7 KiB
Go
109 lines
2.7 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (todo Todo) Store(store *BoltStore, user string) error {
|
|
if todo.Owner != user {
|
|
return fmt.Errorf("unauthorized user")
|
|
}
|
|
todo.LastModified = time.Now()
|
|
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(),
|
|
}
|
|
todoJson, err := json.Marshal(todo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return store.SaveValueToBucket(user, todo.Name, string(todoJson))
|
|
}
|
|
|
|
func (todoList TodoList) FindByName(name string) (Todo, bool) {
|
|
for _, todo := range todoList.Todos {
|
|
if todo.Name == name {
|
|
return todo, true
|
|
}
|
|
}
|
|
return Todo{}, false
|
|
}
|
|
|
|
func (todo Todo) PrintIndexed(index int) {
|
|
|
|
if todo.Deleted {
|
|
return
|
|
}
|
|
var statusColor string
|
|
|
|
// Select color based on the status (case-insensitive)
|
|
switch strings.ToLower(todo.Status) {
|
|
case Done:
|
|
statusColor = ColorGreen
|
|
case WIP:
|
|
statusColor = ColorYellow
|
|
case Pending, NotStarted:
|
|
statusColor = ColorBlue
|
|
case Blocked, Failed:
|
|
statusColor = ColorRed
|
|
default:
|
|
statusColor = ColorWhite // No color for unknown statuses
|
|
}
|
|
|
|
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)
|
|
|
|
// 5. Description (Indented and slightly dimmer if possible, or standard)
|
|
fmt.Printf(" %s\n", todo.Description)
|
|
|
|
// 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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (t Todo) IsEqualIgnoringStatus(other Todo) bool {
|
|
return t.Name == other.Name &&
|
|
t.Description == other.Description &&
|
|
t.Owner == other.Owner &&
|
|
t.Deleted == other.Deleted
|
|
}
|