All Projects → abiosoft → Ishell

abiosoft / Ishell

Licence: mit
Library for creating interactive cli applications.

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Ishell

Np
A better `npm publish`
Stars: ✭ 6,401 (+467.97%)
Mutual labels:  cli-app, cli
Initior
A command line application that let's you initialize your new projects the right way, replaces npm and yarn's init 🎆
Stars: ✭ 17 (-98.49%)
Mutual labels:  cli-app, cli
Sade
Smooth (CLI) Operator 🎶
Stars: ✭ 746 (-33.81%)
Mutual labels:  cli-app, cli
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (-52.09%)
Mutual labels:  cli-app, cli
Libcmdf
Single-header library for writing CLI applications in C/C++
Stars: ✭ 30 (-97.34%)
Mutual labels:  cli, readline
Carbon Now Cli
🎨 Beautiful images of your code — from right inside your terminal.
Stars: ✭ 5,165 (+358.3%)
Mutual labels:  cli-app, cli
Archisteamfarm
C# application with primary purpose of idling Steam cards from multiple accounts simultaneously.
Stars: ✭ 7,219 (+540.55%)
Mutual labels:  cli-app, cli
Dark Mode
Control the macOS dark mode from the command-line
Stars: ✭ 518 (-54.04%)
Mutual labels:  cli-app, cli
Make Space
💾 [WIP] Free up space from the command line
Stars: ✭ 21 (-98.14%)
Mutual labels:  cli-app, cli
Captain
command line python scripts for humans
Stars: ✭ 10 (-99.11%)
Mutual labels:  cli-app, cli
What Anime Cli
❓🖼 Find the anime scene by image using your terminal
Stars: ✭ 533 (-52.71%)
Mutual labels:  cli-app, cli
Crossline
A small, self-contained, zero-config, MIT licensed, cross-platform, readline and libedit replacement.
Stars: ✭ 53 (-95.3%)
Mutual labels:  cli, readline
F License
Open Source License Key Generation and Verification Tool written in Go
Stars: ✭ 535 (-52.53%)
Mutual labels:  cli-app, cli
Tml
🌈💻🎨 A tiny markup language for terminal output. Makes formatting output in CLI apps easier!
Stars: ✭ 634 (-43.74%)
Mutual labels:  cli-app, cli
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (-52.8%)
Mutual labels:  cli-app, cli
Fkill Cli
Fabulously kill processes. Cross-platform.
Stars: ✭ 6,418 (+469.48%)
Mutual labels:  cli-app, cli
Macos Wallpaper
Manage the desktop wallpaper on macOS
Stars: ✭ 450 (-60.07%)
Mutual labels:  cli-app, cli
Rust Sloth
A 3D software rasterizer... for the terminal!
Stars: ✭ 478 (-57.59%)
Mutual labels:  cli-app, cli
Github Spray
Draw on your GitHub contribution graph ░▒▓█
Stars: ✭ 908 (-19.43%)
Mutual labels:  cli-app, cli
Jay
😎 Supercharged JavaScript REPL
Stars: ✭ 970 (-13.93%)
Mutual labels:  cli, readline

ishell

ishell is an interactive shell library for creating interactive cli applications.

Documentation Go Report Card

Older version

The current master is not backward compatible with older version. Kindly change your import path to gopkg.in/abiosoft/ishell.v1.

Older version of this library is still available at https://gopkg.in/abiosoft/ishell.v1.

However, you are advised to upgrade to v2 https://gopkg.in/abiosoft/ishell.v2.

Usage

import "strings"
import "github.com/abiosoft/ishell"

func main(){
    // create new shell.
    // by default, new shell includes 'exit', 'help' and 'clear' commands.
    shell := ishell.New()

    // display welcome info.
    shell.Println("Sample Interactive Shell")

    // register a function for "greet" command.
    shell.AddCmd(&ishell.Cmd{
        Name: "greet",
        Help: "greet user",
        Func: func(c *ishell.Context) {
            c.Println("Hello", strings.Join(c.Args, " "))
        },
    })

    // run shell
    shell.Run()
}

Execution

Sample Interactive Shell
>>> help

Commands:
  clear      clear the screen
  greet      greet user
  exit       exit the program
  help       display help

>>> greet Someone Somewhere
Hello Someone Somewhere
>>> exit
$

Reading input

// simulate an authentication
shell.AddCmd(&ishell.Cmd{
    Name: "login",
    Help: "simulate a login",
    Func: func(c *ishell.Context) {
        // disable the '>>>' for cleaner same line input.
        c.ShowPrompt(false)
        defer c.ShowPrompt(true) // yes, revert after login.

        // get username
        c.Print("Username: ")
        username := c.ReadLine()

        // get password.
        c.Print("Password: ")
        password := c.ReadPassword()

        ... // do something with username and password

        c.Println("Authentication Successful.")
    },
})

Execution

>>> login
Username: someusername
Password:
Authentication Successful.

Multiline input

Builtin support for multiple lines.

>>> This is \
... multi line

>>> Cool that << EOF
... everything here goes
... as a single argument.
... EOF

User defined

shell.AddCmd(&ishell.Cmd{
    Name: "multi",
    Help: "input in multiple lines",
    Func: func(c *ishell.Context) {
        c.Println("Input multiple lines and end with semicolon ';'.")
        lines := c.ReadMultiLines(";")
        c.Println("Done reading. You wrote:")
        c.Println(lines)
    },
})

Execution

>>> multi
Input multiple lines and end with semicolon ';'.
>>> this is user defined
... multiline input;
You wrote:
this is user defined
multiline input;

Keyboard interrupt

Builtin interrupt handler.

>>> ^C
Input Ctrl-C once more to exit
>>> ^C
Interrupted
exit status 1

Custom

shell.Interrupt(func(count int, c *ishell.Context) { ... })

Multiple Choice

func(c *ishell.Context) {
    choice := c.MultiChoice([]string{
        "Golangers",
        "Go programmers",
        "Gophers",
        "Goers",
    }, "What are Go programmers called ?")
    if choice == 2 {
        c.Println("You got it!")
    } else {
        c.Println("Sorry, you're wrong.")
    }
},

Output

What are Go programmers called ?
  Golangers
  Go programmers
> Gophers
  Goers

You got it!

Checklist

func(c *ishell.Context) {
    languages := []string{"Python", "Go", "Haskell", "Rust"}
    choices := c.Checklist(languages,
        "What are your favourite programming languages ?", nil)
    out := func() []string { ... } // convert index to language
    c.Println("Your choices are", strings.Join(out(), ", "))
}

Output

What are your favourite programming languages ?
    Python
  ✓ Go
    Haskell
 >✓ Rust

Your choices are Go, Rust

Progress Bar

Determinate

func(c *ishell.Context) {
    c.ProgressBar().Start()
    for i := 0; i < 101; i++ {
        c.ProgressBar().Suffix(fmt.Sprint(" ", i, "%"))
        c.ProgressBar().Progress(i)
        ... // some background computation
    }
    c.ProgressBar().Stop()
}

Output

[==========>         ] 50%

Indeterminate


func(c *ishell.Context) {
    c.ProgressBar().Indeterminate(true)
    c.ProgressBar().Start()
    ... // some background computation
    c.ProgressBar().Stop()
}

Output

[ ====               ]

Custom display using briandowns/spinner.

display := ishell.ProgressDisplayCharSet(spinner.CharSets[11])
func(c *Context) { c.ProgressBar().Display(display) ... }

// or set it globally
ishell.ProgressBar().Display(display)

Durable history

// Read and write history to $HOME/.ishell_history
shell.SetHomeHistoryPath(".ishell_history")

Non-interactive execution

In some situations it is desired to exit the program directly after executing a single command.

// when started with "exit" as first argument, assume non-interactive execution
if len(os.Args) > 1 && os.Args[1] == "exit" {
    shell.Process(os.Args[2:]...)
} else {
    // start shell
    shell.Run()
}
# Run normally - interactive mode:
$ go run main.go
>>> |

# Run non-interactivelly
$ go run main.go exit greet Someusername
Hello Someusername

Output with Color

You can use fatih/color.

func(c *ishell.Context) {
    yellow := color.New(color.FgYellow).SprintFunc()
    c.Println(yellow("This line is yellow"))
}

Execution

>>> color
This line is yellow

Example

Available here.

go run example/main.go

Supported Platforms

  • [x] Linux
  • [x] OSX
  • [x] Windows [Not tested but should work]

Note

ishell is in active development and can still change significantly.

Roadmap (in no particular order)

  • [x] Multiline inputs
  • [x] Command history
  • [x] Customizable tab completion
  • [x] Handle ^C interrupts
  • [x] Subcommands and help texts
  • [x] Scrollable paged output
  • [x] Progress bar
  • [x] Multiple choice prompt
  • [x] Checklist prompt
  • [x] Support for command aliases
  • [ ] Multiple line progress bars
  • [ ] Testing, testing, testing

Contribution

  1. Create an issue to discuss it.
  2. Send in Pull Request.

License

MIT

Credits

Library Use
github.com/flynn-archive/go-shlex splitting input into command and args.
github.com/chzyer/readline readline capabilities.

Donate

bitcoin: 1GTHYEDiy2C7RzXn5nY4wVRaEN2GvLjwZN
paypal: [email protected]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].