refactor: replace explicit fields with tag-based property system

This commit is contained in:
2026-03-27 02:11:46 +01:00
parent 2d4cff717b
commit b2225cff7b
17 changed files with 485 additions and 825 deletions

View File

@@ -12,160 +12,106 @@ import (
"github.com/fatih/color"
)
type RenderMap map[string]struct {
s string
l string
c *color.Color
}
var (
idColor = color.New(color.FgYellow)
titleColor = color.New(color.FgWhite, color.Bold)
statusOpen = color.New(color.FgYellow)
statusDone = color.New(color.FgHiBlack)
prioHigh = color.New(color.FgHiRed)
prioMedium = color.New(color.FgYellow)
prioLow = color.New(color.FgYellow, color.Faint)
tagColor = color.New(color.FgCyan)
nsColor = color.New(color.FgYellow)
dimColor = color.New(color.FgHiBlack)
contentColor = color.New(color.FgWhite)
labelColor = color.New(color.FgYellow)
typeIssue = color.New(color.FgMagenta)
typeNote = color.New(color.FgHiBlue)
typeUser = color.New(color.FgHiGreen)
typeNs = color.New(color.FgHiYellow)
cPrimary = color.New(color.FgCyan)
cSecond = color.New(color.FgMagenta)
cDim = color.New(color.FgHiBlack)
cText = color.New(color.FgWhite)
cTitle = color.New(color.FgWhite, color.Bold)
cGood = color.New(color.FgGreen)
cWarn = color.New(color.FgYellow)
cBad = color.New(color.FgRed)
typeRM = RenderMap{
"issue": {" ", "\uf188 issue", cSecond},
"note": {"\uf15c", "\uf15c note", cPrimary},
"user": {"\uf007", "\uf007 user", cGood},
"namespace": {"\uf07b", "\uf07b namespace", cWarn},
"": {" ", "n/a", cDim},
}
statusRM = RenderMap{
"open": {"●", "● open", cPrimary},
"done": {"○", "○ done", cDim},
"": {"—", "n/a", cDim},
}
prioRM = RenderMap{
"high": {"\uf0e7", "high", cBad},
"medium": {"\uf0e7", "medium", cWarn},
"low": {" ", "low", cDim},
"": {" ", "n/a", cDim},
}
relIcons = map[string]string{"blocks": "\uf068", "subtask": "\uf0da", "related": "\uf0c1", "assignee": "\uf007"}
prioRanks = map[string]int{"high": 3, "medium": 2, "low": 1}
statusRanks = map[string]int{"open": 2, "": 1, "done": 0}
)
type iconSet struct {
Issue, Note, User, Namespace string
Blocks, Subtask, Related string
Assignee, Created string
Tag, Calendar string
Check, Cross string
const (
iconCalendar = "\uf133"
iconCheck = "\uf00c"
iconCross = "\uf00d"
iconNamespace = "\uf07b"
)
func render(rm RenderMap, key string, short bool) string {
v, ok := rm[key]
if !ok {
v, ok = rm[""]
if !ok {
return ""
}
}
if short {
return v.c.Sprint(v.s)
}
return v.c.Sprint(v.l)
}
var icons = iconSet{
Issue: "\uf188", // bug icon
Note: "\uf15c", // file-text
User: "\uf007", // user
Namespace: "\uf07b", // folder
Blocks: "\uf068", // minus
Subtask: "\uf0da", // caret-right
Related: "\uf0c1", // link
Assignee: "\uf007", // user
Created: "\uf007", // user
Tag: "\uf02b", // tag
Calendar: "\uf133", // calendar
Check: "\uf00c", // check
Cross: "\uf00d", // times
}
func typeIcon(t string) string {
switch t {
case "issue":
return icons.Issue
case "note":
return icons.Note
case "user":
return icons.User
case "namespace":
return icons.Namespace
default:
return icons.Issue
}
}
func typeIconColored(t string) string {
switch t {
case "issue":
return typeIssue.Sprint(icons.Issue)
case "note":
return typeNote.Sprint(icons.Note)
case "user":
return typeUser.Sprint(icons.User)
case "namespace":
return typeNs.Sprint(icons.Namespace)
default:
return typeIssue.Sprint(icons.Issue)
}
}
func statusIcon(s string) string {
if s == "done" {
return icons.Check
}
return icons.Cross
}
func statusIconColored(s string) string {
if s == "" {
return " "
}
if s == "done" {
return dimColor.Sprint("○")
}
return dimColor.Sprint("●")
}
func statusColored(s string) string {
if s == "" {
return dimColor.Sprint("—")
}
if s == "done" {
return statusDone.Sprint("○ done")
}
return statusOpen.Sprint("● open")
}
func prioColored(p string) string {
switch p {
case "high":
return prioHigh.Sprint("● high")
case "medium":
return prioMedium.Sprint("◆ medium")
case "low":
return prioLow.Sprint("○ low")
default:
return dimColor.Sprint("—")
func getDisplayTags(n *models.Node) []string {
var tags []string
for _, t := range n.Tags {
if !strings.HasPrefix(t, "_") {
tags = append(tags, t)
}
}
return tags
}
func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
if jsonOut {
return json.NewEncoder(w).Encode(nodes)
}
fmt.Fprintln(w)
if len(nodes) == 0 {
fmt.Fprintln(w, dimColor.Sprint(" No results."))
fmt.Fprintln(w, cDim.Sprint("No results."))
return nil
}
fmt.Fprintln(w)
sort.Slice(nodes, func(i, j int) bool {
si, sj := nodes[i].GetProperty("status"), nodes[j].GetProperty("status")
if si != sj {
return statusRank(si) > statusRank(sj)
return statusRanks[si] > statusRanks[sj]
}
pi, pj := nodes[i].GetProperty("prio"), nodes[j].GetProperty("prio")
return prioRank(pi) > prioRank(pj)
return prioRanks[nodes[i].GetProperty("prio")] > prioRanks[nodes[j].GetProperty("prio")]
})
for _, n := range nodes {
tags := getDisplayTags(n)
nodeType := n.GetType()
var typeAndStatus string
if nodeType == "issue" {
typeAndStatus = statusIconColored(n.GetProperty("status"))
} else {
typeAndStatus = typeIconColored(nodeType) + " " + statusIconColored(n.GetProperty("status"))
}
fmt.Fprintf(w, " %s %s %s %s %s",
idColor.Sprintf("%s", n.ID),
prioColoredShort(n.GetProperty("prio")),
typeAndStatus,
titleColor.Sprint(truncate(n.Title, 35)),
dimColor.Sprint("["+n.GetProperty("namespace")+"]"),
fmt.Fprintf(w, " %s %s %s %s %s %s",
cDim.Sprint(n.ID),
render(prioRM, n.GetProperty("prio"), true),
render(statusRM, n.GetProperty("status"), true),
render(typeRM, n.GetProperty("type"), true),
cTitle.Sprint(truncate(n.Title, 35)),
cDim.Sprint("["+n.GetProperty("namespace")+"]"),
)
if len(tags) > 0 {
var hashTags []string
for _, t := range tags {
hashTags = append(hashTags, "#"+t)
}
fmt.Fprintf(w, " %s", tagColor.Sprint(strings.Join(hashTags, " ")))
fmt.Fprintf(w, " %s", cPrimary.Sprint("#"+strings.Join(tags, " #")))
}
fmt.Fprintln(w)
}
@@ -173,121 +119,53 @@ func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
return nil
}
func prioColoredShort(p string) string {
switch p {
case "high":
return prioHigh.Sprint("\uf0e7 ")
case "medium":
return prioMedium.Sprint("\uf12a ")
case "low":
return prioLow.Sprint("\uf068 ")
default:
return " "
}
}
func prioRank(p string) int {
switch p {
case "high":
return 3
case "medium":
return 2
case "low":
return 1
default:
return 0
}
}
func statusRank(s string) int {
switch s {
case "open":
return 2
case "done":
return 0
default:
return 1
}
}
func getDisplayTags(n *models.Node) []string {
var tags []string
for _, t := range n.Tags {
if _, _, ok := models.ParseTag(t); !ok {
tags = append(tags, t)
}
}
return tags
}
func PrintNode(w io.Writer, n *models.Node, jsonOut bool) error {
if jsonOut {
return json.NewEncoder(w).Encode(n)
}
icon := typeIcon(n.GetType())
nodeType := strings.Title(n.GetType())
fmt.Fprintln(w)
fmt.Fprintf(w, " %s %s %s %s\n",
icon,
idColor.Sprint(n.ID),
titleColor.Sprint(n.Title),
dimColor.Sprint("["+nodeType+"]"),
)
fmt.Fprintln(w, dimColor.Sprint(" ───────────────────────────"))
fmt.Fprintf(w, " Status: %s\n", statusColored(n.GetProperty("status")))
fmt.Fprintf(w, " Priority: %s\n", prioColored(n.GetProperty("prio")))
fmt.Fprintf(w, " Namespace: %s\n", nsColor.Sprint(n.GetProperty("namespace")))
fmt.Fprintf(w, " %s %s %s\n", render(typeRM, n.GetProperty("type"), false), cDim.Sprint(n.ID), cTitle.Sprint(n.Title))
fmt.Fprintln(w, cDim.Sprint(" ───────────────────────────────"))
fmt.Fprintf(w, " Status: %s\n", render(statusRM, n.GetProperty("status"), false))
fmt.Fprintf(w, " Priority: %s\n", render(prioRM, n.GetProperty("prio"), false))
fmt.Fprintf(w, " Namespace: %s\n", cWarn.Sprint(n.GetProperty("namespace")))
if n.DueDate != "" {
fmt.Fprintf(w, " Due: %s %s\n", icons.Calendar, n.DueDate)
fmt.Fprintf(w, " Due: %s %s\n", iconCalendar, n.DueDate)
}
fmt.Fprintf(w, " Created: %s\n", dimColor.Sprint(n.CreatedAt))
fmt.Fprintf(w, " Created: %s\n", cDim.Sprint(n.CreatedAt))
fmt.Fprintf(w, " Updated: %s\n", cDim.Sprint(n.UpdatedAt))
if n.Content != "" {
fmt.Fprintln(w)
fmt.Fprintln(w, labelColor.Sprint(" Content:"))
for _, line := range strings.Split(n.Content, "\n") {
fmt.Fprintf(w, dimColor.Sprint(" │ ")+contentColor.Sprint("%s\n"), line)
fmt.Fprintln(w, "\n"+cPrimary.Sprint(" Content:"))
for i, line := range strings.Split(n.Content, "\n") {
if i > 5 {
fmt.Fprintf(w, "%s ...\n", cDim.Sprint(" │ "))
break
}
fmt.Fprintf(w, "%s%s\n", cDim.Sprint(" │ "), cText.Sprint(line))
}
}
if len(n.Tags) > 0 {
var tags []string
for _, t := range n.Tags {
if _, _, ok := models.ParseTag(t); !ok {
tags = append(tags, t)
}
}
if len(tags) > 0 {
fmt.Fprintln(w)
fmt.Fprintf(w, " Tags: %s\n", tagColor.Sprint(strings.Join(tags, " • ")))
}
if tags := getDisplayTags(n); len(tags) > 0 {
fmt.Fprintf(w, "\n tags: %s\n\n", cPrimary.Sprint(strings.Join(tags, " • ")))
}
if len(n.Relations) > 0 {
fmt.Fprintln(w)
fmt.Fprintln(w, labelColor.Sprint(" Relations:"))
for relType, ids := range n.Relations {
if relType == "created" {
continue
if db, err := db.GetDB(); err == nil {
if len(n.Relations) > 0 {
for relType, ids := range n.Relations {
fmt.Fprintf(w, " %s\n", string(relType))
if relIcon, ok := relIcons[string(relType)]; ok && relType != "created" {
for _, id := range ids {
node, err := db.NodeByID(id)
if err == nil {
fmt.Fprintf(w, " %s %s\n", relIcon, node.Title)
}
}
}
}
relIcon := ""
switch relType {
case "blocks":
relIcon = icons.Blocks
case "subtask":
relIcon = icons.Subtask
case "related":
relIcon = icons.Related
case "assignee":
relIcon = icons.Assignee
}
coloredIDs := make([]string, len(ids))
for i, id := range ids {
coloredIDs[i] = idColor.Sprint(id)
}
fmt.Fprintf(w, " %s %s %s\n", relIcon, strings.Title(string(relType)), strings.Join(coloredIDs, " "))
}
} else {
fmt.Fprintf(w, "failed to attach to db: %v", err)
}
fmt.Fprintln(w)
@@ -299,30 +177,27 @@ func PrintAliases(w io.Writer, aliases []*db.Alias, jsonOut bool) error {
return json.NewEncoder(w).Encode(aliases)
}
if len(aliases) == 0 {
fmt.Fprintln(w, dimColor.Sprint(" No aliases defined."))
fmt.Fprintln(w, cDim.Sprint("No aliases defined."))
return nil
}
fmt.Fprintln(w)
for _, a := range aliases {
fmt.Fprintf(w, " %s %s\n",
idColor.Sprint(a.Name),
dimColor.Sprint(a.Command),
)
fmt.Fprintf(w, " %s %s\n", cPrimary.Sprint(a.Name), cDim.Sprint(a.Command))
}
fmt.Fprintln(w)
return nil
}
func PrintSuccess(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, icons.Check+" "+format+"\n", args...)
}
func PrintDeleted(w io.Writer, id string) {
fmt.Fprintf(w, " "+icons.Cross+" Deleted %s\n", idColor.Sprint(id))
}
func PrintCreated(w io.Writer, dbPath string) {
fmt.Fprintf(w, " "+icons.Namespace+" Created %s\n", dimColor.Sprint(dbPath))
func PrintAction(w io.Writer, action, detail string, isError bool) {
if isError {
fmt.Fprintln(w, cBad.Sprint(iconCross+" "+action+" ")+cDim.Sprint(detail))
return
}
icon := iconCheck
if action == "Created" {
icon = iconNamespace
}
fmt.Fprintln(w, cGood.Sprint(icon+" "+action+" ")+cDim.Sprint(detail))
}
func truncate(s string, max int) string {