console

package module
v0.0.0-...-69e7104 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 6 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:

  • ascii: simple, and works everywhere
  • nerdfont: you need to use Nerd Font in your terminal
  • color01: uses color LightGrey/Green for background/foreground
  • inverted: inverts the back/foreground color that the terminal is using

License

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAborted = errors.New("aborted")
)

Functions

func ClearLines

func ClearLines(n int)

ClearLines clears the specified number of lines in the console.

func TerminalSize

func TerminalSize() (width, height int)

TerminalSize returns the visible dimensions of the given terminal. When size could not be retrieved, weight 80 and height 24 is returned.

Types

type DefaultValue

type DefaultValue struct {
	Value any
	Found bool
}

type DefaultValueProps

type DefaultValueProps struct {
	SelectOption any
}

type Direction

type Direction int

type Form

type Form struct {
	Elements []FormElementer
	// contains filtered or unexported fields
}

func NewForm

func NewForm() *Form

func NewFormWithScanner

func NewFormWithScanner(scanner func(value any, dest any) error) *Form

func (*Form) AddElements

func (f *Form) AddElements(elements ...FormElementer)

func (*Form) Clear

func (f *Form) Clear()

func (*Form) Execute

func (f *Form) Execute() error

func (*Form) RawValues

func (f *Form) RawValues() map[string]any

func (*Form) SetTheme

func (f *Form) SetTheme(theme Theme) *Form

type FormElementer

type FormElementer interface {
	Label() string
	Name() string
	Value() any
	DefaultValue(func(props *DefaultValueProps) DefaultValue) FormElementer

	AddValidator(func(value any) error) FormElementer
	// contains filtered or unexported methods
}

type FormInput

type FormInput struct {
	// contains filtered or unexported fields
}

func NewFormInput

func NewFormInput(name, label string, dest any) *FormInput

func (*FormInput) AddValidator

func (fi *FormInput) AddValidator(f func(value any) error) FormElementer

func (*FormInput) DefaultValue

func (fi *FormInput) DefaultValue(f func(props *DefaultValueProps) DefaultValue) FormElementer

func (FormInput) Label

func (fe FormInput) Label() string

func (FormInput) Name

func (fe FormInput) Name() string

func (FormInput) Value

func (fe FormInput) Value() any

type FormSelect

type FormSelect struct {
	// contains filtered or unexported fields
}

func NewFormSelect

func NewFormSelect(name, label string, dest any, props SelectProps) *FormSelect

func (*FormSelect) AddValidator

func (fs *FormSelect) AddValidator(f func(value any) error) FormElementer

func (*FormSelect) DefaultValue

func (fs *FormSelect) DefaultValue(f func(props *DefaultValueProps) DefaultValue) FormElementer

func (FormSelect) Label

func (fe FormSelect) Label() string

func (FormSelect) Name

func (fe FormSelect) Name() string

func (FormSelect) Value

func (fe FormSelect) Value() any

type SelectProps

type SelectProps struct {
	Options  []string
	Values   []any
	Showing  int
	Selected func(value any) bool
}

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 Theme) 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]) SelectedOption

func (s *Selection[E]) SelectedOption() string

SelectedOption returns the currently selected option from the Selection.

func (*Selection[E]) SetSelected

func (s *Selection[E]) SetSelected(p int)

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(t Theme)

type Theme

type Theme string
const (
	ThemeNerdFont Theme = "nerdfont"
	ThemeAscii    Theme = "ascii"
	ThemeColor01  Theme = "color01"
	ThemeInverted Theme = "inverted"
)

func ThemeLookup

func ThemeLookup(name string) (Theme, bool)

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

Render renders the Selection.

func (*Toggle[T]) RenderWithTheme

func (tg *Toggle[T]) RenderWithTheme(t Theme) 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 (*Toggle[T]) Selected

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

Selected returns the currently toggled option.

func (*Toggle[E]) SetTheme

func (tg *Toggle[E]) SetTheme(t Theme)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳