gochoice

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2021 License: Apache-2.0 Imports: 6 Imported by: 1

README

go-choice

A very simple library for interactively selecting an option on a terminal

Usage

package main

import (
    "fmt"

    "github.com/TwiN/go-choice"
)

func main() {
    choice, index, err := gochoice.Pick(
        "What do you want to do?\nPick:",
        []string{
            "Connect to the production environment",
            "Connect to the test environment",
            "Update",
        },
    )
    if err != nil {
        fmt.Println("You didn't select anything!")
    } else {
        fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
    }
}

example

You can customize the experience further by appending options at the end of the Pick function:

package main

import (
    "fmt"

    "github.com/TwiN/go-choice"
)

func main() {
    choice, index, err := gochoice.Pick(
        "What do you want to do?\nPick:",
        []string{
            "Connect to the production environment",
            "Connect to the test environment",
            "Update",
        },
        gochoice.OptionBackgroundColor(gochoice.Black), 
        gochoice.OptionTextColor(gochoice.White),
        gochoice.OptionSelectedTextColor(gochoice.Red),
        gochoice.OptionSelectedTextBold(),
    )
    if err != nil {
        fmt.Println("You didn't select anything!")
    } else {
        fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoChoiceSelected is the error returned when no choices have been selected.
	// This can happen when the user quits the application by terminating the process (e.g. CTRL+C)
	// or by exiting the application through the ESC key or the left arrow key.
	ErrNoChoiceSelected = errors.New("no choice selected")

	// ErrNoChoice is the error returned when there are no choices to pick from
	ErrNoChoice = errors.New("no choices to choose from")
)

Functions

func OptionBackgroundColor

func OptionBackgroundColor(color Color) func(config *Config)

func OptionSelectedTextBold

func OptionSelectedTextBold() func(config *Config)

func OptionSelectedTextColor

func OptionSelectedTextColor(color Color) func(config *Config)

func OptionTextColor

func OptionTextColor(color Color) func(config *Config)

func Pick

func Pick(question string, choicesToPickFrom []string, options ...Option) (string, int, error)

Pick prompts the user to choose an option from a list of choices

Types

type Choice

type Choice struct {
	Id       int
	Value    string
	Selected bool
	// contains filtered or unexported fields
}

type Color

type Color int
const (
	Black Color = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	Orange
	Gold
	Gray
	Grey
	Fuchsia
	Brown
	Pink
	Purple
	Crimson
	DarkRed
	DarkBlue
	DarkGray
	DarkGrey
	LightBlue
	LightGray
	LightGrey
	White
)

type Config

type Config struct {
	TextColor         tcell.Color
	BackgroundColor   tcell.Color
	SelectedTextColor tcell.Color
	SelectedTextBold  bool
}

type Option

type Option func(config *Config)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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