interactive

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

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

Go to latest
Published: Jul 14, 2017 License: MIT Imports: 5 Imported by: 0

README

lukasmalkmus/interactive

Simple and easy interactive commandline applications. - by Lukas Malkmus

Travis Status Coverage Status Go Report GoDoc Latest Release License


Table of Contents

  1. Introduction
  2. Usage
  3. Contributing
  4. License
Introduction

This package is build on top of the excellent golang.org/x/crypto/ssh/terminal package and tries to simplify the creation of small and simple applications which run in shell mode. It isn't very powerful (yet) but has enough features for basic usage. For example it shuts down gracefully on Ctrl^C and Ctrl^D.

Todo
  • Tests!
Usage
Installation

Please use a dependency manager like glide to make sure you use a tagged release.

Install using go get:

go get -u github.com/lukasmalkmus/interactive
Minimum setup

A more complete example can be found here.

This small application echos the entered text:

s := interactive.New(">")
s.Action = func(c *interactive.Context) error {
    text, _ := c.Scan()
    c.Println(text)
    return nil
}
s.Run()

Returning an error instead of nil will print the error. Calling context.Close() will close the session with the specified exit code.

Contributing

Feel free to submit PRs or to fill Issues. Every kind of help is appreciated.

License

© Lukas Malkmus, 2017

Distributed under MIT License (The MIT License).

See LICENSE for more information.

Documentation

Overview

Package interactive provides an easy to implement shell for simple, interactive commandline applications. It is build on top of the excellent https://golang.org/x/crypto/ssh/terminal package and tries to simplify the creation of small and simple applications which run in shell mode. It isn't very powerful (yet) but has enough features for basic usage. For example it shuts down gracefully on Ctrl^C and Ctrl^D.

This small application echos the entered text:

s := interactive.New(">")
s.Action = func(c *interactive.Context) error {
    text, _ := c.Scan()
    c.Println(text)
    return nil
}
s.Run()

Returning an error instead of nil will print the error. Calling context.Close() will close the session with the specified exit code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(*Context) error

An Action is implemented by the package user and used by the session.

type Context

type Context struct {
	*Session
}

A Context is bound to a session and provides methods to interact with it. It hasn't anything in common with the official context package/type.

func (*Context) Close

func (c *Context) Close(exitCode int)

Close will close the session with the specified exit code and restore the terminal into the previous state.

func (*Context) Print

func (c *Context) Print(a ...interface{})

Print formats using the default formats for its operands and writes to the sessions output. Spaces are added between operands when neither is a string. See fmt.Sprint and fmt.Print for more information.

func (*Context) Printf

func (c *Context) Printf(format string, a ...interface{})

Printf formats according to a format specifier and writes to the sessions output. See fmt.Sprintf and fmt.Printf for more information.

func (*Context) Println

func (c *Context) Println(a ...interface{})

Println formats using the default formats for its operands and writes to the sessions output. Spaces are always added between operands and a newline is appended. See fmt.Sprintln and fmt.Println for more information.

func (*Context) Scan

func (c *Context) Scan() (string, error)

Scan reads user input from the session. It does NOT behave like fmt.Scan().

func (*Context) ScanHidden

func (c *Context) ScanHidden() (string, error)

ScanHidden reads without echo.

type Session

type Session struct {
	// Action is the actual application logic which is looped until the
	// application gets terminated.
	Action Action

	// After is run AFTER the action function, BEFORE the session is closed.
	After Action

	// Before is run BEFORE the action function.
	Before Action
	// contains filtered or unexported fields
}

A Session is an interactive shell. The New() function should be used to obtain a new Session instance.

func New

func New(prompt string) *Session

New spawns an interactive session in the current terminal. A prompt character needs to be provided which will be printed when user input is awaited.

func (*Session) Run

func (s *Session) Run()

Run is a blocking method that executes the actual logic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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