tscreen

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 4 Imported by: 0

README

tscreen

A simple wrapper for tcell-based text-only interfaces.

Requirements

  • go get github.com/gdamore/tcell

What and why?

I wanted a terminal-based environment with a fixed-position command line and a large text area to display text. Other packages were mimicking GUIs in text mode, but that's more than I needed for some of my projects.

Features

  • Command box with editable input and history.
  • Text output with word wrap and less-mode.
  • Title and status lines.
  • Callbacks for input and command handling.

Keyboard shortcuts

There is a separate handler for different types of input. These are:

  • OnCommandFunc: This callback receives a string with the command buffer. The rest is up to the user.
  • OnRuneFunc: Filter callback to allow special handling while typing. Optionally return 0 to skip symbols.
  • OnFunc: The generic callback, used with SetTabFunc() and SetEscFunc().
  • OnCtrlFunc: Handler for control keys.

NOTE: Remember to set up a key or command to call the Quit() method.

TODO

(Possible) future enhancements:

  • Coloured status and mode
  • Command line manipulation in the filter?
  • Built-in TAB-completion?
  • Figure out why resizing sometimes messes up things

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WordWrap

func WordWrap(txt string, width int) []string

WordWrap returns one or more lines, based on the desired width. This will be split at spaces or punctuation as far as possible, but if using small widths and long words they will be split and a hyphen added.

Types

type OnCommandFunc

type OnCommandFunc = func(string)

OnCommandFunc is the callback to run when a command has been entered and return has been pressed.

type OnCtrlFunc

type OnCtrlFunc = func(tcell.Key)

OnCtrlFunc is the callback for ctrl+<key> combinations without other modifiers.

type OnFunc

type OnFunc = func()

OnFunc is the signature for callbacks without any input.

type OnRuneFunc

type OnRuneFunc = func(rune) rune

ObRuneFunc is the callback for keypresses corresponding to a printable character, including space. Use this to filter or modify the input. Return the rune to display as-is, 0 to skip it, or modify the command buffer directly.

type Screen

type Screen struct {
	sync.WaitGroup
	tcell.Screen

	// callbacks
	OnRune    OnRuneFunc
	OnCommand OnCommandFunc
	OnTab     OnFunc
	OnEsc     OnFunc
	OnCtrl    OnCtrlFunc
	// contains filtered or unexported fields
}

Screen controls a terminal's input and output.

func New

func New() (*Screen, error)

New returns a pointer to a Screen structure which wraps some tcell functionality.

func (*Screen) AddHistory

func (s *Screen) AddHistory(cmd string)

AddHistory adds a string to the command history.

func (*Screen) AddText

func (s *Screen) AddText(txt string)

AddText adds text to the output buffer, splitting it if needed. TODO: History buffer and PgUp/PgDown.

func (*Screen) ClearCommand

func (s *Screen) ClearCommand()

ClearCommand clears the command buffer and updates the display.

func (*Screen) ClearText

func (s *Screen) ClearText()

ClearText clears the output section.

func (*Screen) ClearTextBuffer

func (s *Screen) ClearTextBuffer()

ClearTextBuffer clears the output buffer.

func (*Screen) Close

func (s *Screen) Close()

Close the screen and restore the terminal.

func (*Screen) P

func (s *Screen) P(x, y int, txt string)

P prints a string at the desired position.

func (*Screen) PC

func (s *Screen) PC()

PC prints the command buffer.

func (*Screen) PL

func (s *Screen) PL()

PL prints all lines in the line buffer.

func (*Screen) Quit

func (s *Screen) Quit()

Quit signals that the screen should close and the input loop should stop.

func (*Screen) Refresh

func (s *Screen) Refresh()

Refresh the display.

func (*Screen) Run

func (s *Screen) Run()

Run the input loop.

func (*Screen) SetCommandFunc

func (s *Screen) SetCommandFunc(fn OnCommandFunc)

SetCommandFunc sets a command handler.

func (*Screen) SetCtrlFunc

func (s *Screen) SetCtrlFunc(fn OnCtrlFunc)

SetEscFunc sets a callback for control keys.

func (*Screen) SetEscFunc

func (s *Screen) SetEscFunc(fn OnFunc)

SetEscFunc sets a callback for escape.

func (*Screen) SetRuneFunc

func (s *Screen) SetRuneFunc(fn OnRuneFunc)

SetRuneFunc sets an input filter callback for command input.

func (*Screen) SetStatus

func (s *Screen) SetStatus(txt string)

SetStatus sets the status text above the command entry.

func (*Screen) SetTabFunc

func (s *Screen) SetTabFunc(fn OnFunc)

SetTabFunc sets a TAB callback.

func (*Screen) SetTitle

func (s *Screen) SetTitle(t string)

SetTitle sets the text at the top of the terminal, with white background.

func (*Screen) UpdateStatus

func (s *Screen) UpdateStatus()

UpdateStatus updates the status text and symbol in front of the command line.

func (*Screen) UpdateTitle

func (s *Screen) UpdateTitle()

UpdateTitle refreshes the title display.

func (*Screen) Whiteout

func (s *Screen) Whiteout(x, y, n int)

Whiteout creates a line of n white blocks at the specified position.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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