Skip to content

Commit 20b269c

Browse files
committed
feat: add log command
1 parent 46328de commit 20b269c

File tree

6 files changed

+204
-11
lines changed

6 files changed

+204
-11
lines changed

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module github.com/charmbracelet/gum
22

3-
go 1.18
3+
go 1.19
44

55
require (
66
github.com/alecthomas/kong v0.8.1
77
github.com/alecthomas/mango-kong v0.1.0
88
github.com/charmbracelet/bubbles v0.16.1
99
github.com/charmbracelet/bubbletea v0.24.2
1010
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d
11-
github.com/charmbracelet/lipgloss v0.9.0
11+
github.com/charmbracelet/lipgloss v0.9.1
12+
github.com/charmbracelet/log v0.3.0
1213
github.com/mattn/go-isatty v0.0.20
1314
github.com/muesli/reflow v0.3.0
1415
github.com/muesli/roff v0.1.0
@@ -24,6 +25,7 @@ require (
2425
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2526
github.com/dlclark/regexp2 v1.8.1 // indirect
2627
github.com/dustin/go-humanize v1.0.1 // indirect
28+
github.com/go-logfmt/logfmt v0.6.0 // indirect
2729
github.com/gorilla/css v1.0.0 // indirect
2830
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2931
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -36,10 +38,10 @@ require (
3638
github.com/rivo/uniseg v0.4.4 // indirect
3739
github.com/yuin/goldmark v1.5.4 // indirect
3840
github.com/yuin/goldmark-emoji v1.0.1 // indirect
39-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
41+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
4042
golang.org/x/net v0.8.0 // indirect
4143
golang.org/x/sync v0.1.0 // indirect
42-
golang.org/x/sys v0.12.0 // indirect
44+
golang.org/x/sys v0.13.0 // indirect
4345
golang.org/x/term v0.6.0 // indirect
4446
golang.org/x/text v0.8.0 // indirect
4547
)

go.sum

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06
1818
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
1919
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d h1:S4Ejl/M2VrryIgDrDbiuvkwMUDa67/t/H3Wz3i2/vUw=
2020
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d/go.mod h1:swCB3CXFsh22H1ESDYdY1tirLiNqCziulDyJ1B6Nt7Q=
21-
github.com/charmbracelet/lipgloss v0.9.0 h1:BHIM7U4vX77xGEld8GrTKspBMtSv7j0wxPCH73nrdxE=
22-
github.com/charmbracelet/lipgloss v0.9.0/go.mod h1:h8KDyaivONasw1Bhb4nWiKlk4P1wHPly+3+3v6EFMmA=
21+
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
22+
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
23+
github.com/charmbracelet/log v0.3.0 h1:u5aB2KJDgNZo4WOfOC8C+KvGIkJ2rCFNlPWDu6xhnqI=
24+
github.com/charmbracelet/log v0.3.0/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g=
2325
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
2426
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
27+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2528
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
2629
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
2730
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
2831
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
32+
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
33+
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
2934
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
3035
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
3136
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
@@ -56,28 +61,31 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
5661
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
5762
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
5863
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
64+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5965
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
6066
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
6167
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
6268
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
6369
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
6470
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
71+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6572
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
6673
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
6774
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
6875
github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os=
6976
github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGjRIBbIZQFqkQ=
70-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
71-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
77+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
78+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
7279
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
7380
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
7481
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
7582
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7683
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7784
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
78-
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
79-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
85+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
86+
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8087
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
8188
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
8289
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
8390
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
91+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

gum.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/charmbracelet/gum/format"
1212
"github.com/charmbracelet/gum/input"
1313
"github.com/charmbracelet/gum/join"
14+
"github.com/charmbracelet/gum/log"
1415
"github.com/charmbracelet/gum/man"
1516
"github.com/charmbracelet/gum/pager"
1617
"github.com/charmbracelet/gum/spin"
@@ -204,4 +205,13 @@ type Gum struct {
204205
// $ gum write > output.text
205206
//
206207
Write write.Options `cmd:"" help:"Prompt for long-form text"`
208+
209+
// Log provides a shell script interface for logging using Log.
210+
// https://github.com/charmbracelet/log
211+
//
212+
// It can be used to log messages to output.
213+
//
214+
// $ gum log --level info "Hello, world!"
215+
//
216+
Log log.Options `cmd:"" help:"Log messages to output"`
207217
}

log/command.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package log
2+
3+
import (
4+
"math"
5+
"os"
6+
"strings"
7+
8+
"github.com/charmbracelet/log"
9+
)
10+
11+
// Run is the command-line interface for logging text.
12+
func (o Options) Run() error {
13+
l := log.New(os.Stderr)
14+
15+
l.SetPrefix(o.Prefix)
16+
l.SetLevel(-math.MaxInt32) // log all levels
17+
l.SetReportTimestamp(o.Time)
18+
l.SetTimeFormat(o.TimeFormat)
19+
20+
st := log.DefaultStyles()
21+
st.Levels[log.DebugLevel] = o.LevelDebugStyle.ToLipgloss().
22+
Inline(true).
23+
SetString(strings.ToUpper(log.DebugLevel.String()))
24+
st.Levels[log.InfoLevel] = o.LevelInfoStyle.ToLipgloss().
25+
Inline(true).
26+
SetString(strings.ToUpper(log.InfoLevel.String()))
27+
st.Levels[log.WarnLevel] = o.LevelWarnStyle.ToLipgloss().
28+
Inline(true).
29+
SetString(strings.ToUpper(log.WarnLevel.String()))
30+
st.Levels[log.ErrorLevel] = o.LevelErrorStyle.ToLipgloss().
31+
Inline(true).
32+
SetString(strings.ToUpper(log.ErrorLevel.String()))
33+
st.Levels[log.FatalLevel] = o.LevelFatalStyle.ToLipgloss().
34+
Inline(true).
35+
SetString(strings.ToUpper(log.FatalLevel.String()))
36+
st.Timestamp = o.TimeStyle.ToLipgloss().
37+
Inline(true)
38+
st.Prefix = o.PrefixStyle.ToLipgloss().
39+
Inline(true)
40+
st.Message = o.MessageStyle.ToLipgloss().
41+
Inline(true)
42+
st.Key = o.KeyStyle.ToLipgloss().
43+
Inline(true)
44+
st.Value = o.ValueStyle.ToLipgloss().
45+
Inline(true)
46+
st.Separator = o.SeparatorStyle.ToLipgloss().
47+
Inline(true)
48+
49+
l.SetStyles(st)
50+
51+
if o.File != "" {
52+
f, err := os.OpenFile(o.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
53+
if err != nil {
54+
return err
55+
}
56+
57+
defer f.Close() // nolint: errcheck
58+
l.SetOutput(f)
59+
}
60+
61+
switch o.Formatter {
62+
case "json":
63+
l.SetFormatter(log.JSONFormatter)
64+
case "logfmt":
65+
l.SetFormatter(log.LogfmtFormatter)
66+
case "text":
67+
l.SetFormatter(log.TextFormatter)
68+
}
69+
70+
var arg0 string
71+
var args []interface{}
72+
if len(o.Text) > 0 {
73+
arg0 = o.Text[0]
74+
}
75+
76+
if len(o.Text) > 1 {
77+
args = make([]interface{}, len(o.Text[1:]))
78+
for i, arg := range o.Text[1:] {
79+
args[i] = arg
80+
}
81+
}
82+
83+
switch o.Level {
84+
case "none":
85+
if o.Format {
86+
l.Printf(arg0, args...)
87+
} else if o.Structured {
88+
l.Print(arg0, args...)
89+
} else {
90+
l.Print(strings.Join(o.Text, " "))
91+
}
92+
case "debug":
93+
if o.Format {
94+
l.Debugf(arg0, args...)
95+
} else if o.Structured {
96+
l.Debug(arg0, args...)
97+
} else {
98+
l.Debug(strings.Join(o.Text, " "))
99+
}
100+
case "info":
101+
if o.Format {
102+
l.Infof(arg0, args...)
103+
} else if o.Structured {
104+
l.Info(arg0, args...)
105+
} else {
106+
l.Info(strings.Join(o.Text, " "))
107+
}
108+
case "warn":
109+
if o.Format {
110+
l.Warnf(arg0, args...)
111+
} else if o.Structured {
112+
l.Warn(arg0, args...)
113+
} else {
114+
l.Warn(strings.Join(o.Text, " "))
115+
}
116+
case "error":
117+
if o.Format {
118+
l.Errorf(arg0, args...)
119+
} else if o.Structured {
120+
l.Error(arg0, args...)
121+
} else {
122+
l.Error(strings.Join(o.Text, " "))
123+
}
124+
case "fatal":
125+
if o.Format {
126+
l.Fatalf(arg0, args...)
127+
} else if o.Structured {
128+
l.Fatal(arg0, args...)
129+
} else {
130+
l.Fatal(strings.Join(o.Text, " "))
131+
}
132+
}
133+
134+
return nil
135+
}

log/options.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package log
2+
3+
import (
4+
"github.com/alecthomas/kong"
5+
"github.com/charmbracelet/gum/style"
6+
)
7+
8+
// Options is the set of options that can configure a join.
9+
type Options struct {
10+
Text []string `arg:"" help:"Text to log"`
11+
12+
File string `short:"o" help:"Log to file"`
13+
Format bool `short:"f" help:"Format message using printf" xor:"format,structured"`
14+
Formatter string `help:"The log formatter to use" enum:"json,logfmt,text" default:"text"`
15+
Level string `help:"The log level to use" enum:"none,debug,info,warn,error,fatal" default:"none"`
16+
Prefix string `help:"Prefix to print before the message"`
17+
Structured bool `short:"s" help:"Use structured logging" xor:"format,structured"`
18+
Time bool `help:"Whether to print the time"`
19+
TimeFormat string `help:"The time format to use" default:"2006/01/02 15:04:05"`
20+
21+
LevelDebugStyle style.Styles `embed:"" prefix:"level.debug." help:"The style of the debug level" set:"defaultBold=true" set:"defaultForeground=63" envprefix:"GUM_LOG_LEVEL_DEBUG_"` //nolint:staticcheck
22+
LevelInfoStyle style.Styles `embed:"" prefix:"level.info." help:"The style of the info level" set:"defaultBold=true" set:"defaultForeground=83" envprefix:"GUM_LOG_LEVEL_INFO_"` //nolint:staticcheck
23+
LevelWarnStyle style.Styles `embed:"" prefix:"level.warn." help:"The style of the warn level" set:"defaultBold=true" set:"defaultForeground=192" envprefix:"GUM_LOG_LEVEL_WARN_"` //nolint:staticcheck
24+
LevelErrorStyle style.Styles `embed:"" prefix:"level.error." help:"The style of the error level" set:"defaultBold=true" set:"defaultForeground=204" envprefix:"GUM_LOG_LEVEL_ERROR_"` //nolint:staticcheck
25+
LevelFatalStyle style.Styles `embed:"" prefix:"level.fatal." help:"The style of the fatal level" set:"defaultBold=true" set:"defaultForeground=134" envprefix:"GUM_LOG_LEVEL_FATAL_"` //nolint:staticcheck
26+
TimeStyle style.Styles `embed:"" prefix:"time." help:"The style of the time" envprefix:"GUM_LOG_TIME_"`
27+
PrefixStyle style.Styles `embed:"" prefix:"prefix." help:"The style of the prefix" set:"defaultBold=true" set:"defaultFaint=true" envprefix:"GUM_LOG_PREFIX_"` //nolint:staticcheck
28+
MessageStyle style.Styles `embed:"" prefix:"message." help:"The style of the message" envprefix:"GUM_LOG_MESSAGE_"`
29+
KeyStyle style.Styles `embed:"" prefix:"key." help:"The style of the key" set:"defaultFaint=true" envprefix:"GUM_LOG_KEY_"`
30+
ValueStyle style.Styles `embed:"" prefix:"value." help:"The style of the value" envprefix:"GUM_LOG_VALUE_"`
31+
SeparatorStyle style.Styles `embed:"" prefix:"separator." help:"The style of the separator" set:"defaultFaint=true" envprefix:"GUM_LOG_SEPARATOR_"`
32+
}
33+
34+
// BeforeReset hook. Used to unclutter style flags.
35+
func (o Options) BeforeReset(ctx *kong.Context) error {
36+
style.HideFlags(ctx)
37+
return nil
38+
}

table/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (o Options) Run() error {
8787
}
8888

8989
if o.Print {
90-
headers := make([]any, 0, len(columnNames))
90+
headers := make([]string, 0, len(columnNames))
9191
for _, name := range columnNames {
9292
headers = append(headers, name)
9393
}

0 commit comments

Comments
 (0)