gte/cmd/cli/format/format.go

165 lines
3.3 KiB
Go
Raw Normal View History

2021-07-10 11:44:06 +02:00
package format
import (
2021-09-04 12:20:35 +02:00
"errors"
2021-07-10 11:44:06 +02:00
"fmt"
2021-08-20 11:27:12 +02:00
"sort"
2021-09-04 12:20:35 +02:00
"strings"
2021-07-10 11:44:06 +02:00
2021-09-19 11:59:26 +02:00
"ewintr.nl/gte/internal/task"
2021-07-10 11:44:06 +02:00
)
2021-09-04 12:20:35 +02:00
var (
ErrFieldAlreadyUsed = errors.New("field was already used")
)
2022-06-04 14:28:45 +02:00
type column int
const (
COL_ID column = iota
COL_STATUS
COL_DATE
COL_TASK
COL_PROJECT
)
2021-07-10 11:44:06 +02:00
func FormatError(err error) string {
return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error())
}
2022-06-04 14:28:45 +02:00
func FormatTasks(tasks []*task.LocalTask) string {
2021-07-14 07:17:53 +02:00
if len(tasks) == 0 {
return "no tasks to display\n"
}
2021-08-20 17:50:08 +02:00
sort.Sort(task.ByDefault(tasks))
2021-08-20 11:27:12 +02:00
2022-06-04 14:28:45 +02:00
normal, recurring := []*task.LocalTask{}, []*task.LocalTask{}
for _, lt := range tasks {
if lt.Folder == task.FOLDER_RECURRING {
recurring = append(recurring, lt)
continue
}
2022-06-04 14:28:45 +02:00
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
for _, t := range tasks {
var line []string
for _, col := range cols {
switch col {
case COL_ID:
line = append(line, fmt.Sprintf("%d", t.LocalId))
case COL_STATUS:
var updated string
if t.LocalStatus == task.STATUS_UPDATED {
updated = "*"
}
line = append(line, updated)
case COL_DATE:
line = append(line, t.Due.String())
case COL_TASK:
line = append(line, t.Action)
case COL_PROJECT:
line = append(line, t.Project)
}
2021-09-23 06:50:54 +02:00
}
2022-06-04 14:28:45 +02:00
data = append(data, line)
2021-07-10 11:44:06 +02:00
}
2022-06-04 14:28:45 +02:00
return FormatTable(data)
2021-07-10 11:44:06 +02:00
}
2021-07-27 07:10:01 +02:00
func FormatTask(t *task.LocalTask) string {
2021-08-16 07:09:06 +02:00
output := fmt.Sprintf(`folder: %s
action: %s
2021-07-27 07:10:01 +02:00
project: %s
due: %s
2021-08-16 07:09:06 +02:00
`, t.Folder, t.Action, t.Project, t.Due.String())
2021-07-27 07:10:01 +02:00
if t.IsRecurrer() {
output += fmt.Sprintf("recur:%s", t.Recur.String())
}
2021-09-04 19:47:36 +02:00
return fmt.Sprintf("%s\n", output)
2021-07-27 07:10:01 +02:00
}
2021-09-04 12:20:35 +02:00
func ParseTaskFieldArgs(args []string) (*task.LocalUpdate, error) {
lu := &task.LocalUpdate{}
action, fields := []string{}, []string{}
for _, f := range args {
split := strings.SplitN(f, ":", 2)
if len(split) == 2 {
switch split[0] {
case "project":
if lu.Project != "" {
return &task.LocalUpdate{}, fmt.Errorf("%w: %s", ErrFieldAlreadyUsed, task.FIELD_PROJECT)
}
lu.Project = split[1]
fields = append(fields, task.FIELD_PROJECT)
case "due":
if !lu.Due.IsZero() {
return &task.LocalUpdate{}, fmt.Errorf("%w: %s", ErrFieldAlreadyUsed, task.FIELD_DUE)
}
lu.Due = task.NewDateFromString(split[1])
fields = append(fields, task.FIELD_DUE)
}
} else {
if len(f) > 0 {
action = append(action, f)
}
}
}
if len(action) > 0 {
lu.Action = strings.Join(action, " ")
fields = append(fields, task.FIELD_ACTION)
}
lu.Fields = fields
return lu, nil
}
2022-06-04 14:28:45 +02:00
func FormatTable(data [][]string) string {
if len(data) == 0 {
return ""
}
max := make([]int, len(data))
for _, line := range data {
for i, col := range line {
if len(col) > max[i] {
max[i] = len(col)
}
}
}
var output string
for _, line := range data {
for i, col := range line {
output += col
for s := 0; s < max[i]-len(col); s++ {
output += " "
}
output += " "
}
output += "\n"
}
return output
}