icli

package module
v0.0.0-...-8333422 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2015 License: MIT Imports: 9 Imported by: 1

README

icli-go

A library for building simple interactive command line program in Go.

The library uses termbox-go.

Welcome to file bugs and suggestions.

Screen Shot

Installation

Install and update this go package with go get -u github.com/lixin9311/icli-go

Examples

Checkout the _demo directory.

Features

  • Word completion with tab.
  • Command history.
  • Command highlight.
  • Stdout is redirected to the screen buffer, you can use fmt.Print safely in your function.

Documentation

Index

Constants

View Source
const (
	// TabLength 4 or 8? That's a question.
	TabLength = 4
	// NumOfLines is the Maximum lines of the stdbuffer.
	NumOfLines = 1000
	// NumOfHistory is the Maximum lines of recored user input.
	NumOfHistory = 1000
)

Variables

View Source
var (
	ExitIcli    = errors.New("Exit the main icli loop.") // Function return this err to exit the mainloop
	CmdNotFound = errors.New("Command not found.")       // Command not found.
	PrintDesc   = errors.New("Print Description.")       // Print description.
)

Errors

Functions

func AddCmd

func AddCmd(cmds []CommandOption)

AddCmd adds cmd to icli, options of the same name will overwrites by the latest one.

func SetPromt

func SetPromt(s string)

SetPromt sets the promt of user input line.

func Start

func Start(errorHandler func(error) error)

Start the main icli loop. It will redirect the stdout.

Types

type CommandLine

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

CommandLine handles the user inputs.

func (*CommandLine) AddCmd

func (cmdline *CommandLine) AddCmd(cmds []CommandOption)

AddCmd a Command Option.

func (*CommandLine) Cell

func (cmdline *CommandLine) Cell() []termbox.Cell

Cell return the CommandLine buffer on the screen.

func (*CommandLine) Cursor

func (cmdline *CommandLine) Cursor() (X, Y int)

Cursor reports the cursor postion.

func (*CommandLine) DeleteRuneBackward

func (cmdline *CommandLine) DeleteRuneBackward()

DeleteRuneBackward handles backspace key.

func (*CommandLine) DeleteRuneForward

func (cmdline *CommandLine) DeleteRuneForward()

DeleteRuneForward handles del key.

func (*CommandLine) DeleteTheRestOfTheLine

func (cmdline *CommandLine) DeleteTheRestOfTheLine()

DeleteTheRestOfTheLine like the name, handles CtrlK.

func (*CommandLine) Draw

func (cmdline *CommandLine) Draw(x, y int)

Draw itcmdline on point(x, y). \t should not exist, but here is a good excise for tab process.

func (*CommandLine) Execute

func (cmdline *CommandLine) Execute() error

Execute the current cmd.

func (*CommandLine) HasCmd

func (cmdline *CommandLine) HasCmd(cmd string) bool

HasCmd checks whether the Command Option exists.

func (*CommandLine) InsertRune

func (cmdline *CommandLine) InsertRune(r rune)

InsertRune to the postion of cursor.

func (*CommandLine) KeyCompletion

func (cmdline *CommandLine) KeyCompletion()

KeyCompletion based on existing Command Options.

func (*CommandLine) MoveCursorOneRuneBackward

func (cmdline *CommandLine) MoveCursorOneRuneBackward()

MoveCursorOneRuneBackward moves the cursor one rune backward.

func (*CommandLine) MoveCursorOneRuneForward

func (cmdline *CommandLine) MoveCursorOneRuneForward()

MoveCursorOneRuneForward moves the cursor one rune forward.

func (*CommandLine) MoveCursorTo

func (cmdline *CommandLine) MoveCursorTo(boffset int)

MoveCursorTo the postion of byte.

func (*CommandLine) MoveCursorToBeginningOfTheLine

func (cmdline *CommandLine) MoveCursorToBeginningOfTheLine()

MoveCursorToBeginningOfTheLine handles home key.

func (*CommandLine) MoveCursorToEndOfTheLine

func (cmdline *CommandLine) MoveCursorToEndOfTheLine()

MoveCursorToEndOfTheLine handles end key.

func (*CommandLine) MoveDown

func (cmdline *CommandLine) MoveDown()

MoveDown handles key arrow down.

func (*CommandLine) MoveUp

func (cmdline *CommandLine) MoveUp()

MoveUp handles key arrow up.

func (*CommandLine) PrintDesc

func (cmdline *CommandLine) PrintDesc()

PrintDesc prints the description of all options.

func (*CommandLine) RuneBeforeCursor

func (cmdline *CommandLine) RuneBeforeCursor() (rune, int)

RuneBeforeCursor returns the rune before the cursor.

func (*CommandLine) RuneUnderCursor

func (cmdline *CommandLine) RuneUnderCursor() (rune, int)

RuneUnderCursor returns the current rune under cursor.

func (*CommandLine) SetPromt

func (cmdline *CommandLine) SetPromt(b []byte)

SetPromt sets the prefix of user command input line.

type CommandOption

type CommandOption struct {
	Command     string
	Description string
	Function    func(args ...string) error
}

CommandOption is an option like "ls" or "cd". Stdout is redirected to screen buffer. Function can call fmt.Print directly

type StdBuffer

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

StdBuffer is a standard buffer to handle stdout or other input to redirect it onto the screen.

func (*StdBuffer) Draw

func (stdbuf *StdBuffer) Draw(x, y, termW, termH int)

Draw the buffer.

func (*StdBuffer) Fd

func (stdbuf *StdBuffer) Fd() *os.File

Fd init and return the file describer for redirct the stdout.

func (*StdBuffer) PutByte

func (stdbuf *StdBuffer) PutByte(b []byte, refresh func())

PutByte converts the byte to termbox.Cell. then calls PutCell

func (*StdBuffer) PutCell

func (stdbuf *StdBuffer) PutCell(c []termbox.Cell, endWithNewline bool, refresh func())

PutCell directly handles the termbox.Cell. Usually PutByte and PutString is enough.

func (*StdBuffer) PutString

func (stdbuf *StdBuffer) PutString(s string, refresh func())

PutString calls PutByte.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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