gte/cmd/cli/format/format.go

155 lines
3.0 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-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-05 13:00:23 +02:00
type Column int
2022-06-04 14:28:45 +02:00
const (
2022-06-05 13:00:23 +02:00
COL_ID Column = iota
2022-06-04 14:28:45 +02:00
COL_STATUS
COL_DATE
2022-06-05 13:00:23 +02:00
COL_ACTION
2022-06-04 14:28:45 +02:00
COL_PROJECT
)
2022-06-05 13:00:23 +02:00
var (
COL_ALL = []Column{COL_ID, COL_STATUS, COL_DATE, COL_ACTION, 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-05 13:00:23 +02:00
func FormatTaskTable(tasks []*task.LocalTask, cols []Column) string {
2021-07-14 07:17:53 +02:00
if len(tasks) == 0 {
return "no tasks to display\n"
}
2022-06-04 14:28:45 +02:00
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:
2022-06-05 13:00:23 +02:00
var updated []string
2022-06-04 14:28:45 +02:00
if t.LocalStatus == task.STATUS_UPDATED {
2022-06-05 13:00:23 +02:00
updated = append(updated, "u")
}
if task.Today.After(t.Due) {
updated = append(updated, "o")
2022-06-04 14:28:45 +02:00
}
2022-06-05 13:00:23 +02:00
line = append(line, strings.Join(updated, ""))
2022-06-04 14:28:45 +02:00
case COL_DATE:
line = append(line, t.Due.String())
2022-06-05 13:00:23 +02:00
case COL_ACTION:
2022-06-04 14:28:45 +02:00
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-05 13:00:23 +02:00
return fmt.Sprintf("\n%s", 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
2022-06-05 11:33:07 +02:00
for r, line := range data {
if r%3 == 0 {
output += fmt.Sprintf("%s", "\x1b[48;5;237m")
}
for c, col := range line {
2022-06-04 14:28:45 +02:00
output += col
2022-06-05 11:33:07 +02:00
for s := 0; s < max[c]-len(col); s++ {
2022-06-04 14:28:45 +02:00
output += " "
}
2022-06-05 13:00:23 +02:00
if c != len(line)-1 {
output += " "
}
2022-06-04 14:28:45 +02:00
}
2022-06-05 11:33:07 +02:00
if r%3 == 0 {
output += fmt.Sprintf("%s", "\x1b[49m")
}
output += "\r\n"
2022-06-04 14:28:45 +02:00
}
return output
}