Skip to content

Commit

Permalink
another output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
paragor committed Aug 10, 2024
1 parent 9c55258 commit cf1871c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
42 changes: 34 additions & 8 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ import (
"github.com/spf13/cobra"
"log"
"net/http"
"slices"
"strings"
"time"
)

var clientOutput = "table"
var clientOutputAllowed = []string{"table", "json", "csv", "markdown", "html", "tsv"}

func init() {
rootCmd.AddCommand(clientCmd)
clientCmd.Flags().StringVarP(&clientOutput, "output", "o", clientOutput, "output format (json, table)")
clientCmd.Flags().StringVarP(
&clientOutput,
"output",
"o",
clientOutput,
fmt.Sprintf("output format (%s)", strings.Join(clientOutputAllowed, ", ")),
)
}

var clientCmd = &cobra.Command{
Use: "client",
Short: "Run todolist console client",
Long: models.HumanInputHelp,
RunE: func(cmd *cobra.Command, args []string) error {
if clientOutput != "json" && clientOutput != "table" {
if !slices.Contains(clientOutputAllowed, clientOutput) {
return fmt.Errorf("unknown output format")
}
input := strings.Join(args, " ")
Expand Down Expand Up @@ -111,18 +119,36 @@ func outputAgenda(agenda []models.TaskGroup) {
if clientOutput == "json" {
fmt.Println(prettyOutputJson(agenda))
} else {
fmt.Println(prettyOutputTasksGroupsTable(agenda))
fmt.Println(outputTableWriter(prettyOutputTasksGroupsTable(agenda)))
}
}

func outputTasks(tasks []*models.Task) {
if clientOutput == "json" {
fmt.Println(prettyOutputJson(tasks))
} else {
fmt.Println(prettyOutputTasksTable(tasks))
fmt.Println(outputTableWriter(prettyOutputTasksTable(tasks)))
}
}
func prettyOutputTasksGroupsTable(groups []models.TaskGroup) string {

func outputTableWriter(tableWriter table.Writer) string {
switch clientOutput {
case "table":
return tableWriter.Render()
case "csv":
return tableWriter.RenderCSV()
case "markdown":
return tableWriter.RenderMarkdown()
case "html":
return tableWriter.RenderHTML()
case "tsv":
return tableWriter.RenderTSV()
default:
return tableWriter.Render()
}
}

func prettyOutputTasksGroupsTable(groups []models.TaskGroup) table.Writer {
tableWriter := table.NewWriter()
headerRow := tableGetTasksHeaderRow()
tableWriter.AppendHeader(headerRow)
Expand All @@ -138,14 +164,14 @@ func prettyOutputTasksGroupsTable(groups []models.TaskGroup) string {
tableWriter.AppendSeparator()
tableWriter.AppendRows(tableGetTasksBodyRows(group.Tasks))
}
return tableWriter.Render()
return tableWriter
}

func prettyOutputTasksTable(tasks []*models.Task) string {
func prettyOutputTasksTable(tasks []*models.Task) table.Writer {
tableWriter := table.NewWriter()
tableWriter.AppendHeader(tableGetTasksHeaderRow())
tableWriter.AppendRows(tableGetTasksBodyRows(tasks))
return tableWriter.Render()
return tableWriter
}

func tableGetTasksHeaderRow() table.Row {
Expand Down
24 changes: 7 additions & 17 deletions pkg/models/human_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,13 @@ func ParseHumanInput(input string) (*HumanInputParserResult, error) {
firstSpace = len(input)
}
result := &HumanInputParserResult{}
var action HumanAction
switch strings.ToLower(input[:firstSpace]) {
case string(HumanActionAdd):
action = HumanActionAdd
case string(HumanActionModify):
action = HumanActionModify
case string(HumanActionList):
action = HumanActionList
case string(HumanActionInfo):
action = HumanActionInfo
case string(HumanActionCopy):
action = HumanActionCopy
case string(HumanActionDone):
action = HumanActionDone
case string(HumanActionAgenda):
action = HumanActionAgenda
default:
action := HumanAction(strings.ToLower(input[:firstSpace]))
allActions := []HumanAction{
HumanActionAdd, HumanActionModify, HumanActionList,
HumanActionInfo, HumanActionCopy, HumanActionDone,
HumanActionAgenda,
}
if !slices.Contains(allActions, action) {
return nil, fmt.Errorf("invalid action: %s", input[:firstSpace])
}
result.Action = action
Expand Down

0 comments on commit cf1871c

Please sign in to comment.