shell

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Command-shell

A go library for creating simple command-based tui's (terminal user interfaces). Built on top of this keyboard package.

Features

  • Default exit command
  • Minimal number of arguments safety
  • Commands can store data
  • Up-arrow for previous command
  • History
  • Default & automatic help command
  • Customizable LinePrefix
  • And more...

TODO

  • Tab autocomplete
  • Cursor moving around line

Example

output
> add 2 6
8
>
code
package main

// imports
import (
    "fmt"
    "strconv"

    shell "github.com/48thFlame/Command-Shell"
)

// addCommand represents the `add` command you can see in the output section
func addCommand(input *shell.CommandInput) error {
    args := input.Args

    // convert arguments to numbers
    // can safely assume there will be at lease 2 arguments
    // we specify this in the `shell.NewCommand` constructor
    a, err := strconv.Atoi(args[0])
    if err != nil {
        return err
    }
    b, err := strconv.Atoi(args[1])
    if err != nil {
        return err
    }

    // output the addition
    fmt.Fprint(input.Stdout, a+b)

    // no errors occurred
    return nil
}

func timesRanCommand(input *shell.CommandInput) error {
    // get number of times ran
    // if doesn't exist the error will be thrown away `_` and val will be the default int -> 0
    val, _ := input.Cmd.Data["times"].(int)

    // increment `times` by 1
    input.Cmd.Data["times"] = val + 1

    // output times ran
    fmt.Fprint(input.Stdout, val)

    // no errors
    return nil
}

func main() {
    s, err := shell.NewShell(
        shell.NewCommand("add", 2, addCommand),
        shell.NewCommand("times", 0, timesRanCommand),
    )
    if err != nil {
        panic(err)
    }

    err = s.Run()
    if err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name         string                 // commands name
	MinNumOfArgs int                    // minium number of arguments needed to run command
	Handler      HandlerType            // the `HandlerType` for this command
	Data         map[string]interface{} // data stored for the command
}

Command type, use

NewCommand()

to create a new Command

func NewCommand

func NewCommand(name string, minNumOfArgs int, handler HandlerType) *Command

NewCommand returns a `Command` type, Args:

name // name used to call command
minNumOfArgs // minimum number of arguments needed for command to run (the `Shell` won't call the command without enough args)
handler // the handler to call for the command

type CommandInput

type CommandInput struct {
	Shell  *Shell    // the `Shell` type
	Stdout io.Writer // write any command output to here, avoid printing to `os.Stdout`
	Args   []string  // command-line arguments
	Cmd    *Command  // the `Command` type
}

CommandInput is the input type for `CommandHandler` function

type HandlerType

type HandlerType func(*CommandInput) error

HandlerType is the type for

Command.Handler

Its a function called when running its command

type Shell

type Shell struct {
	LinePrefix string              // Prefix printed at beginning of every command line
	Path       map[string]*Command // Similar to unix $PATH
	// contains filtered or unexported fields
}

Shell type the main part of the program, use

NewShell()

to create a new Shell

func NewShell

func NewShell(cmds ...*Command) (*Shell, error)

Return a new `Shell` and any `error` that occurred. Takes as input a `[]Command` that the shell should know how to run, Use the `NewCommand` function to create new `Command`s NewShell is the constructor for `Shell` type

func (*Shell) GetHistory

func (s *Shell) GetHistory() []string

GetHistory returns command history similar to Unix history, user can use default `history` command to display history

func (*Shell) Run

func (s *Shell) Run() error

Run runs the `Shell` and return any `error`s, its a blocking function only returns when user exits the `Shell` (CtrlC, etc)

Jump to

Keyboard shortcuts

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