Skip to content

Commit be74596

Browse files
authored
feat: add colored output for console logs (#16)
1 parent efb7d34 commit be74596

5 files changed

Lines changed: 75 additions & 8 deletions

File tree

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ go 1.26.0
55
require github.com/spf13/cobra v1.10.2
66

77
require (
8+
github.com/fatih/color v1.18.0 // indirect
89
github.com/inconshreveable/mousetrap v1.1.0 // indirect
10+
github.com/mattn/go-colorable v0.1.13 // indirect
11+
github.com/mattn/go-isatty v0.0.20 // indirect
912
github.com/spf13/pflag v1.0.9 // indirect
13+
golang.org/x/sys v0.25.0 // indirect
1014
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
3+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
24
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
35
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
7+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
8+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
9+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
10+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
411
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
512
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
613
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
714
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
815
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
916
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
17+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
20+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1021
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/reporter/reporter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ package reporter
33
import (
44
"fmt"
55
"io"
6+
7+
"github.com/space-code/linkctl/pkg/iostreams"
68
)
79

810
func PrintBanner(w io.Writer) {
911
fmt.Println(w, "Debugger")
1012
}
1113

12-
func PrintDeviceList(w io.Writer, platform string, devices []string) {
14+
func PrintDeviceList(w io.Writer, cs *iostreams.ColorScheme, platform string, devices []string) {
1315
if len(devices) == 0 {
14-
fmt.Fprintf(w, " No %s devices found (booted / connected)\n\n", platform)
16+
fmt.Fprintf(w, " %s No %s devices found (booted / connected)\n\n", "⚠️", platform)
1517
return
1618
}
1719

18-
fmt.Fprintf(w, "%s\n", platform)
19-
20+
fmt.Fprintf(w, "%s\n", cs.Bold(platform))
2021
for _, d := range devices {
21-
fmt.Fprintf(w, "%s %s\n", "*", d)
22+
fmt.Fprintf(w, "%s %s\n", cs.Muted("•"), d)
2223
}
2324
fmt.Fprintln(w)
2425
}

pkg/cmd/devices/devices.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func run(f *cmdutil.Factory, opts *options) error {
5454
}
5555

5656
w := f.IOStreams.Out
57-
reporter.PrintDeviceList(w, "iOS", iosDevices)
57+
cs := f.IOStreams.ColorScheme()
58+
reporter.PrintDeviceList(w, cs, "iOS", iosDevices)
5859
return nil
5960
}

pkg/iostreams/iostreams.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"io"
66
"os"
7+
8+
"github.com/fatih/color"
79
)
810

911
type fileWriter interface {
@@ -20,6 +22,8 @@ type IOStreams struct {
2022
In fileReader
2123
Out fileWriter
2224
ErrOut fileWriter
25+
26+
colorEnabled bool
2327
}
2428

2529
func System() *IOStreams {
@@ -29,6 +33,8 @@ func System() *IOStreams {
2933
ErrOut: os.Stderr,
3034
}
3135

36+
ios.colorEnabled = !color.NoColor
37+
3238
return ios
3339
}
3440

@@ -42,13 +48,57 @@ func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
4248
fd: 0,
4349
ReadCloser: io.NopCloser(in),
4450
},
45-
Out: &fdWriter{fd: 1, Writer: out},
46-
ErrOut: &fdWriter{fd: 2, Writer: errOut},
51+
Out: &fdWriter{fd: 1, Writer: out},
52+
ErrOut: &fdWriter{fd: 2, Writer: errOut},
53+
colorEnabled: false,
4754
}
4855

4956
return io, in, out, errOut
5057
}
5158

59+
func (ios *IOStreams) ColorEnabled() bool {
60+
return ios.colorEnabled
61+
}
62+
63+
type ColorScheme struct {
64+
enabled bool
65+
}
66+
67+
func (ios *IOStreams) ColorScheme() *ColorScheme {
68+
return &ColorScheme{enabled: ios.colorEnabled}
69+
}
70+
71+
func (cs *ColorScheme) format(c *color.Color, s string) string {
72+
if !cs.enabled {
73+
return s
74+
}
75+
return c.Sprint(s)
76+
}
77+
78+
func (cs *ColorScheme) Bold(s string) string {
79+
return cs.format(color.New(color.Bold), s)
80+
}
81+
82+
func (cs *ColorScheme) Green(s string) string {
83+
return cs.format(color.New(color.FgGreen), s)
84+
}
85+
86+
func (cs *ColorScheme) Cyan(s string) string {
87+
return cs.format(color.New(color.FgCyan), s)
88+
}
89+
90+
func (cs *ColorScheme) Yellow(s string) string {
91+
return cs.format(color.New(color.FgYellow), s)
92+
}
93+
94+
func (cs *ColorScheme) Red(s string) string {
95+
return cs.format(color.New(color.FgRed), s)
96+
}
97+
98+
func (cs *ColorScheme) Muted(s string) string {
99+
return cs.format(color.New(color.FgHiBlack), s)
100+
}
101+
52102
type fdWriter struct {
53103
io.Writer
54104
fd uintptr

0 commit comments

Comments
 (0)