Categorygithub.com/DomBlack/bubble-shell
modulepackage
0.0.0-20230824143140-99472343d062
Repository: https://github.com/domblack/bubble-shell.git
Documentation: pkg.go.dev

# README

Bubble Shell

This library provides you with a interactive shell powered by bubbletea by executing cobra, allowing you to build powerful interactive terminal based shell applications.

Usage

Create a root command as you would normally with cobra, attaching your various commands to it. Then pass it to the shell.New function to create a new shell.

package main

import (
	tea "github.com/charmbracelet/bubbletea"
    "github.com/DomBlack/bubble-shell"
    "github.com/spf13/cobra"
)

func main() {
	// Create the root command which we can attach everything to
    rootCmd := &cobra.Command{}
	
	// Create a command to add to the root command
	myCmd := &cobra.Command{
		Use: "hi",
        RunE: func(cmd *cobra.Command, args []string) error {
			cmd.OutOrStdout().Write([]byte("Hello World\n"))
			return nil
        },
	}
	rootCmd.AddCommand(myCmd)
    
	// Create the shell
	shell := shell.New(rootCmd)
	
	// Run the shell
	p := tea.NewProgram(shell)
	if _, err := p.Run(); err != nil {
		panic(err)
	}
}

In this example, a user will be given an interactive shell with a single command hi which will print Hello World when run.

Options

The shell.New function takes a number of options to configure the shell, which are detailed below. Full documentaton for them can be seen in options.go.

shell.WithHistoryFile / shell.WithNoHistory

By default the shell will save the history of commands to .bubble-shell-history in the user's home directory, however using these two options you can change this behaviour, either providing your own filename or disabling history entirely.

shell.WithKeyMap

You can use this option to customise the key bindings used by the shell. The default key bindings are located in the keymap package.

shell.WithStyles

You can use this option to customise the styles used by the shell. The default styles are located in the styles package.

shell.WithStackTraceFilters / shell.WithAdditionalStackTraceFilters

By default the shell will filter out stack traces from the github.com/DomBlack/bubble-shell package and other related packages when rendering the stack traces of errors to the user. You can use these options to customise this behaviour, either by providing your own list of packages to filter out, or by adding to the default list.

Guidelines for building commands

  1. The shell supports autocompletion of commands and arguments, so ideally implement a ValidArgsFunction function or ValidArgs property on your command.
  2. If you implement your commands using RunE rather than Run you can then return an error to bubble-shell which will be displayed to the user. If the error carries a stack trace, it will be displayed to the user. (I recommend using cockroachdb/errors to create errors with stack traces by default).
  3. If you need a context use cmd.Context() rather than creating your own. This is because the shell will cancel the context when the user presses Ctrl+C.
  4. If you want to display a message to the user, use cmd.OutOrStdout() rather than fmt.Println("Hello World"). While both will be captured and returned to the user, the former will be streamed to the user as it is written, while the os.Stdout and os.Stderr streams are buffered and only displayed when the command completes.

Example Apps

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
WithAdditionalStackTraceFilters adds additional packages to filter from the stack traces of errors when rendered to the user.
WithBaseContext sets the context that commands will be run with when they are executed by users.
WithHistoryFile sets the history file to be used by the shell if not absolute will be relative to $HOME.
WithInlineShell sets the shell to be inline rather than trying to render full screen This means that recovered history will not be shown, however your terminals own render will be incharge of scrolling.
WithKeyMap sets the keymap to be used by the shell.
WithMaxStackFrames sets the maximum number of stack frames to show in errors when rendered to the user.
WithNoHistory disables history for the shell.
WithPromptFunc sets the function for rendering the prompt By default a function will be provided that returns "> ".
WithStackTraceFilters sets the packages to filter from the stack traces of errors when rendered to the user.
WithStyles sets the styles to be used by the shell.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Model is the main shell model.
ShutdownMsg is sent by the shell when it wants to shutdown the application.

# Interfaces

Mode is an interface which represents an input mode the shell can be in and is used to handle input.

# Type aliases

KeyMap is a collection of all the key bindings used by the shell A default is provided and will be used by the shell if no other KeyMap is provided when creating a new shell.
Option is a function that configures the shell.
Styles is the set of styles which will be used to render the shell.