Skip to content

Commit

Permalink
refactor: switch to pterm
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Aug 9, 2024
1 parent 807c11b commit 1468174
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 90 deletions.
29 changes: 11 additions & 18 deletions cmd/gt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,26 @@ import (
"fmt"
"os"

"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/colors"
"github.com/pterm/pterm"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

const goTemplate = "go/template"

func main() {
output := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.EnvColorProfile()))

cmd := buildRootCommand(output)
cmd := buildRootCommand()
if err := cmd.Execute(); err != nil {
printError(output, err)
printError(err)
os.Exit(1)
}
}

func printError(output *termenv.Output, err error) {
redStyler := output.String().Foreground(output.Color(colors.Red))

_, _ = fmt.Fprintf(
os.Stderr, "%s: %s\n",
redStyler.Bold().Styled("ERROR"),
redStyler.Styled(err.Error()),
)
func printError(err error) {
pterm.Error.PrintOnError(err)
}

func buildRootCommand(output *termenv.Output) *cobra.Command {
func buildRootCommand() *cobra.Command {
gt := gotemplate.New()

cmd := &cobra.Command{
Expand All @@ -48,23 +39,25 @@ To begin working with %[1]s, run the 'gt new' command:
This will prompt you to create a new Golang project using standard configs.
For more information, please visit the project's Github page: github.com/schwarzit/go-template.`,
output.String(goTemplate).Foreground(output.Color(colors.Cyan)),
pterm.Cyan(goTemplate),
),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Enable swapping out stdout/stderr for testing
gt.Out = cmd.OutOrStdout()
gt.Err = cmd.OutOrStderr()
gt.InScanner = bufio.NewScanner(cmd.InOrStdin())

pterm.SetDefaultOutput(gt.Out)

gt.CheckVersion()
},
// don't show errors and usage on errors in any RunE function.
SilenceErrors: true,
SilenceUsage: true,
}

cmd.AddCommand(buildNewCommand(output, gt))
cmd.AddCommand(buildVersionCommand(output, gt))
cmd.AddCommand(buildNewCommand(gt))
cmd.AddCommand(buildVersionCommand(gt))

return cmd
}
6 changes: 3 additions & 3 deletions cmd/gt/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package main
import (
"fmt"

"github.com/muesli/termenv"
"github.com/pterm/pterm"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

func buildNewCommand(output *termenv.Output, gt *gotemplate.GT) *cobra.Command {
func buildNewCommand(gt *gotemplate.GT) *cobra.Command {
var (
configFile string
opts gotemplate.NewRepositoryOptions
)

underline := output.String().Underline().Styled
underline := pterm.Underscore.Sprint

cmd := &cobra.Command{
Use: "new",
Expand Down
7 changes: 3 additions & 4 deletions cmd/gt/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package main
import (
"fmt"

"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/colors"
"github.com/pterm/pterm"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

func buildVersionCommand(output *termenv.Output, gt *gotemplate.GT) *cobra.Command {
goTemplateHighlighted := output.String(goTemplate).Foreground(output.Color(colors.Cyan))
func buildVersionCommand(gt *gotemplate.GT) *cobra.Command {
goTemplateHighlighted := pterm.Cyan(goTemplate)
cmd := &cobra.Command{
Use: "version",
Short: fmt.Sprintf("Print the version number of %s", goTemplateHighlighted),
Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/golangci/golangci-lint v1.59.1
github.com/google/go-github/v56 v56.0.0
github.com/muesli/termenv v0.15.2
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.79
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/vuln v1.1.3
Expand All @@ -18,6 +18,9 @@ require (
require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/4meepo/tagalign v1.3.4 // indirect
github.com/Abirdcfly/dupword v0.0.14 // indirect
github.com/Antonboom/errname v0.1.13 // indirect
Expand All @@ -35,7 +38,6 @@ require (
github.com/alingse/asasalint v0.0.11 // indirect
github.com/ashanbrown/forbidigo v1.6.0 // indirect
github.com/ashanbrown/makezero v1.1.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bkielbasa/cyclop v1.2.1 // indirect
github.com/blizzy78/varnamelen v0.8.0 // indirect
Expand All @@ -50,6 +52,7 @@ require (
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/ckaznocha/intrange v0.1.2 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
github.com/daixiang0/gci v0.13.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand Down Expand Up @@ -83,6 +86,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gordonklaus/ineffassign v0.1.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
Expand All @@ -109,7 +113,7 @@ require (
github.com/ldez/gomoddirectives v0.2.4 // indirect
github.com/ldez/tagliatelle v0.5.0 // indirect
github.com/leonklingele/grouper v1.1.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lufeee/execinquery v1.2.1 // indirect
github.com/macabu/inamedparam v0.1.3 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down Expand Up @@ -179,6 +183,7 @@ require (
github.com/ultraware/whitespace v0.1.1 // indirect
github.com/uudashr/gocognit v1.1.2 // indirect
github.com/xen0n/gosmopolitan v1.2.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.3.0 // indirect
github.com/ykadowak/zerologlint v0.1.5 // indirect
Expand All @@ -195,6 +200,7 @@ require (
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/telemetry v0.0.0-20240717194752-0b706e19b701 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
Loading

0 comments on commit 1468174

Please sign in to comment.