Skip to content

Commit

Permalink
Hide Style Flags consistently (#457)
Browse files Browse the repository at this point in the history
* refactor: hide style flags on error to not clutter usage

* docs(style): add comment regarding dynamically hiding flags
  • Loading branch information
maaslalani authored Nov 28, 2023
1 parent fb6849c commit 01a6651
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 102 deletions.
8 changes: 0 additions & 8 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -155,12 +153,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 {
Expand Down
9 changes: 0 additions & 9 deletions confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions file/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -141,9 +139,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
}
8 changes: 0 additions & 8 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
7 changes: 0 additions & 7 deletions log/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package log

import (
"github.com/alecthomas/kong"
"github.com/charmbracelet/gum/style"
)

Expand All @@ -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
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -74,7 +75,6 @@ func main() {
if errors.Is(err, exit.ErrAborted) {
os.Exit(exit.StatusAborted)
}
fmt.Println(err)
os.Exit(1)
}
}
8 changes: 0 additions & 8 deletions pager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions spin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
17 changes: 0 additions & 17 deletions style/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"strings"

"github.com/alecthomas/kong"
"github.com/charmbracelet/gum/internal/stdin"
)

Expand All @@ -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
}
}
}
}
21 changes: 21 additions & 0 deletions style/lipgloss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
36 changes: 33 additions & 3 deletions style/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,8 +18,38 @@ 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.
//
// 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"`
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"`
Expand Down
7 changes: 0 additions & 7 deletions table/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions write/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 01a6651

Please sign in to comment.