ns

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 19 Imported by: 2

README

nilshell

Command shell for golang which provides a minimal line editor and command processing loop. Here's what you get with NilShell:

  • Line editor (type, insert, delete)
  • Command history (up/down to navigate, load/export)
  • Reverse search (simple pattern match, most recent history first)
  • Tab completion hook
  • Handling of terminal resize

What it doesn't do

  • Any sort of argument parsing / tokenization

For a full CLI parser implementation using nilshell, check out Commander

Usage

import (
    ns "github.com/hashibuto/nilshell"
)

config := ns.ReaderConfig{

    CompletionFunction: func(beforeCursor string, afterCursor string, full string) *ns.Suggestions {
        // This is where you would return tab completion suggestions based on the input before the cursor, perhaps after the
        // cursor, or even the entire line buffer.

        return &ns.Suggestions{
            Total: 0
            Items: []*ns.Suggestion{}
        }
    },

    ProcessFunction: func(text string) error {
        // text contains the command to be processed by your own command interpreter
        return nil
    }

    // implement your own history manager if you want to persist history across multiple invocations, or use the default (nil)
	HistoryManager: nil,

    PromptFunction: func() string {
        // Return a prompt with terminal escape chars to add style

        return "$ "
    },

    Debug: false,

    // enable the log file to dump debugging info to a tailable log file
    LogFile: "",
}

r := NewReader(config)

// block until the process captures SIGINT or SIGTERM
r.ReadLoop()

Documentation

Index

Constants

View Source
const (
	KEY_CTRL_C      = "\x03" // Signal interrupt
	KEY_CTRL_D      = "\x04" // Signal EOF
	KEY_CTRL_L      = "\x0C" // Clear terminal
	KEY_TAB         = "\x09"
	KEY_ENTER       = "\x0D"
	KEY_CTRL_R      = "\x12" // Search backward
	KEY_CTRL_T      = "\x14"
	KEY_ESCAPE      = "\x1B"
	KEY_BACKSPACE   = "\x7F"
	KEY_DEL         = "\x1B[3~"
	KEY_END         = "\x1B[F"
	KEY_HOME        = "\x1B[H"
	KEY_UP_ARROW    = "\x1B[A"
	KEY_DOWN_ARROW  = "\x1B[B"
	KEY_RIGHT_ARROW = "\x1B[C"
	KEY_LEFT_ARROW  = "\x1B[D"
)

Variables

View Source
var (
	ErrInterrupt = errors.New("interrupt")
	ErrEof       = errors.New("eof")
)

Functions

func CalculateColumnWidth added in v0.2.1

func CalculateColumnWidth(allText []string, screenWidth int, minColumns int, gutterWidth int) (int, int)

CalculateColumnWidth returns the column width and number of columns per row

Types

type BasicHistoryIterator added in v1.0.0

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

func (*BasicHistoryIterator) Backward added in v1.0.0

func (bhi *BasicHistoryIterator) Backward() string

func (*BasicHistoryIterator) Forward added in v1.0.0

func (bhi *BasicHistoryIterator) Forward() string

type BasicHistoryManager added in v1.0.0

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

func NewBasicHistoryManager added in v1.0.0

func NewBasicHistoryManager(maxKeep int) *BasicHistoryManager

func (*BasicHistoryManager) Exit added in v1.0.0

func (h *BasicHistoryManager) Exit()

func (*BasicHistoryManager) GetIterator added in v1.0.0

func (h *BasicHistoryManager) GetIterator() HistoryIterator

func (*BasicHistoryManager) Push added in v1.0.0

func (h *BasicHistoryManager) Push(value string)

func (*BasicHistoryManager) Search added in v1.0.0

func (h *BasicHistoryManager) Search(pattern string) []string

type CompletionFunc added in v1.0.0

type CompletionFunc func(beforeCursor string, afterCursor string, full string) *Suggestions

type FileLock added in v1.0.0

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

func NewFileLock added in v1.0.0

func NewFileLock(filename string) *FileLock

func (*FileLock) Lock added in v1.0.0

func (fl *FileLock) Lock() error

func (*FileLock) Unlock added in v1.0.0

func (fl *FileLock) Unlock() error

type HistoryIterator added in v1.0.0

type HistoryIterator interface {
	Backward() string
	Forward() string
}

type HistoryManager added in v1.0.0

type HistoryManager interface {
	Exit()
	GetIterator() HistoryIterator
	Push(string)
	Search(string) []string
}

type PersistedHistoryManager added in v1.0.0

type PersistedHistoryManager struct {
	*BasicHistoryManager
	// contains filtered or unexported fields
}

func NewPersistedHistoryManager added in v1.0.0

func NewPersistedHistoryManager(maxKeep int, filename string) *PersistedHistoryManager

func (*PersistedHistoryManager) Exit added in v1.0.0

func (pm *PersistedHistoryManager) Exit()

func (*PersistedHistoryManager) Push added in v1.0.0

func (pm *PersistedHistoryManager) Push(value string)

type Position added in v1.0.0

type Position struct {
	Row    int
	Column int
}

type Reader added in v1.0.0

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

func NewReader added in v1.0.0

func NewReader(config ReaderConfig) *Reader

func (*Reader) GetWindowSize added in v1.0.0

func (r *Reader) GetWindowSize() *Size

func (*Reader) MoveCursorToRenderEnd added in v1.0.0

func (r *Reader) MoveCursorToRenderEnd(renderLength int)

func (*Reader) MoveCursorToRenderStart added in v1.0.0

func (r *Reader) MoveCursorToRenderStart()

func (*Reader) ReadLoop added in v1.0.0

func (r *Reader) ReadLoop() error

ReadLoop reads commands from the standard input and blocks until exit

func (*Reader) SetEditCursorPosition added in v1.0.0

func (r *Reader) SetEditCursorPosition(prompt string, offset ...int)

type ReaderConfig added in v1.0.0

type ReaderConfig struct {
	CompletionFunction CompletionFunc
	ProcessFunction    func(string) error
	HistoryManager     HistoryManager
	PromptFunction     func() string
	Debug              bool
	LogFile            string
}

type Size added in v1.0.0

type Size struct {
	Rows    int
	Columns int
}

type Suggestion added in v1.0.0

type Suggestion struct {
	Display string
	Value   string
}

func NewSuggestion added in v1.0.1

func NewSuggestion(display string, value string) *Suggestion

type Suggestions added in v1.0.0

type Suggestions struct {
	Total int // reflects the total (could be longer than len(.Items)
	Items []*Suggestion
}

func NewSuggestions added in v1.0.1

func NewSuggestions() *Suggestions

func (*Suggestions) Add added in v1.0.1

func (s *Suggestions) Add(suggestion *Suggestion)

Add will always reset the total after the suggestion is added, thus it is necessary to set the total after, if it is different than the length of suggestions

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳