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

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)
    }
}
Similar Resources

A very simple command line tool for downloading YouTube videos.

GoTube Overview This repository contains a single-file implementation of YouTube video downloader written in Go. It does not require any third-party p

Dec 20, 2022

Stonks is a terminal based stock visualizer and tracker that displays realtime stocks in graph format in a terminal.

Stonks is a terminal based stock visualizer and tracker that displays realtime stocks in graph format in a terminal.

Stonks is a terminal based stock visualizer and tracker. Installation Requirements: golang = 1.13 Manual Clone the repo Run make && make install Pack

Dec 16, 2022

Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

Sloc Cloc and Code (scc) A tool similar to cloc, sloccount and tokei. For counting physical the lines of code, blank lines, comment lines, and physica

Jan 8, 2023

A very basic cli keyring tool to use accross various OS.

A very basic cli keyring tool to use accross various OS.

Dec 14, 2022

A UI library for terminal applications.

A UI library for terminal applications.

tui: Terminal UI for Go A UI library for terminal applications. tui (pronounced tooey) provides a higher-level programming model for building rich ter

Jan 6, 2023

Terminal UI library with rich, interactive widgets — written in Golang

Terminal UI library with rich, interactive widgets — written in Golang

Rich Interactive Widgets for Terminal UIs This Go package provides commonly needed components for terminal based user interfaces. Among these componen

Jan 7, 2023

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows.

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows.

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Dec 30, 2022

A Go library for terminal background color detection

go-termbg A Go library for terminal background color detection. The detected color is provided by RGB or theme ( dark or light ). Based on https://git

Jan 4, 2022

Qalam - An easy to use terminal styling library

Qalam - An easy to use terminal styling library

Qalam ✏️ Qalam is a Go library for easy terminal styling. Installation go get gi

Jan 6, 2023
Comments
  • Too many screen refreshes

    Too many screen refreshes

    screen.Clear() is pretty laggy on Windows, the following hack should be considered:

    Instead of always clearing before printing text, the text could be filled with empty spaces (this is what tcell.Clear() does in the background anyways)

    Here's an example:

    func render(screen tcell.Screen, question string, options []*Choice, config *Config, selectedChoice *Choice) {
    	//screen.Clear()
    	_, maximumThatCanBeDisplayed := screen.Size()
    	lineNumber := 0
    	questionLines := strings.Split(question, "\n")
    	for _, line := range questionLines {
    		printText(screen, 1, lineNumber, line, config.TextColor, config.BackgroundColor, config.SelectedTextBold)
    		lineNumber += 1
    	}
    	min := selectedChoice.Id + len(questionLines)
    	max := maximumThatCanBeDisplayed
    	if selectedChoice.Id > max {
    		min += 1
    		max += 1
    	}
    	for _, option := range options {
    		if option.Id <= (min+1)-maximumThatCanBeDisplayed && !(option.Id > (min+1)-maximumThatCanBeDisplayed) {
    			continue
    		}
    		if option.Selected {
    			printText(screen, 1, lineNumber, fmt.Sprintf("> %s", option.Value), config.SelectedTextColor, config.BackgroundColor, config.SelectedTextBold)
    		} else {
    			printText(screen, 1, lineNumber, fmt.Sprintf("  %s", option.Value), config.TextColor, config.BackgroundColor, config.SelectedTextBold)
    		}
    		lineNumber += 1
    	}
    	screen.Show()
    }
    
    func printText(screen tcell.Screen, x, y int, text string, fg, bg tcell.Color, bold bool) {
    	for _, character := range text {
    		screen.SetCell(x, y, tcell.StyleDefault.Background(bg).Foreground(fg).Bold(bold), character)
    		x += runewidth.RuneWidth(character)
    	}
    	// instead of using screen.Clear(), we'll just update the ENTIRE line with empty spaces
    	width, _ := screen.Size()
    	for i := len(text); i < width; i++ {
    		screen.SetCell(x, y, tcell.StyleDefault.Background(bg).Foreground(fg), ' ')
    		x += runewidth.RuneWidth(' ')
    	}
    }
    

    Note that when the list of choices doesn't fit in the screen, this will cause some issues (e.g. bottom lines aren't updated), so something need to be done about that

  • Support higher number of options

    Support higher number of options

    When there's more options than what can fit on the terminal, ... should be added as the first and last option and should allow users to change the page.

A very very simple CLI tool to know the next and previous SpaceX flights.

Rocket A very very simple CLI tool to know the next and previous SpaceX flights. Commands rocket Get the next flight. rocket latest Get the last fligh

Apr 19, 2021
Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Flag Flag is a simple but powerful commandline flag parsing library for Go. Documentation Documentation can be found at Godoc Supported features bool

Sep 26, 2022
Download and install binaries from GitHub Releases, interactively.

bget Download and install binary files from GitHub Releases. Preview Install For Mac/Linux users, you can use Homebrew to install it: brew install ego

Aug 24, 2022
TScli - a very simple terminal-based client for TSWeb online judge

TScli TScli - a very simple terminal-based client for TSWeb online judge. It supports submitting problems and receiving feedback on them. Installation

Oct 24, 2021
A very simple note-taking CLI you can use from the terminal that uses a SQLite DB to persist, and query, notes.

Note Logger Summary A very simple note-taking CLI you can use from the terminal that uses a SQLite DB to persist, and query, notes. Building/Installin

Apr 14, 2022
go command line option parser

go-flags: a go library for parsing command line arguments This library provides similar functionality to the builtin flag library of go, but provides

Jan 4, 2023
Fully featured Go (golang) command line option parser with built-in auto-completion support.

go-getoptions Go option parser inspired on the flexibility of Perl’s GetOpt::Long. Table of Contents Quick overview Examples Simple script Program wit

Dec 14, 2022
Option container for golang

Option Option provides an Option container which can be used to force some addit

Dec 21, 2021
Option: a busy optional parameter for golang

Option option is a busy optional parameter init opt := option.New() set opt.Set("parameter name", 3.141592) apply a := 0.0 opt.Apply("parameter name",

Dec 28, 2021
Mcli - A mininal and very powerful cli library for Go

mcli mcli is a minimal but powerful cli library for Go. m stands for minimal and

Nov 18, 2022