term

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 8 Imported by: 0

README

term

Package term provides some simple terminal helper functions. Go's built in support is extremely limited when it comes to working with terminal input. For example go is unable out of the box to read input data without a return.

Table of Contents

Usage

Listen for SIGWINCH
var err error
var tty *term.TTY
if tty, err = term.Open(); err != nil {
	return
}

go func() {
	for {
		select {
		case <-tty.SigWinSizeChan:
			if w, h, err := tty.Size(); err == nil {
				// take some action
			}
		}
	}
}()
Read from TTY
var err error
var tty *term.TTY
if tty, err = term.Open(); err != nil {
	return
}
defer tty.Close()

// Read char
c, _ := tty.ReadChar()
fmt.Println(c)

Research

First why would you want to do that? Well it turns out that Go doesn't have the ability out of the box to be able to read cli input without first having enter pressed. This is extremely inconvenient when you just want your app to wait for any key to be pressed, not only the enter key. To this end I'm putting a little time into understanding how to handle this and writing a few helper functions for this use case.

Termios

Termios is the newer Unix API for terminal I/O. An app that interacts with the terminal typically will open a serial device with the standard Unix system call open, configure communication paramaters, make read and write calls then finish with close.

There are two primary modes termios provides:

  • Cooked/Canonical Mode - input is assembled into lines and special characters are processed
  • Raw/Non-Canonical Mode - input is not processed in anyway, just characters are emitted.

Configuration is done via termios flags

Opening a TTY

You have the choice to open the TTY for reading and/or writing.

Documentation

Overview

Package term provides TTY helper functions for prompting for passwords etc...

Index

Constants

View Source
const (
	// Ascii 0 - 31 plus 127 are control characters
	KeyNUL       = rune(0)
	KeyBackSpace = rune(8)
	KeyTab       = rune(9)
	KeyLineFeed  = rune(10)
	KeyReturn    = rune(13)
	KeyEscape    = rune(27)
	KeyDelete    = rune(127)

	KeySpace       = rune(32)
	KeyExclamation = rune(33)
	KeyQuote       = rune(34)
	KeyHash        = rune(35)
	KeyDollar      = rune(36)
	KeyPercent     = rune(37)
	KeyAmpersand   = rune(38)
	KeyApostrophe  = rune(39)
	KeyLeftParen   = rune(40)
	KeyRightParen  = rune(41)
	KeyAsterisk    = rune(42)
	KeyPlus        = rune(43)
	KeyComma       = rune(44)
	KeyHyphen      = rune(45)
	KeyPeriod      = rune(46)
	KeySlash       = rune(47)
	KeyDigit0      = rune(48)
)

Symbol to rune table

Variables

This section is empty.

Functions

func AnyKey

func AnyKey() (err error)

AnyKey waits for any key to be pressed before returning.

func IsTTY added in v1.1.19

func IsTTY() bool

IsTTY simply checks if we are working with a TTY on os.Stdout

func IsTTYP added in v1.1.19

func IsTTYP(fd uintptr) bool

IsTTYP simply checks if we are working with a TTY File Descriptor

func Prompt

func Prompt(msg string) (err error)

Prompt prints out the given message and waits for any key to be pressed before returning.

func PromptRes

func PromptRes(msg string, opts ...string) (res string, err error)

PromptRes prints out the given message and waits for user response terminated by a return. Optionally desired options may be given and the message repeated if not a valid option. Options are not case sensitive

func WaitForKey

func WaitForKey(key byte) (err error)

WaitForKey waits the given key to be pressed

Types

type TTY

type TTY struct {
	Stdin          *bufio.Reader  // input reader
	Stdout         *bufio.Writer  // output writer
	SigWinSizeChan chan os.Signal // signal channel for unix.SIGWINCH
	// contains filtered or unexported fields
}

TTY provides interaction with the system tty

func Open

func Open() (tty *TTY, err error)

Open a new TTY helper instance https://github.com/golang/crypto/blob/master/ssh/terminal/util.go

func (*TTY) Close

func (tty *TTY) Close() (err error)

Close TTY resources and restore termios save state

func (*TTY) PromptRes

func (tty *TTY) PromptRes(msg string, opts ...string) (res string, err error)

PromptRes prints out the given message and waits for user response terminated by a return. Optionally desired options may be given and the message repeated if not a valid option. Options are not case sensitive

func (*TTY) ReadChar

func (tty *TTY) ReadChar() (res string, err error)

ReadChar from the TTY, blocks until data is present

func (*TTY) ReadLine

func (tty *TTY) ReadLine() (result string, err error)

ReadLine reads from the TTY until return is pressed i.e. '\r' returned string does not include the trailing '\r'

func (*TTY) ReadPassword

func (tty *TTY) ReadPassword() (result string, err error)

ReadPassword reads from TTY until return is pressed, printing out asterisks in place of echo

func (*TTY) ReadRune

func (tty *TTY) ReadRune() (r rune, err error)

ReadRune from the TTY, blocks until data is present

func (*TTY) ReadSensitive

func (tty *TTY) ReadSensitive() (result string, err error)

ReadSensitive reads from TTY until return is pressed, does not echo

func (*TTY) ReadString

func (tty *TTY) ReadString() (result string, err error)

ReadString reads from the TTY until return is pressed and echos back to TTY rune by rune

func (*TTY) Size

func (tty *TTY) Size() (col int, row int, err error)

Size returns the current size of the terminal window. Used in conjunction with the SigWinSizeChan one can react to terminal size changes.

Directories

Path Synopsis
Package color provides basic terminal color output via ANSI Escape Codes https://misc.flogisoft.com/bash/tip_colors_and_formatting
Package color provides basic terminal color output via ANSI Escape Codes https://misc.flogisoft.com/bash/tip_colors_and_formatting

Jump to

Keyboard shortcuts

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