diff --git a/cmd/cli/command/folder.go b/cmd/cli/command/folder.go index 3251914..51da1f3 100644 --- a/cmd/cli/command/folder.go +++ b/cmd/cli/command/folder.go @@ -55,5 +55,5 @@ func (f *Folder) Do() string { return "no tasks here\n\n" } - return format.FormatTaskTable(res.Tasks) + return format.FormatTasks(res.Tasks) } diff --git a/cmd/cli/command/project.go b/cmd/cli/command/project.go index 6a2a4ac..7bdac14 100644 --- a/cmd/cli/command/project.go +++ b/cmd/cli/command/project.go @@ -42,5 +42,5 @@ func (p *Project) Do() string { return "no tasks here\n\n" } - return format.FormatTaskTable(res.Tasks) + return format.FormatTasks(res.Tasks) } diff --git a/cmd/cli/command/today.go b/cmd/cli/command/today.go index 8958261..6093d44 100644 --- a/cmd/cli/command/today.go +++ b/cmd/cli/command/today.go @@ -36,5 +36,5 @@ func (t *Today) Do() string { return format.FormatError(err) } - return format.FormatTaskTable(res.Tasks) + return format.FormatTasks(res.Tasks) } diff --git a/cmd/cli/command/tomorrow.go b/cmd/cli/command/tomorrow.go index bbe22a9..8be4ebd 100644 --- a/cmd/cli/command/tomorrow.go +++ b/cmd/cli/command/tomorrow.go @@ -36,5 +36,5 @@ func (t *Tomorrow) Do() string { return format.FormatError(err) } - return format.FormatTaskTable(res.Tasks) + return format.FormatTasks(res.Tasks) } diff --git a/cmd/cli/command/week.go b/cmd/cli/command/week.go index c79e134..526d73e 100644 --- a/cmd/cli/command/week.go +++ b/cmd/cli/command/week.go @@ -34,5 +34,5 @@ func (w *Week) Do() string { return format.FormatError(err) } - return format.FormatTaskTable(res.Tasks) + return format.FormatTasks(res.Tasks) } diff --git a/cmd/cli/format/format.go b/cmd/cli/format/format.go index 9e6d918..489b8af 100644 --- a/cmd/cli/format/format.go +++ b/cmd/cli/format/format.go @@ -13,30 +13,74 @@ var ( ErrFieldAlreadyUsed = errors.New("field was already used") ) +type column int + +const ( + COL_ID column = iota + COL_STATUS + COL_DATE + COL_TASK + COL_PROJECT +) + func FormatError(err error) string { return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error()) } -func FormatTaskTable(tasks []*task.LocalTask) string { +func FormatTasks(tasks []*task.LocalTask) string { if len(tasks) == 0 { return "no tasks to display\n" } sort.Sort(task.ByDefault(tasks)) - var output string - for _, t := range tasks { - var projectStr, updateStr string - if t.LocalStatus == task.STATUS_UPDATED { - updateStr = " *" + normal, recurring := []*task.LocalTask{}, []*task.LocalTask{} + for _, lt := range tasks { + if lt.Folder == task.FOLDER_RECURRING { + recurring = append(recurring, lt) + continue } - if t.Folder != task.FOLDER_NEW { - projectStr = fmt.Sprintf(" (%s)", t.Project) - } - output += fmt.Sprintf("%d%s\t%s\t%s%s\n", t.LocalId, updateStr, t.Due.String(), t.Action, projectStr) + normal = append(normal, lt) } - return fmt.Sprintf("%s\n", output) + 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) + } + } + data = append(data, line) + } + + return FormatTable(data) } func FormatTask(t *task.LocalTask) string { @@ -89,3 +133,32 @@ func ParseTaskFieldArgs(args []string) (*task.LocalUpdate, error) { return lu, nil } + +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 +}