cloumns today, status

This commit is contained in:
Erik Winter 2022-06-05 13:00:23 +02:00
parent b1d35b7f02
commit 56a24ff4b8
6 changed files with 29 additions and 48 deletions

View File

@ -51,9 +51,5 @@ func (f *Folder) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
if len(res.Tasks) == 0 { return format.FormatTaskTable(res.Tasks, format.COL_ALL)
return "no tasks here\n\n"
}
return format.FormatTasks(res.Tasks)
} }

View File

@ -38,9 +38,5 @@ func (p *Project) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
if len(res.Tasks) == 0 { return format.FormatTaskTable(res.Tasks, format.COL_ALL)
return "no tasks here\n\n"
}
return format.FormatTasks(res.Tasks)
} }

View File

@ -1,6 +1,8 @@
package command package command
import ( import (
"sort"
"ewintr.nl/gte/cmd/cli/format" "ewintr.nl/gte/cmd/cli/format"
"ewintr.nl/gte/internal/configuration" "ewintr.nl/gte/internal/configuration"
"ewintr.nl/gte/internal/process" "ewintr.nl/gte/internal/process"
@ -36,5 +38,8 @@ func (t *Today) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
return format.FormatTasks(res.Tasks) sort.Sort(task.ByDefault(res.Tasks))
cols := []format.Column{format.COL_ID, format.COL_STATUS, format.COL_ACTION, format.COL_PROJECT}
return format.FormatTaskTable(res.Tasks, cols)
} }

View File

@ -36,5 +36,5 @@ func (t *Tomorrow) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
return format.FormatTasks(res.Tasks) return format.FormatTaskTable(res.Tasks, format.COL_ALL)
} }

View File

@ -34,5 +34,5 @@ func (w *Week) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
return format.FormatTasks(res.Tasks) return format.FormatTaskTable(res.Tasks, format.COL_ALL)
} }

View File

@ -3,7 +3,6 @@ package format
import ( import (
"errors" "errors"
"fmt" "fmt"
"sort"
"strings" "strings"
"ewintr.nl/gte/internal/task" "ewintr.nl/gte/internal/task"
@ -13,49 +12,29 @@ var (
ErrFieldAlreadyUsed = errors.New("field was already used") ErrFieldAlreadyUsed = errors.New("field was already used")
) )
type column int type Column int
const ( const (
COL_ID column = iota COL_ID Column = iota
COL_STATUS COL_STATUS
COL_DATE COL_DATE
COL_TASK COL_ACTION
COL_PROJECT COL_PROJECT
) )
var (
COL_ALL = []Column{COL_ID, COL_STATUS, COL_DATE, COL_ACTION, COL_PROJECT}
)
func FormatError(err error) string { func FormatError(err error) string {
return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error()) return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error())
} }
func FormatTasks(tasks []*task.LocalTask) string { func FormatTaskTable(tasks []*task.LocalTask, cols []Column) string {
if len(tasks) == 0 { if len(tasks) == 0 {
return "no tasks to display\n" return "no tasks to display\n"
} }
sort.Sort(task.ByDefault(tasks))
normal, recurring := []*task.LocalTask{}, []*task.LocalTask{}
for _, lt := range tasks {
if lt.Folder == task.FOLDER_RECURRING {
recurring = append(recurring, lt)
continue
}
normal = append(normal, lt)
}
output := ""
if len(recurring) > 0 {
output = fmt.Sprintf("%s\nRecurring:\n%s\nTasks:\n", output, FormatTaskTable(recurring))
}
return fmt.Sprintf("%s\n%s\n", output, FormatTaskTable(normal))
}
func FormatTaskTable(tasks []*task.LocalTask, cols ...column) string {
if len(cols) == 0 {
cols = []column{COL_ID, COL_STATUS, COL_DATE, COL_TASK, COL_PROJECT}
}
var data [][]string var data [][]string
for _, t := range tasks { for _, t := range tasks {
var line []string var line []string
@ -64,14 +43,17 @@ func FormatTaskTable(tasks []*task.LocalTask, cols ...column) string {
case COL_ID: case COL_ID:
line = append(line, fmt.Sprintf("%d", t.LocalId)) line = append(line, fmt.Sprintf("%d", t.LocalId))
case COL_STATUS: case COL_STATUS:
var updated string var updated []string
if t.LocalStatus == task.STATUS_UPDATED { if t.LocalStatus == task.STATUS_UPDATED {
updated = "*" updated = append(updated, "u")
} }
line = append(line, updated) if task.Today.After(t.Due) {
updated = append(updated, "o")
}
line = append(line, strings.Join(updated, ""))
case COL_DATE: case COL_DATE:
line = append(line, t.Due.String()) line = append(line, t.Due.String())
case COL_TASK: case COL_ACTION:
line = append(line, t.Action) line = append(line, t.Action)
case COL_PROJECT: case COL_PROJECT:
line = append(line, t.Project) line = append(line, t.Project)
@ -80,7 +62,7 @@ func FormatTaskTable(tasks []*task.LocalTask, cols ...column) string {
data = append(data, line) data = append(data, line)
} }
return FormatTable(data) return fmt.Sprintf("\n%s", FormatTable(data))
} }
func FormatTask(t *task.LocalTask) string { func FormatTask(t *task.LocalTask) string {
@ -157,7 +139,9 @@ func FormatTable(data [][]string) string {
for s := 0; s < max[c]-len(col); s++ { for s := 0; s < max[c]-len(col); s++ {
output += " " output += " "
} }
output += " " if c != len(line)-1 {
output += " "
}
} }
if r%3 == 0 { if r%3 == 0 {
output += fmt.Sprintf("%s", "\x1b[49m") output += fmt.Sprintf("%s", "\x1b[49m")