console

package module
v0.0.0-...-1e33b8f Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 4 Imported by: 0

README

console - Tools and widgets for an interactive console

Copyright (c) 2024, Geert JM Vanderkelen

Package console offers a collection of tools and widgets for creating an interactive command line interface (CLI).

Quick Start

Show a list and use Up/Down keys to select an option:

package main

import (
	"fmt"
	"log"

	"github.com/golistic/console"
)

func main() {

	var options []string
	var values []int

	for i := range 40 {
		options = append(options, fmt.Sprintf("Option %02d", i))
	}

	for i := range 40 {
		values = append(values, i)
	}

	s, err := console.NewSelection(options, values)
	if err != nil {
		log.Fatal(err)
	}

	if err := s.RenderWithTheme("ascii"); err != nil {
		log.Fatal(err)
	}

	fmt.Println("Selected:", s.Selected())
}

The above will adapt by default to the height of the terminal, but you can use s.SetShowing(5) to only show 5 options at a time.

Supported themes:

License

Distributed under the MIT license. See LICENSE.txt for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction int

type Selection

type Selection[E any] struct {
	// contains filtered or unexported fields
}

Selection represents a selectable list of options with corresponding values. A user can use the Up- and Down-cursor keys to select an option, and push Enter to confirm the selection.

By default, the `ascii` theme is used, but a Nerd Font theme `nerdfont` is also available.

func NewSelection

func NewSelection[V ~[]E, E any](options []string, values V) (*Selection[E], error)

func (*Selection[E]) Render

func (s *Selection[E]) Render() error

Render renders the Selection.

func (*Selection[E]) RenderWithTheme

func (s *Selection[E]) RenderWithTheme(themeName string) error

RenderWithTheme renders the Selection with the specified theme. If the theme with the given name does not exist, the default theme of the Selection is used.

func (*Selection[E]) Selected

func (s *Selection[E]) Selected() E

Selected returns the currently selected option from the Selection.

func (*Selection[E]) SetShowing

func (s *Selection[E]) SetShowing(n int)

SetShowing sets the number of options to be shown in the selection. If n is less than 1, it sets the number of options to the terminal height minus 3. Otherwise, it sets the number of options to n.

func (*Selection[E]) SetTheme

func (s *Selection[E]) SetTheme(name string)

type Toggle

type Toggle[T any] struct {
	// contains filtered or unexported fields
}

func NewToggle

func NewToggle[V ~[]T, T any](label string, options []string, values V) (*Toggle[T], error)

func (*Toggle[T]) Label

func (tg *Toggle[T]) Label() string

func (*Toggle[T]) Render

func (tg *Toggle[T]) Render() error

func (*Toggle[T]) Selected

func (tg *Toggle[T]) Selected() T

Selected returns the currently toggled option.

func (*Toggle[E]) SetTheme

func (tg *Toggle[E]) SetTheme(name string)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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