options

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 4 Imported by: 0

README

options

options is a minimal Go library for console apps that want a little more than scanf.

You get the ability to easily construct terminal UI such as this:

Controls
  • Check Box
  • Radio Box
Conventions
  • Space toggles a choice
  • Enter accepts input
  • Escape exits the program (you can turn this off with Console.NoExitOnEscape)
Example
package main

import (
	"github.com/IMQS/options"
)

func main() {
	con := options.NewConsole()
	defer con.Close()

	boxes := []options.Checkbox{
		{false, "one"},
		{true, "two"},
	}

	con.CheckBoxes("Some checkboxes", "", boxes)
	// Use boxes[i].Checked to get status of the boxes

	con.Radio("Some radio buttons", "", -1, []string{"thing one", "thing two"})
	// Use return value from con.Radio to find out which items was selected

	con.Close()
}

For more examples, see the 'examples' directory in the source tree.

This library is built on top of https://github.com/gdamore/tcell.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checkbox

type Checkbox struct {
	Checked bool
	Title   string
}

Checkbox represents an item (typically in a group of two or more), that the user can tick on/off

type Console

type Console struct {
	Screen         tcell.Screen
	NoExitOnEscape bool // If true, then commands will not issue an os.Exit() if the user presses Escape. The user can still press Ctrl-C to perform an os.Exit()
}

Console represents a terminal console that you can control for UI

func NewConsole

func NewConsole() *Console

NewConsole creates a new console option, and initializes it. If anything fails, the function calls os.Exit(1) You must call Close() when you are finished using a console object, otherwise you will leave your user's terminal in an unusable state.

func (*Console) CheckBoxes

func (c *Console) CheckBoxes(title string, subTitle string, els []Checkbox) bool

Returns true if the user pressed enter to continue, or false in any other case.

func (*Console) Close

func (c *Console) Close()

Close the console neatly

func (*Console) DrawString

func (c *Console) DrawString(x, y int, str string, style tcell.Style)

DrawString is a helper function for drawing a string at a given location

func (*Console) EventPoll

func (c *Console) EventPoll(quit chan bool, eventHandler func(ev tcell.Event) bool)

EventPoll is a helper function that runs a Goroutine to perform event handling, and handles common things such as ENTER press, ESCAPE, Ctrl-C, Ctrl-L. eventHandler returns true to indicate that it has handled the event, or false to pass control onto this function.

func (*Console) Exit

func (c *Console) Exit()

Exit closes the console neatly, then does an os.Exit(1)

func (*Console) Radio

func (c *Console) Radio(title string, subTitle string, choice int, els []string) int

Returns the index of the chosen item, or -1 if the user pressed escape choice is the index of the currently chosen item

func (*Console) UILoop

func (c *Console) UILoop(quit chan bool, render func()) bool

UILoop runs the standard event loop

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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