Add pretty printing, complete Makefile

This commit is contained in:
2024-11-27 16:06:30 -05:00
parent 8ba2caa16d
commit 4dc24d9961
8 changed files with 271 additions and 173 deletions

View File

@ -1,10 +1,11 @@
package get
import (
"fmt"
"slices"
"strings"
"github.com/k0kubun/pp/v3"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
@ -40,7 +41,10 @@ func RunGetRouteCmd(cmd *cobra.Command, args []string) {
logger.Fatal().Err(err).Send()
}
pp.Println(route)
bytes, _ := yaml.Marshal(route)
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
Bytes: append([]byte(fmt.Sprintf("name: %s\n", args[0])), bytes...),
})))
}
func completeRouteRoutes(cmd *cobra.Command, args []string, toComplete string) (

View File

@ -0,0 +1,75 @@
package util
import (
"fmt"
"github.com/fatih/color"
"github.com/goccy/go-yaml/lexer"
"github.com/goccy/go-yaml/printer"
)
const escape = "\x1b"
type PrettyOpts struct {
Bytes []byte
LineNumbers bool
}
func PrettyBytes(opts *PrettyOpts) []byte {
tokens := lexer.Tokenize(string(opts.Bytes))
var p printer.Printer
p.LineNumber = opts.LineNumbers
p.LineNumberFormat = func(num int) string {
fn := color.New(color.Bold, color.FgHiWhite).SprintFunc()
return fn(fmt.Sprintf("%2d | ", num))
}
p.Bool = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
}
}
p.Number = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
}
}
p.MapKey = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiCyan),
Suffix: format(color.Reset),
}
}
p.Anchor = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
}
}
p.Alias = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
}
}
p.String = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiGreen),
Suffix: format(color.Reset),
}
}
p.Comment = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiBlack),
Suffix: format(color.Reset),
}
}
return []byte(p.PrintTokens(tokens))
}
func format(attr color.Attribute) string {
return fmt.Sprintf("%s[%dm", escape, attr)
}