widgets

package module
v0.0.0-...-4347dc3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gocui-widgets

Set of thin gocui widgets with higher-level abstractions such as event listeners and changers to help you build TUI apps. It is meant to use with gocui not instead.

go get github.com/shilangyu/gocui-widgets

Usage

All widgets implement gocui.Manager therefore can be added as managers or rendered directly with Widget.Layout(g).

All widgets expose their *gocui.View in the Widget.View property.

All widget have a widget type to distinguish them apart in the Widget.Type property.

Some widgets accept callback functions called OnSomething that are called when Something happens.

Some widgets have changers methods called ChangeSomething that you call with gocui.Update whenever you need to change Something.

Check the _examples to find out more and godoc for a more thorough documentation

Widgets

Text

Plain text

NewText(name, text string, frame, center bool, x, y int)

Parameters:

name description
name ID of your widget (passed to the gocui.View)
text value to the printed in your widget
frame if true a frame is rendered
center if true x and y become the center of the widget
x, y coordinates of the widget

Changers:

name description
ChangeText changes the inner text of the widget
Collection

Grouping frame with a title

NewCollection(name, title string, center bool, x, y int, w, h int)

Parameters:

name description
name ID of your widget (passed to the gocui.View)
title title to be printed
center if true x and y become the center of the widget
x, y coordinates of the widget
w, h size of the widget

Changers:

name description
ChangeTitle changes the title of the widget
Input

Input field

NewInput(name string, frame, center bool, x, y int, w, h int, onChange func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool)

Parameters:

name description
name ID of your widget (passed to the gocui.View)
frame if true a frame is rendered
center if true x and y become the center of the widget
x, y coordinates of the widget
w, h size of the widget

Changers:

name description
ChangeText changes the input text

Listeners:

name description
onChange a gocui.EditorFunc function that returns a bool, if returned false this change wont be accepted and the input will stay unchanged
Menu

Items menu (make sure to set g.Mouse to true for mouse support)

NewMenu(name string, items []string, center, arrows bool, x, y int, onChange, onSubmit func(i int))

Parameters:

name description
name ID of your widget (passed to the gocui.View)
items slice of items in your menu
center if true x and y become the center of the widget
x, y coordinates of the widget

Listeners:

name description
onChange accepts the index of currently selected item, ran whenever a new item is selected
onSubmit accepts the index of the submitted item, ran whenever an item is double clicked or pressed with Enter

Changers:

name description
ChangeSelected changes the selected menu item
Menu

Modal with options (make sure to set g.Mouse to true for mouse support)

NewModal(name string, text string, choices []string, center bool, x, y int, onChange, onSubmit func(i int))

Parameters:

name description
name ID of your widget (passed to the gocui.View)
text message text to be displayed
choices slice of choices in the modal
center if true x and y become the center of the widget
x, y coordinates of the widget

Listeners:

name description
onChange accepts the index of currently selected item, ran whenever a new item is selected
onSubmit accepts the index of the submitted item, ran whenever an item is double clicked or pressed with Enter

Hide with

modal.Hide()

Examples

To run a widget example clone this repo and go to the _examples directory. Then:

go run <widget-name>.go

Documentation

Overview

Package widgets gives you widgets that allow you to quickly start writing TUI apps with gocui. Creating a widget is easy:

package main

import (
    "github.com/awesome-gocui/gocui"
    "github.com/panproteus/gocui-widgets"
)

func main() {
    g, err := gocui.NewGui(gocui.OutputNormal)
    if err != nil {
        panic(err)
    }
    defer g.Close()

    w, h := g.Size()
    textWi := widgets.NewText("example-widget", "hello world", true, true, w/2, h/2)

    g.SetManager(text)

    if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
        panic(err)
    }
}

To see more please visit '_examples/'

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection struct {
	// View is a pointer to the gocui view of this widget
	View *gocui.View
	// Type is an enum WidgetType
	Type WidgetType
	// contains filtered or unexported fields
}

Collection is a widget that groups other widgets

func NewCollection

func NewCollection(name, title string, center bool, x, y int, w, h int, overlaps byte) *Collection

NewCollection initializes the Collection widget if center is true x and y becomes the center not start

func (*Collection) ChangeTitle

func (w *Collection) ChangeTitle(t string) func(g *gocui.Gui) error

ChangeTitle changes the title of the widget

func (*Collection) Layout

func (w *Collection) Layout(g *gocui.Gui) error

Layout renders the Collection widget

type Input

type Input struct {
	// View is a pointer to the gocui view of this widget
	View *gocui.View
	// Type is an enum WidgetType
	Type WidgetType
	// contains filtered or unexported fields
}

Input is a widget allows for user input

func NewInput

func NewInput(name string, frame, center bool, x, y int, w, h int, overlaps byte, onChange func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool) *Input

NewInput initializes the Input widget if frame is true a border is rendered if center is true x and y becomes the center not start

func (*Input) ChangeText

func (w *Input) ChangeText(s string) func(g *gocui.Gui) error

ChangeText changes the inner text

func (*Input) Layout

func (w *Input) Layout(g *gocui.Gui) error

Layout renders the Input widget

type Menu struct {
	// View is a pointer to the gocui view of this widget
	View *gocui.View
	// Type is an enum WidgetType
	Type WidgetType
	// contains filtered or unexported fields
}

Menu is a widget that creates a vertical clickable menu

func NewMenu

func NewMenu(name string, items []string, center bool, x, y int, overlaps byte, onChange, onSubmit func(i int)) *Menu

NewMenu initializes the Menu widget

func (w *Menu) ChangeSelected(i int) func(g *gocui.Gui) error

ChangeSelected changes the selected menu item

func (w *Menu) Layout(g *gocui.Gui) error

Layout renders the Menu widget

type Modal struct {
	// View is a pointer to the gocui view of this widget
	View *gocui.View
	// Type is an enum WidgetType
	Type WidgetType
	// contains filtered or unexported fields
}

Modal is a widget creates a pop-up with buttons

func NewModal

func NewModal(name string, text string, choices []string, center bool, x, y int, overlaps byte, onChange, onSubmit func(i int)) *Modal

NewModal initializes the Menu widget

func (*Modal) Hide

func (w *Modal) Hide()

Hide "kills" the modal

func (*Modal) Layout

func (w *Modal) Layout(g *gocui.Gui) error

Layout renders the Modal widget

type Text

type Text struct {
	// View is a pointer to the gocui view of this widget
	View *gocui.View
	// Type is an enum WidgetType
	Type WidgetType
	// contains filtered or unexported fields
}

Text is a widget that displays text

func NewText

func NewText(name, text string, frame, center bool, x, y int, overlaps byte) *Text

NewText initializes the Text widget if frame is true a border is rendered if center is true x and y becomes the center not start

func (*Text) ChangeText

func (w *Text) ChangeText(s string) func(g *gocui.Gui) error

ChangeText changes the text within

func (*Text) Layout

func (w *Text) Layout(g *gocui.Gui) error

Layout renders the Text widget

type WidgetType

type WidgetType int

WidgetType is a enum describing the widget

const (
	// TypeText for Text widget
	TypeText WidgetType = iota
	// TypeMenu for Menu widget
	TypeMenu
	// TypeCollection for Collection widget
	TypeCollection
	// TypeInput for Input widget
	TypeInput
	// TypeModal for Modal widget
	TypeModal
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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