boa

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 12 Imported by: 0

README

Boa

Go Reference Go Report Card

Boa implements Cobra's help and usage functions to provided an interactive user experience by leveraging Bubbletea. User's no longer need to spend time running multiple help commands to see how nested sub commands work!

Install

Use go get to install the latest version of the library.

go get -u github.com/elewis787/boa@latest

Next, include Boa in your application:

import "github.com/elewis787/boa"

Usage

Using Boa is very simple. Below is an example on how to set the help/usage functions on a root command defined using Cobra.

	rootCmd := &cobra.Command{
		Version: "v0.0.1",
		Use:     "Example",
		Long:    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
		Short:   "example command",
		Example: "example [sub command]",
		RunE: func(cmd *cobra.Command, args []string) error {
		    return nil
		},
	}

	rootCmd.SetUsageFunc(boa.UsageFunc)
	rootCmd.SetHelpFunc(boa.HelpFunc)

The key lines are:

	rootCmd.SetUsageFunc(boa.UsageFunc)
	rootCmd.SetHelpFunc(boa.HelpFunc)

To futher customize the look and feel of boa, you can optional set additional styles or extend the defaults

	styles := boa.DefaultStyles()
	styles.Title.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
	styles.Border.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
	styles.SelectedItem.Foreground(lipgloss.AdaptiveColor{Light: `#353C3B`, Dark: `#353C3B`}).
		Background(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})

	b := boa.New(boa.WithStyles(styles))

	rootCmd.SetUsageFunc(b.UsageFunc)
	rootCmd.SetHelpFunc(b.HelpFunc)

Use the documentation to see a full list of styles as well as other available options that can be set.

Demo

demo

Used by

Future work

  • Eval how styles are exported. Goal is to make it easy to customize the layout without needing to build a cmd parser for cobra
  • Add back button
  • Option to execute sub command
  • Adjust help based on cmd context availability
  • Other ideas ? - Open a feature request or submit a PR !

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorWriter = &bytes.Buffer{}

Functions

func HelpFunc

func HelpFunc(cmd *cobra.Command, s []string)

HelpFunc puts out the help for the command. Used when a user calls help [command]. Set by calling Cobra's SetHelpFunc

func UsageFunc

func UsageFunc(cmd *cobra.Command) error

UsageFunc puts out the usage for the command. Used when a user provides invalid input. Set by calling Cobra's SetUsageFunc

Types

type Boa

type Boa struct {
	// contains filtered or unexported fields
}

func New

func New(options ...Options) *Boa

Create a new instance of Boa with custom Options

func (*Boa) HelpFunc

func (b *Boa) HelpFunc(cmd *cobra.Command, s []string)

func (*Boa) UsageFunc

func (b *Boa) UsageFunc(cmd *cobra.Command) error

type Options

type Options interface {
	// contains filtered or unexported methods
}

func WithAltScreen

func WithAltScreen(b bool) Options

func WithErrWriter

func WithErrWriter(b *bytes.Buffer) Options

func WithStyles

func WithStyles(styles *Styles) Options

type Styles

type Styles struct {
	Border       lipgloss.Style
	Title        lipgloss.Style
	SubTitle     lipgloss.Style
	Section      lipgloss.Style
	Text         lipgloss.Style
	ErrorText    lipgloss.Style
	SelectedItem lipgloss.Style
	Item         lipgloss.Style
	Info         lipgloss.Style
	CmdPrint     lipgloss.Style
}

func DefaultStyles

func DefaultStyles() *Styles

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL