ui

package
v0.1.0-beta2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package ui provides terminal widgets for git-spice.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfirmKeyMap = ConfirmKeyMap{
	Yes: key.NewBinding(
		key.WithKeys("y", "Y"),
		key.WithHelp("y", "yes"),
	),
	No: key.NewBinding(
		key.WithKeys("n", "N"),
		key.WithHelp("n", "no"),
	),
	Accept: key.NewBinding(
		key.WithKeys("enter", "tab"),
		key.WithHelp("enter/tab", "accept"),
	),
}

DefaultConfirmKeyMap is the default key map for a Confirm field.

View Source
var DefaultConfirmStyle = ConfirmStyle{
	Key: lipgloss.NewStyle().Foreground(_magentaColor),
}

DefaultConfirmStyle is the default style for a Confirm field.

View Source
var DefaultFormKeyMap = FormKeyMap{
	Cancel: key.NewBinding(
		key.WithKeys("ctrl+c"),
		key.WithHelp("ctrl+c", "cancel"),
	),
}

DefaultFormKeyMap is the default key map for a Form.

View Source
var DefaultFormStyle = FormStyle{
	Error:         lipgloss.NewStyle().Foreground(_redColor),
	Title:         _titleStyle,
	Description:   _descriptionStyle,
	AcceptedTitle: _acceptedTitleStyle,

	AcceptedField: lipgloss.NewStyle().Faint(true),
}

DefaultFormStyle is the default style for a Form.

View Source
var DefaultInputKeyMap = InputKeyMap{
	Accept: key.NewBinding(
		key.WithKeys("enter", "tab"),
		key.WithHelp("enter/tab", "accept"),
	),
}

DefaultInputKeyMap is the default key map for an input field.

View Source
var DefaultInputStyle = InputStyle{}

DefaultInputStyle is the default style for an input field.

View Source
var DefaultOpenEditorKeyMap = OpenEditorKeyMap{
	Edit: key.NewBinding(
		key.WithKeys("e"),
		key.WithHelp("e", "open editor"),
	),
	Accept: key.NewBinding(
		key.WithKeys("enter", "tab"),
		key.WithHelp("enter/tab", "accept"),
	),
}

DefaultOpenEditorKeyMap is the default key map for an OpenEditor field.

View Source
var DefaultOpenEditorStyle = OpenEditorStyle{
	Key:    lipgloss.NewStyle().Foreground(_magentaColor),
	Editor: lipgloss.NewStyle().Foreground(_greenColor),
}

DefaultOpenEditorStyle is the default style for an OpenEditor field.

View Source
var DefaultSelectKeyMap = SelectKeyMap{
	Up: key.NewBinding(
		key.WithKeys("up"),
		key.WithHelp("up", "go up"),
	),
	Down: key.NewBinding(
		key.WithKeys("down"),
		key.WithHelp("down", "go down"),
	),
	Accept: key.NewBinding(
		key.WithKeys("enter", "tab"),
		key.WithHelp("enter/tab", "accept"),
	),
	DeleteFilterChar: key.NewBinding(
		key.WithKeys("backspace", "ctrl+h"),
		key.WithHelp("backspace", "delete filter character"),
	),
}

DefaultSelectKeyMap is the default key map for a Select.

View Source
var DefaultSelectStyle = SelectStyle{
	Selected:     lipgloss.NewStyle().Foreground(_yellowColor),
	Highlight:    lipgloss.NewStyle().Foreground(_cyanColor),
	ScrollMarker: lipgloss.NewStyle().Foreground(_grayColor),
}

DefaultSelectStyle is the default style for a Select.

Functions

func AcceptField

func AcceptField() tea.Msg

AcceptField is a tea.Cmd to accept the currently focused field.

It should be returned by a field's [Update] method to accept the field and move to the next one.

func Run

func Run(f Field) error

Run presents a single field to the user and blocks until it's accepted or canceled.

This is a convenience function for forms with just one field.

Types

type Confirm

type Confirm struct {
	KeyMap ConfirmKeyMap
	Style  ConfirmStyle
	// contains filtered or unexported fields
}

Confirm is a boolean confirmation field that takes a yes or no answer.

func NewConfirm

func NewConfirm() *Confirm

NewConfirm builds a new confirm field that prompts the user with a yes or no question.

func (*Confirm) Description

func (c *Confirm) Description() string

Description returns the description for the confirm field.

func (*Confirm) Err

func (c *Confirm) Err() error

Err reports any errors in the confirm field.

func (*Confirm) Render

func (c *Confirm) Render(w Writer)

Render renders the confirm field to the given writer.

func (*Confirm) Title

func (c *Confirm) Title() string

Title returns the title for the confirm field.

func (*Confirm) Update

func (c *Confirm) Update(msg tea.Msg) tea.Cmd

Update handles a bubbletea event.

func (*Confirm) Value

func (c *Confirm) Value() bool

Value returns the current value of the confirm field.

func (*Confirm) WithDescription

func (c *Confirm) WithDescription(desc string) *Confirm

WithDescription sets the desc for the confirm field.

func (*Confirm) WithTitle

func (c *Confirm) WithTitle(title string) *Confirm

WithTitle sets the title for the confirm field.

func (*Confirm) WithValue

func (c *Confirm) WithValue(value *bool) *Confirm

WithValue sets the destination for the confirm field. The result of the field will be written to the given boolean pointer. The pointer's current value will be used as the default.

type ConfirmKeyMap

type ConfirmKeyMap struct {
	Yes    key.Binding
	No     key.Binding
	Accept key.Binding
}

ConfirmKeyMap defines the key bindings for Confirm.

type ConfirmStyle

type ConfirmStyle struct {
	Key lipgloss.Style // how to highlight keys
}

ConfirmStyle configures the appearance of a Confirm field.

type Editor

type Editor struct {
	// Command is the editor command to run.
	//
	// Defaults to "$EDITOR".
	Command string

	// Args are the arguments to pass to the editor command
	// before the file name.
	Args []string

	// Ext is the extension to assign to the file
	// before opening the editor.
	//
	// Defaults to "md".
	Ext string
}

Editor configures the editor to open.

func DefaultEditor

func DefaultEditor() Editor

DefaultEditor returns the default editor configuration.

type Field

type Field interface {
	Update(msg tea.Msg) tea.Cmd
	Render(Writer)

	// Err reports any errors for the field at render time.
	// These will be rendered in red below the field.
	//
	// It is the field's responsibility to ensure
	// that it does not post [AcceptField] while in an error state.
	Err() error

	// Title is a short title for the field.
	// This is always visible.
	Title() string

	// Description is a longer description of the field.
	// This is visible only while the field is focused.
	Description() string
}

Field is a single field in a form.

type Form

type Form struct {
	KeyMap FormKeyMap
	Style  FormStyle
	// contains filtered or unexported fields
}

Form presents a series of fields for the user to fill.

func NewForm

func NewForm(fields ...Field) *Form

NewForm builds a new form with the given fields.

func (*Form) Err

func (f *Form) Err() error

Err reports any errors that occurred during the form's execution or from any of the fields.

func (*Form) Init

func (f *Form) Init() tea.Cmd

Init initializes the form.

func (*Form) Run

func (f *Form) Run() error

Run runs the form and blocks until it's accepted or canceled. It returns a combination of all errors returned by the fields.

func (*Form) Update

func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update implements tea.Model.

func (*Form) View

func (f *Form) View() string

View implements tea.Model.

type FormKeyMap

type FormKeyMap struct {
	Cancel key.Binding
}

FormKeyMap defines the key bindings for a form. See DefaultFormKeyMap for default values.

type FormStyle

type FormStyle struct {
	Error lipgloss.Style

	Title         lipgloss.Style
	Description   lipgloss.Style
	AcceptedTitle lipgloss.Style

	AcceptedField lipgloss.Style
}

FormStyle configures the appearance of a Form.

type Input

type Input struct {
	KeyMap InputKeyMap
	Style  InputStyle
	// contains filtered or unexported fields
}

Input is a text input field. It accepts a single line of text.

func NewInput

func NewInput() *Input

NewInput builds a new input field.

func (*Input) Description

func (i *Input) Description() string

Description returns the description of the input field.

func (*Input) Err

func (i *Input) Err() error

Err reports any errors encountered during the operation. The error is nil if the input was accepted.

func (*Input) Render

func (i *Input) Render(w Writer)

Render renders the input field.

func (*Input) Title

func (i *Input) Title() string

Title returns the title of the input field.

func (*Input) Update

func (i *Input) Update(msg tea.Msg) tea.Cmd

Update handles a bubbletea event.

func (*Input) WithDescription

func (i *Input) WithDescription(desc string) *Input

WithDescription sets the description of the input field.

func (*Input) WithTitle

func (i *Input) WithTitle(title string) *Input

WithTitle sets the title of the input field.

func (*Input) WithValidate

func (i *Input) WithValidate(f func(string) error) *Input

WithValidate sets a validation function for the input field.

The field will not accept the input until the validation function returns nil.

func (*Input) WithValue

func (i *Input) WithValue(value *string) *Input

WithValue sets the destination for the input field. If the value is non-empty, it will be used as the initial value.

type InputKeyMap

type InputKeyMap struct {
	Accept key.Binding
}

InputKeyMap defines the key bindings for an input field.

type InputStyle

type InputStyle struct{}

InputStyle defines the styles for an input field.

type OpenEditor

type OpenEditor struct {
	KeyMap OpenEditorKeyMap
	Style  OpenEditorStyle
	Editor Editor
	// contains filtered or unexported fields
}

OpenEditor is a dialog that asks the user to press a key to open an editor and write a message.

func NewOpenEditor

func NewOpenEditor() *OpenEditor

NewOpenEditor builds an OpenEditor field. It will prompt the user to open an editor and write a message, or accept the current value.

func (*OpenEditor) Description

func (a *OpenEditor) Description() string

Description returns the description for the field.

func (*OpenEditor) Err

func (a *OpenEditor) Err() error

Err reports any errors encountered during the operation.

func (*OpenEditor) Render

func (a *OpenEditor) Render(w Writer)

Render renders the field to the screen.

func (*OpenEditor) Title

func (a *OpenEditor) Title() string

Title returns the title for the field.

func (*OpenEditor) Update

func (a *OpenEditor) Update(msg tea.Msg) tea.Cmd

Update receives a new event from bubbletea and updates the field's internal state.

func (*OpenEditor) WithDescription

func (a *OpenEditor) WithDescription(desc string) *OpenEditor

WithDescription sets the description for the field.

func (*OpenEditor) WithTitle

func (a *OpenEditor) WithTitle(title string) *OpenEditor

WithTitle sets the title for the field.

func (*OpenEditor) WithValue

func (a *OpenEditor) WithValue(value *string) *OpenEditor

WithValue specifies the value to edit. The current value will be used as the initial content of the editor. The value will be updated when the editor is closed, or left unchanged if the user skips the editor.

type OpenEditorKeyMap

type OpenEditorKeyMap struct {
	Edit   key.Binding
	Accept key.Binding
}

OpenEditorKeyMap defines the key bindings for OpenEditor.

type OpenEditorStyle

type OpenEditorStyle struct {
	Key    lipgloss.Style // how to highlight keys
	Editor lipgloss.Style
}

OpenEditorStyle defines the display style for OpenEditor.

type Select

type Select struct {
	KeyMap SelectKeyMap
	Style  SelectStyle
	// contains filtered or unexported fields
}

Select is a prompt that allows selecting from a list of options using a fuzzy filter.

func NewSelect

func NewSelect() *Select

NewSelect builds a new Select field.

func (*Select) Description

func (s *Select) Description() string

Description returns the description of the select field.

func (*Select) Err

func (s *Select) Err() error

Err reports any errors in the select field.

func (*Select) Render

func (s *Select) Render(out Writer)

Render renders the select field.

func (*Select) Title

func (s *Select) Title() string

Title returns the title of the select field.

func (*Select) Update

func (s *Select) Update(msg tea.Msg) tea.Cmd

Update receives messages from bubbletea.

func (*Select) Value

func (s *Select) Value() string

Value reports the current value of the select field.

func (*Select) WithDescription

func (s *Select) WithDescription(desc string) *Select

WithDescription sets the description for the select field.

func (*Select) WithOptions

func (s *Select) WithOptions(opts ...string) *Select

WithOptions sets the available options for the select field. The options will be presented in the order they are provided. Existing options will be replaced.

func (*Select) WithTitle

func (s *Select) WithTitle(title string) *Select

WithTitle sets the title for the select field.

func (*Select) WithValue

func (s *Select) WithValue(value *string) *Select

WithValue sets the destination for the select field. The existing value, if any, will be selected by default.

func (*Select) WithVisible

func (s *Select) WithVisible(visible int) *Select

WithVisible sets the number of visible options in the select field. If unset, a default is picked based on the terminal height.

type SelectKeyMap

type SelectKeyMap struct {
	Up     key.Binding
	Down   key.Binding
	Accept key.Binding

	DeleteFilterChar key.Binding
}

SelectKeyMap defines the key bindings for Select.

type SelectStyle

type SelectStyle struct {
	Selected  lipgloss.Style
	Highlight lipgloss.Style

	ScrollMarker lipgloss.Style
}

SelectStyle defines the styles for Select.

type Writer

type Writer interface {
	io.Writer
	io.StringWriter
}

Writer receives a rendered view of a Field.

Jump to

Keyboard shortcuts

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