gte/cmd/cli/format/format.go

83 lines
1.6 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
2022-06-05 15:00:28 +02:00
COL_DUE
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 (
2022-06-05 15:00:28 +02:00
COL_ALL = []Column{COL_ID, COL_STATUS, COL_DUE, COL_ACTION, COL_PROJECT}
2022-06-05 13:00:23 +02:00
)
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())
}
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
}