Skip to content

Getting Started

maxlandon edited this page May 29, 2023 · 7 revisions

Creating the application

The following snippet shows how to create a new console instance with the following defaults:

  • A readline shell with a default configuration (Emacs mode).
  • A default -main- menu, with an empty command tree.
  • Bound to this menu, an in-memory history source.
package main

func main() {
    // Instantiate a new app, with a single, default menu.
    // All defaults are set, and nothing is needed to make it work.
    //
    // The call requires an application name (which can be left empty), which will be used 
    // by the underlying readline shell to load per-application options and command binds.
    // See below.
    app := console.New("ApplicationName")
        
    // ... We will include code in here as we go through the documentation...
        
    // And will run the console application.
    app.Start()
}

Global settings

To begin with, we setup a few global options, applying to all menus and contexts:

// Surround command output, asynchronous logs and interrupt handlers with a newline.
app.NewlineBefore = true
app.NewlineAfter = true

// Set the logo function
app.SetPrintLogo(func(_ *console.Console) {
    fmt.Print(`
_____            __ _           _   _              _____                      _      
|  __ \          / _| |         | | (_)            / ____|                    | |     
| |__) |___  ___| |_| | ___  ___| |_ ___   _____  | |     ___  _ __  ___  ___ | | ___ 
|  _  // _ \/ _ \  _| |/ _ \/ __| __| \ \ / / _ \ | |    / _ \| '_ \/ __|/ _ \| |/ _ \
| | \ \  __/  __/ | | |  __/ (__| |_| |\ V /  __/ | |___| (_) | | | \__ \ (_) | |  __/
|_|  \_\___|\___|_| |_|\___|\___|\__|_| \_/ \___|  \_____\___/|_| |_|___/\___/|_|\___|

`)
})

Shell settings

The underlying readline shell used by the console is highly configurable. However, for most intents and purposes, its defaults are powerful enough, and most of the customization is made through user-side inputrc files.

The shell configuration and documentation can be found here.

Of interest for developers using this console application is the fact that the shell inputrc supports that each application using readline can declare itself so that per-application user bindings and options apply, like in the legacy GNU readline library.

Therefore, it is highly recommanded to use a unique name for your console application.

An extract of such per-application settings in a user inputrc file is the following:

# github.com/reeflective/readline Go readline library
$if ApplicationName 
    set autocomplete on
    set usage-hint-always on
    set history-autosuggest off
    set autopairs on
    set prompt-transient off

    # Other per-application command bindings/macros/etc.
$endif

Some more specialized options can be passed to the readline shell if you want to reload the user configuration, although this is very likely not needed:

import "github.com/reeflective/readline/inputrc"

var opts []inputrc.Option

opts = append(opts, inputrc.WithName("/path/to/app_config.inputrc")) // Force a specific configuration file.
opts = append(opts, inputrc.WithMode("xterm-256colors"))             // Force a specific terminal

// Set the shell options and/or reload the configuration.
app.Shell().Opts = opts                // If assigned, those will be used on each user-triggered reload
app.Shell().Keymap.ReloadConfig(opts)  // If only reloaded through this call, will apply only once.

Clone this wiki locally