Compare commits
2 Commits
fix/table-
...
5205ddf094
| Author | SHA1 | Date | |
|---|---|---|---|
| 5205ddf094 | |||
| 79c6a1ea8a |
13
cmd/root.go
13
cmd/root.go
@ -4,8 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/output"
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -15,6 +19,7 @@ var rootCmd = &cobra.Command{
|
||||
Short: "Unifood is a CLI for retrieving restaurant information",
|
||||
Long: ``,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_ = cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
@ -30,11 +35,17 @@ func Execute() {
|
||||
func initRootCmd() {
|
||||
var appConfig AppConfig
|
||||
|
||||
// Compile list of available formatters
|
||||
formatters := slices.AppendSeq([]string{}, maps.Keys(output.Formatters))
|
||||
formattersList := fmt.Sprintf("(available: %s)", strings.Join(formatters, ", "))
|
||||
|
||||
// Add persistent flags
|
||||
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
|
||||
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format")
|
||||
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format "+formattersList)
|
||||
rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order")
|
||||
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
|
||||
|
||||
// Create logger and add child commands
|
||||
logger := jlog.New(slog.LevelDebug)
|
||||
ctx := jlog.ContextWith(context.Background(), logger)
|
||||
|
||||
|
||||
@ -12,4 +12,5 @@ var Formatters = map[string]interfaces.Formatter{
|
||||
"csv": &TableFormatter{HideSummary: true, RenderFormat: tableFormatCSV},
|
||||
"html": &TableFormatter{HideSummary: true, RenderFormat: tableFormatHTML},
|
||||
"markdown": &TableFormatter{HideSummary: true, RenderFormat: tableFormatMarkdown},
|
||||
"name": &NameFormatter{},
|
||||
}
|
||||
|
||||
21
core/output/name.go
Normal file
21
core/output/name.go
Normal file
@ -0,0 +1,21 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
|
||||
)
|
||||
|
||||
type NameFormatter struct{}
|
||||
|
||||
func (f *NameFormatter) Format(list *interfaces.ResourceList) (io.Reader, error) {
|
||||
var buffer = make([]byte, 0, 1024)
|
||||
outputBuffer := bytes.NewBuffer(buffer)
|
||||
|
||||
for _, item := range list.Items {
|
||||
outputBuffer.WriteString(item.ItemName() + "\r\n")
|
||||
}
|
||||
|
||||
return outputBuffer, nil
|
||||
}
|
||||
@ -41,6 +41,7 @@ func (f *TableFormatter) Format(list *interfaces.ResourceList) (io.Reader, error
|
||||
|
||||
// Setup table
|
||||
t := table.NewWriter()
|
||||
t.SetOutputMirror(outputBuffer)
|
||||
t.SetStyle(table.StyleLight)
|
||||
|
||||
// Write header
|
||||
@ -65,22 +66,15 @@ func (f *TableFormatter) Format(list *interfaces.ResourceList) (io.Reader, error
|
||||
}
|
||||
|
||||
// Render
|
||||
output := func() string {
|
||||
switch f.RenderFormat {
|
||||
case tableFormatCSV:
|
||||
return t.RenderCSV()
|
||||
t.RenderCSV()
|
||||
case tableFormatHTML:
|
||||
return t.RenderHTML()
|
||||
t.RenderHTML()
|
||||
case tableFormatMarkdown:
|
||||
return t.RenderMarkdown()
|
||||
t.RenderMarkdown()
|
||||
default:
|
||||
return t.Render()
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := outputBuffer.WriteString(output)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to write rendered table: %w", err)
|
||||
t.Render()
|
||||
}
|
||||
|
||||
return outputBuffer, nil
|
||||
|
||||
Reference in New Issue
Block a user