termutil

package module
v0.0.0-...-2ecc4b2 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 7 Imported by: 2

README

tcell-util

GoDoc

This is a simple utility library for tcell v2. It's designed to be a more-or-less drop-in replacement of my earlier library termbox-util (in fact, the code is basically the same, just changing termbox idioms for tcell idioms). It provides sensible printing functions, input functions that take care of their own event loop and give you cute readline-style keybindings, and a way to parse a key event that gives you an Emacs-like keystring (e.g. ^C (ev.Key() is tcell.KeyCtrlC) -> "C-c"). Check out the documentation for more. Note that the package is imported as termutil; if you really like the name tcell-util, or you really hate yourself and are using the original alongside tcell-util (as well as termbox alongside tcell; please don't do this), import it like this:

import (
    "github.com/gdamore/tcell/v2"
    tcell-util "github.com/japanoise/tcell-util"
)

Will you do a version for tcell v1?

Well, no. But this isn't a complicated library, so it's not likely to use too many v2-isms. You could easily revert the commit where I added v2 support, or just do something like:

sed -i -e 's|github.com/gdamore/tcell/v2|github.com/gdamore/tcell|g' *.go

Can I trust it?

Well, I've been dogfooding the original version pretty much since its inception, because Gomacs is my usual "small changes" text editor. Sure, I rarely add much to it, but the API that it has is not going to change (because there's nothing wrong with it, and it'd be a pain...) and I'm on GitHub all the time for work, so any open issues should be seen to quickly (I may leave my own issues lying around, but if they annoy a user other than myself, then the gloves come off... again, see Gomacs).

That being said, I'm pretty lazy. This is a "for fun" side project; I don't even use Gomacs at work much these days because environment issues force me to use the inferior C version. I also don't really care about go modules; I will use them as much as they are needed to not break my code and explode violently, and no more (if the pre-module days were good enough for me, they're good enough for you!), but that should be fine; like I said, the API is stable and extremely unlikely to change. If someone complains, sure, I'll roll up a release at HEAD and be done with it.

It also has one major issue: It always assumes you're not using the combining character madness (i.e. calls to SetContent always have nil as the fourth argument). This will not change. Sorry, I don't want to complicate the API for something that the vast majority of people will never use.

Can't I just change the import lines and use the original with tcell?

Yes and no. I experimented with moving Gomacs over to tcell a while back, using tcell's termbox shim; I noted some issue which I forget. It may or may not occur in termbox-util, too. Sorry, I don't care enough to try it out.

Documentation

Overview

These are some nice functions torn out of Gomacs which I think are better suited to be out of the project for reuse. It's imported as termutil.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChoiceIndex

func ChoiceIndex(screen tcell.Screen, title string, choices []string, def int) int

Allows the user to select one of many choices displayed on-screen. Takes a title, choices, and default selection. Returns an index into the choices array; or def (default)

func ChoiceIndexCallback

func ChoiceIndexCallback(screen tcell.Screen, title string, choices []string, def int, f func(tcell.Screen, int, int, int)) int

As ChoiceIndex, but calls a function after drawing the interface, passing it the current selected choice, screen width, and screen height.

func ClearLine

func ClearLine(screen tcell.Screen, sx, y int)

Pass the screenwidth and a line number; this function will clear the given line.

func DisplayScreenMessage

func DisplayScreenMessage(screen tcell.Screen, messages ...string)

Prints all strings given to the screen, and allows the user to scroll through, rather like less(1).

func DynamicPromptWithCallback

func DynamicPromptWithCallback(screen tcell.Screen, prompt string, refresh func(tcell.Screen, int, int), callback func(string, string) string) string

As prompt, but calls a function after every keystroke that can modify the query.

func Edit

func Edit(screen tcell.Screen, defval, prompt string, refresh func(tcell.Screen, int, int)) string

Edit takes a default value and a refresh function. It allows the user to edit the default value. It returns what the user entered.

func EditDynamicWithCallback

func EditDynamicWithCallback(screen tcell.Screen, defval, prompt string, refresh func(tcell.Screen, int, int), callback func(string, string) string) string

EditDynamicWithCallback takes a default value, prompt, refresh function, and callback. It allows the user to edit the default value. It returns what the user entered.

func IsControl

func IsControl(ru rune) bool

Returns true if the rune is a control character or invalid rune

func ParseTcellEvent

func ParseTcellEvent(ev *tcell.EventKey) string

Parses a tcell.EventKey event and returns it as an emacs-ish keybinding string (e.g. "C-c", "LEFT", "TAB", etc.)

func PickColor

func PickColor(screen tcell.Screen, prompt string) tcell.Color

func PressKey

func PressKey(screen tcell.Screen, p string, refresh func(tcell.Screen, int, int), keys ...string) string

Asks the user to press one of a set of keys. Returns the one which they pressed.

func PrintRune

func PrintRune(screen tcell.Screen, x, y int, ru rune)

Prints the rune given on the screen. Uses reverse colors for control characters.

func PrintRuneStyle

func PrintRuneStyle(screen tcell.Screen, x, y int, ru rune, style tcell.Style)

Print the rune with reverse colors for control characters

func PrintString

func PrintString(screen tcell.Screen, x, y int, s string)

Prints the string given on the screen. Uses the above functions to choose how it appears.

func PrintStringStyle

func PrintStringStyle(screen tcell.Screen, x, y int, s string, style tcell.Style)

Print string with a style

func Prompt

func Prompt(screen tcell.Screen, prompt string, refresh func(tcell.Screen, int, int)) string

Get a string from the user. They can use typical emacs-ish editing commands, or press C-c or C-g to cancel.

func PromptWithCallback

func PromptWithCallback(screen tcell.Screen, prompt string, refresh func(tcell.Screen, int, int), callback func(string, string)) string

As prompt, but calls a function after every keystroke.

func Runewidth

func Runewidth(ru rune) int

Returns how many cells wide the given rune is.

func RunewidthStr

func RunewidthStr(s string) int

Returns how many cells wide the given string is

func WordCharacter

func WordCharacter(c rune) bool

Indicate whether the given rune is a word character

func YesNo

func YesNo(screen tcell.Screen, p string, refresh func(tcell.Screen, int, int)) bool

Displays the prompt p and asks the user to say y or n. Returns true if y; false if no.

func YesNoCancel

func YesNoCancel(screen tcell.Screen, p string, refresh func(tcell.Screen, int, int)) (bool, error)

Same as YesNo, but will return a non-nil error if the user presses C-g.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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