Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colour to command line output #284

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion framework/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/asmaloney/gactar/actr"
"github.com/asmaloney/gactar/amod"

"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/filesystem"
"github.com/asmaloney/gactar/util/python"
)
Expand Down Expand Up @@ -107,7 +108,8 @@ func identifyYourself(frameworkName, exeName string) (err error) {

version := strings.TrimSpace(string(output))

fmt.Printf("%s: Using %s\n", frameworkName, version)
fmt.Print(chalk.Header(frameworkName + ": "))
fmt.Printf("Using %s\n", version)

return
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5
github.com/jwalton/gchalk v1.3.0
github.com/kylelemons/godebug v1.1.0
github.com/urfave/cli/v2 v2.17.1
github.com/vearutop/statigz v1.1.8
Expand All @@ -12,6 +13,9 @@ require (
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/jwalton/go-supportscolor v1.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/jwalton/gchalk v1.3.0 h1:uTfAaNexN8r0I9bioRTksuT8VGjrPs9YIXR1PQbtX/Q=
github.com/jwalton/gchalk v1.3.0/go.mod h1:ytRlj60R9f7r53IAElbpq4lVuPOPNg2J4tJcCxtFqr8=
github.com/jwalton/go-supportscolor v1.1.0 h1:HsXFJdMPjRUAx8cIW6g30hVSFYaxh9yRQwEWgkAR7lQ=
github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -21,4 +25,12 @@ github.com/vearutop/statigz v1.1.8 h1:IJgQHx6EomuYOYd2TzFt3haP+BIzV471zn7aepRiLH
github.com/vearutop/statigz v1.1.8/go.mod h1:pfzrpvgLRnFeSVZd9iUYrpYDLqbV+RgeCfizr3ZFf44=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef h1:fPxZ3Umkct3LZ8gK9nbk+DWDJ9fstZa2grBn+lWVKPs=
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/asmaloney/gactar/modes/shell"
"github.com/asmaloney/gactar/modes/web"

"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/clicontext"
"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/filesystem"
Expand All @@ -37,7 +38,7 @@ type ErrCmdLine struct {
}

func (e *ErrCmdLine) Error() string {
return fmt.Sprintf("error: %s", e.Message)
return chalk.Error("error:", e.Message)
}

func main() {
Expand Down Expand Up @@ -78,14 +79,14 @@ func main() {
Action: func(c *cli.Context) error {
envPath, err := clicontext.ExpandPath(c, "path")
if err != nil {
fmt.Println(err.Error())
chalk.PrintErr(err)
return err
}

dev := c.Bool("dev")
err = setup.Setup(envPath, dev)
if err != nil {
fmt.Println(err.Error())
chalk.PrintErr(err)
return err
}

Expand All @@ -106,7 +107,7 @@ func main() {
Action: func(c *cli.Context) error {
envPath, err := clicontext.ExpandPath(c, "path")
if err != nil {
fmt.Println(err.Error())
chalk.PrintErr(err)
return err
}

Expand All @@ -117,7 +118,7 @@ func main() {

err = setup.Doctor(envPath)
if err != nil {
fmt.Println(err.Error())
chalk.PrintErr(err)
return err
}

Expand Down Expand Up @@ -156,7 +157,7 @@ func main() {
Action: func(c *cli.Context) error {
// Override cli's version printing so we ensure it comes first
cli.VersionPrinter = func(c *cli.Context) {}
fmt.Printf("%s version %s\n", c.App.Name, c.App.Version)
fmt.Println(chalk.Bold(c.App.Name, "version", c.App.Version))

if c.Bool("debug") {
amod.SetDebug(true)
Expand Down Expand Up @@ -198,7 +199,7 @@ func main() {
}

if c.Int("port") != defaultPort {
fmt.Println("info: --port only applies when using --web")
chalk.PrintWarningStr("info: --port only applies when using --web")
}

if c.Bool("interactive") {
Expand Down Expand Up @@ -240,7 +241,8 @@ func setupVirtualEnvironment(ctx *cli.Context) (err error) {
return
}

fmt.Printf("Using virtual environment: %q\n", envPath)
fmt.Print(chalk.Header("Using virtual environment: "))
fmt.Printf("%q\n", envPath)

err = clicontext.SetupPaths(envPath)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion modes/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"sync"

"github.com/jwalton/gchalk"
"github.com/urfave/cli/v2"
"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
Expand Down Expand Up @@ -97,7 +98,8 @@ func Initialize(cli *cli.Context, frameworks framework.List, examples *embed.FS)
}

func (w Web) Start() (err error) {
fmt.Printf("Serving gactar on http://localhost:%d\n", w.port)
fmt.Printf("Serving gactar on ")
fmt.Println(gchalk.WithBlue().Underline(fmt.Sprintf("http://localhost:%d", w.port)))

err = http.ListenAndServe(fmt.Sprintf(":%d", w.port), nil)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions util/chalk/chalk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package chalk provides convenience utilities for working with colourized output using gchalk.
package chalk

import (
"fmt"

"github.com/jwalton/gchalk"
)

var (
Error = gchalk.WithBold().Red
Warning = gchalk.Yellow

Bold = gchalk.Bold
Header = gchalk.Green
)

func PrintErr(err error) {
fmt.Println(Error("error:", err.Error()))
}

func PrintErrStr(str ...string) {
fmt.Print(Error("error: "))
fmt.Println(Error(str...))
}

func PrintWarningStr(str ...string) {
fmt.Println(Warning(str...))
}
7 changes: 3 additions & 4 deletions util/frameworkutil/frameworkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
package frameworkutil

import (
"fmt"

"github.com/urfave/cli/v2"

"github.com/asmaloney/gactar/framework"
"github.com/asmaloney/gactar/framework/ccm_pyactr"
"github.com/asmaloney/gactar/framework/pyactr"
"github.com/asmaloney/gactar/framework/vanilla_actr"
"github.com/asmaloney/gactar/util/chalk"
)

// CreateFrameworks takes a slice of framework names and a context and will
Expand Down Expand Up @@ -38,12 +37,12 @@ func CreateFrameworks(ctx *cli.Context, names []string) (list framework.List) {
fw, err = vanilla_actr.New(ctx)

default:
fmt.Printf("unknown framework: %s\n", f)
chalk.PrintErrStr("unknown framework:", f)
continue
}

if err != nil {
fmt.Println(err.Error())
chalk.PrintErr(err)
continue
}

Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/jwalton/gchalk/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/jwalton/gchalk/.golangci.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/jwalton/gchalk/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/jwalton/gchalk/LICENSE-chalk

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions vendor/github.com/jwalton/gchalk/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading