From 0d5a4cf5595213ea037abaae5c11db05c4fafb8f Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Tue, 28 Nov 2023 11:35:31 -0500 Subject: [PATCH 1/2] refactor: hide style flags on error to not clutter usage --- choose/command.go | 8 -------- confirm/command.go | 9 --------- file/command.go | 8 -------- filter/command.go | 8 -------- input/command.go | 8 -------- log/options.go | 7 ------- main.go | 6 +++--- pager/command.go | 8 -------- spin/command.go | 8 -------- style/command.go | 17 ----------------- style/lipgloss.go | 21 +++++++++++++++++++++ style/options.go | 32 +++++++++++++++++++++++++++++--- table/command.go | 7 ------- write/command.go | 8 -------- 14 files changed, 53 insertions(+), 102 deletions(-) diff --git a/choose/command.go b/choose/command.go index bd7db4129..e65602a13 100644 --- a/choose/command.go +++ b/choose/command.go @@ -7,7 +7,6 @@ import ( "sort" "strings" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/paginator" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -16,7 +15,6 @@ import ( "github.com/charmbracelet/gum/ansi" "github.com/charmbracelet/gum/internal/exit" "github.com/charmbracelet/gum/internal/stdin" - "github.com/charmbracelet/gum/style" ) var ( @@ -150,12 +148,6 @@ func (o Options) Run() error { return nil } -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} - // Check if an array contains a value. func arrayContains(strArray []string, value string) bool { for _, str := range strArray { diff --git a/confirm/command.go b/confirm/command.go index e477922e1..c14ab9e30 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -6,10 +6,7 @@ import ( "github.com/charmbracelet/gum/internal/exit" - "github.com/alecthomas/kong" tea "github.com/charmbracelet/bubbletea" - - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for prompting a user to confirm an @@ -42,9 +39,3 @@ func (o Options) Run() error { return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/file/command.go b/file/command.go index 28b8fc6f4..0d34cab92 100644 --- a/file/command.go +++ b/file/command.go @@ -8,10 +8,8 @@ import ( "github.com/charmbracelet/gum/internal/exit" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/filepicker" tea "github.com/charmbracelet/bubbletea" - "github.com/charmbracelet/gum/style" ) // Run is the interface to picking a file. @@ -72,9 +70,3 @@ func (o Options) Run() error { return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/filter/command.go b/filter/command.go index 4fa6e0ece..6d97d641e 100644 --- a/filter/command.go +++ b/filter/command.go @@ -6,7 +6,6 @@ import ( "os" "strings" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" @@ -17,7 +16,6 @@ import ( "github.com/charmbracelet/gum/internal/exit" "github.com/charmbracelet/gum/internal/files" "github.com/charmbracelet/gum/internal/stdin" - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for filtering through options, powered @@ -132,9 +130,3 @@ func (o Options) checkSelected(m model, isTTY bool) { } } } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/input/command.go b/input/command.go index 8ff1fe0a8..b79acd0e6 100644 --- a/input/command.go +++ b/input/command.go @@ -4,14 +4,12 @@ import ( "fmt" "os" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/cursor" "github.com/charmbracelet/gum/internal/exit" "github.com/charmbracelet/gum/internal/stdin" - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for the text input bubble. @@ -60,9 +58,3 @@ func (o Options) Run() error { fmt.Println(m.textinput.Value()) return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/log/options.go b/log/options.go index ed7776fcf..9a19f3654 100644 --- a/log/options.go +++ b/log/options.go @@ -1,7 +1,6 @@ package log import ( - "github.com/alecthomas/kong" "github.com/charmbracelet/gum/style" ) @@ -25,9 +24,3 @@ type Options struct { ValueStyle style.Styles `embed:"" prefix:"value." help:"The style of the value" envprefix:"GUM_LOG_VALUE_"` SeparatorStyle style.Styles `embed:"" prefix:"separator." help:"The style of the separator" set:"defaultFaint=true" envprefix:"GUM_LOG_SEPARATOR_"` } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/main.go b/main.go index e014adc4b..03de2f58c 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,9 @@ func main() { kong.Description(fmt.Sprintf("A tool for %s shell scripts.", bubbleGumPink.Render("glamorous"))), kong.UsageOnError(), kong.ConfigureHelp(kong.HelpOptions{ - Compact: true, - Summary: false, + Compact: true, + Summary: false, + NoExpandSubcommands: true, }), kong.Vars{ "version": version, @@ -74,7 +75,6 @@ func main() { if errors.Is(err, exit.ErrAborted) { os.Exit(exit.StatusAborted) } - fmt.Println(err) os.Exit(1) } } diff --git a/pager/command.go b/pager/command.go index 69de89e73..5131b96e3 100644 --- a/pager/command.go +++ b/pager/command.go @@ -4,11 +4,9 @@ import ( "fmt" "regexp" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/internal/stdin" - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for the viewport bubble. @@ -50,9 +48,3 @@ func (o Options) Run() error { } return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/spin/command.go b/spin/command.go index 582cfb0ac..5334eaf55 100644 --- a/spin/command.go +++ b/spin/command.go @@ -4,13 +4,11 @@ import ( "fmt" "os" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/mattn/go-isatty" "github.com/charmbracelet/gum/internal/exit" - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for the spinner bubble. @@ -57,9 +55,3 @@ func (o Options) Run() error { os.Exit(m.status) return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/style/command.go b/style/command.go index 5412ec723..850b6cf04 100644 --- a/style/command.go +++ b/style/command.go @@ -10,7 +10,6 @@ import ( "fmt" "strings" - "github.com/alecthomas/kong" "github.com/charmbracelet/gum/internal/stdin" ) @@ -30,19 +29,3 @@ func (o Options) Run() error { fmt.Println(o.Style.ToLipgloss().Render(text)) return nil } - -// HideFlags hides the flags from the usage output. This is used in conjunction -// with BeforeReset hook. -func HideFlags(ctx *kong.Context) { - n := ctx.Selected() - if n == nil { - return - } - for _, f := range n.Flags { - if g := f.Group; g != nil && g.Key == groupName { - if !strings.HasSuffix(f.Name, ".foreground") { - f.Hidden = true - } - } - } -} diff --git a/style/lipgloss.go b/style/lipgloss.go index ef603cc49..9ba52a2f6 100644 --- a/style/lipgloss.go +++ b/style/lipgloss.go @@ -26,3 +26,24 @@ func (s Styles) ToLipgloss() lipgloss.Style { Strikethrough(s.Strikethrough). Underline(s.Underline) } + +// ToLipgloss takes a Styles flag set and returns the corresponding +// lipgloss.Style. +func (s StylesNotHidden) ToLipgloss() lipgloss.Style { + return lipgloss.NewStyle(). + Background(lipgloss.Color(s.Background)). + Foreground(lipgloss.Color(s.Foreground)). + BorderBackground(lipgloss.Color(s.BorderBackground)). + BorderForeground(lipgloss.Color(s.BorderForeground)). + Align(decode.Align[s.Align]). + Border(Border[s.Border]). + Height(s.Height). + Width(s.Width). + Margin(parseMargin(s.Margin)). + Padding(parsePadding(s.Padding)). + Bold(s.Bold). + Faint(s.Faint). + Italic(s.Italic). + Strikethrough(s.Strikethrough). + Underline(s.Underline) +} diff --git a/style/options.go b/style/options.go index 0839ef873..f34e7a0b8 100644 --- a/style/options.go +++ b/style/options.go @@ -6,8 +6,8 @@ const ( // Options is the customization options for the style command. type Options struct { - Text []string `arg:"" optional:"" help:"Text to which to apply the style"` - Style Styles `embed:""` + Text []string `arg:"" optional:"" help:"Text to which to apply the style"` + Style StylesNotHidden `embed:""` } // Styles is a flag set of possible styles. @@ -18,8 +18,34 @@ type Options struct { // components, through embedding and prefixing. type Styles struct { // Colors - Background string `help:"Background Color" default:"${defaultBackground}" group:"Style Flags" env:"BACKGROUND"` Foreground string `help:"Foreground Color" default:"${defaultForeground}" group:"Style Flags" env:"FOREGROUND"` + Background string `help:"Background Color" default:"${defaultBackground}" group:"Style Flags" env:"BACKGROUND" hidden:"true"` + + // Border + Border string `help:"Border Style" enum:"none,hidden,normal,rounded,thick,double" default:"${defaultBorder}" group:"Style Flags" env:"BORDER" hidden:"true"` + BorderBackground string `help:"Border Background Color" group:"Style Flags" default:"${defaultBorderBackground}" env:"BORDER_BACKGROUND" hidden:"true"` + BorderForeground string `help:"Border Foreground Color" group:"Style Flags" default:"${defaultBorderForeground}" env:"BORDER_FOREGROUND" hidden:"true"` + + // Layout + Align string `help:"Text Alignment" enum:"left,center,right,bottom,middle,top" default:"${defaultAlign}" group:"Style Flags" env:"ALIGN" hidden:"true"` + Height int `help:"Text height" default:"${defaultHeight}" group:"Style Flags" env:"HEIGHT" hidden:"true"` + Width int `help:"Text width" default:"${defaultWidth}" group:"Style Flags" env:"WIDTH" hidden:"true"` + Margin string `help:"Text margin" default:"${defaultMargin}" group:"Style Flags" env:"MARGIN" hidden:"true"` + Padding string `help:"Text padding" default:"${defaultPadding}" group:"Style Flags" env:"PADDING" hidden:"true"` + + // Format + Bold bool `help:"Bold text" default:"${defaultBold}" group:"Style Flags" env:"BOLD" hidden:"true"` + Faint bool `help:"Faint text" default:"${defaultFaint}" group:"Style Flags" env:"FAINT" hidden:"true"` + Italic bool `help:"Italicize text" default:"${defaultItalic}" group:"Style Flags" env:"ITALIC" hidden:"true"` + Strikethrough bool `help:"Strikethrough text" default:"${defaultStrikethrough}" group:"Style Flags" env:"STRIKETHROUGH" hidden:"true"` + Underline bool `help:"Underline text" default:"${defaultUnderline}" group:"Style Flags" env:"UNDERLINE" hidden:"true"` +} + +// StylesNotHidden allows the style struct to display full help when not-embedded. +type StylesNotHidden struct { + // Colors + Foreground string `help:"Foreground Color" default:"${defaultForeground}" group:"Style Flags" env:"FOREGROUND"` + Background string `help:"Background Color" default:"${defaultBackground}" group:"Style Flags" env:"BACKGROUND"` // Border Border string `help:"Border Style" enum:"none,hidden,normal,rounded,thick,double" default:"${defaultBorder}" group:"Style Flags" env:"BORDER"` diff --git a/table/command.go b/table/command.go index 8a73a3d39..f39465e8e 100644 --- a/table/command.go +++ b/table/command.go @@ -5,7 +5,6 @@ import ( "fmt" "os" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -131,9 +130,3 @@ func (o Options) Run() error { return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} diff --git a/write/command.go b/write/command.go index 76e46b200..35dccfddd 100644 --- a/write/command.go +++ b/write/command.go @@ -5,14 +5,12 @@ import ( "os" "strings" - "github.com/alecthomas/kong" "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/cursor" "github.com/charmbracelet/gum/internal/exit" "github.com/charmbracelet/gum/internal/stdin" - "github.com/charmbracelet/gum/style" ) // Run provides a shell script interface for the text area bubble. @@ -68,9 +66,3 @@ func (o Options) Run() error { fmt.Println(m.textarea.Value()) return nil } - -// BeforeReset hook. Used to unclutter style flags. -func (o Options) BeforeReset(ctx *kong.Context) error { - style.HideFlags(ctx) - return nil -} From 69bab506b8439a94e45798d3d2b77fa1eb4ab344 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Tue, 28 Nov 2023 14:17:48 -0500 Subject: [PATCH 2/2] docs(style): add comment regarding dynamically hiding flags --- style/options.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/style/options.go b/style/options.go index f34e7a0b8..233e98e74 100644 --- a/style/options.go +++ b/style/options.go @@ -42,6 +42,10 @@ type Styles struct { } // StylesNotHidden allows the style struct to display full help when not-embedded. +// +// NB: We must duplicate this struct to ensure that `gum style` does not hide +// flags when an error pops up. Ideally, we can dynamically hide or show flags +// based on the command run: https://github.com/alecthomas/kong/issues/316 type StylesNotHidden struct { // Colors Foreground string `help:"Foreground Color" default:"${defaultForeground}" group:"Style Flags" env:"FOREGROUND"`