ttymap

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 3 Imported by: 0

README

ttymap

import "github.com/cognusion/go-ttymap"

Overview

Package ttymap is a utility to map keypresses (runes) to callbacks for CLI applications. The rune pressed is sent to the callback, but can be ignored.

Example :
var (
    runeP rune
    runeQ rune
)

z := New()
go z.Run() // launch it async

// closer stops Gnome and Vinz Clortho
closer := func(r rune) {
    // close(someChan) or something.Stop() to shut your app down
    z.Close() // Stupid important that this gets called, but waiting for a defer is generally late.
}

pause := func(r rune) {
    // pause your app?
}

z.Upsert(runeP, pause)  // toggles pause status
z.Upsert(runeQ, closer) // closes all the things

// start your app up

// z.Run() // blocks

Index

Examples
Package files

ttymap.go

type KeyFunc

type KeyFunc func(rune)

KeyFunc is a type used for callbacks upon keypresses.

type TtyMap

type TtyMap struct {
    // contains filtered or unexported fields
}

TtyMap is cool, eh?

func New
func New() *TtyMap

New returns a barely initialized TtyMap.

func (*TtyMap) Close
func (z *TtyMap) Close()

Close will signal the Run() loop to end, and close the TTY. You must create a New() if you want to continue mapping the keypresses.

func (*TtyMap) Remove
func (z *TtyMap) Remove(r rune)

Remove will delete the key denoted by the specified rune if it exists. This function is safe to call across goros. This function is safe to call if one is uncertain if the rune has been Upsertted previously.

func (*TtyMap) Run
func (z *TtyMap) Run()

Run opens the TTY and waits for a keypress. Run is intended to be executed in its own goro, but could be run inline. Will only execute once, but may be called concurrently.

func (*TtyMap) Upsert
func (z *TtyMap) Upsert(r rune, f KeyFunc)

Upsert either updates or adds a func(rune) for keypress of the specified rune. This function is safe to call across goros, however if the same rune is used in multiple requests, the "last" one "wins".


Generated by godoc2md

Documentation

Overview

Package ttymap is a utility to map keypresses (runes) to callbacks for CLI applications. The rune pressed is sent to the callback, but can be ignored.

Example
var (
	runeP rune
	runeQ rune
)

z := New()
go z.Run() // launch it async

// closer stops Gnome and Vinz Clortho
closer := func(r rune) {
	// close(someChan) or something.Stop() to shut your app down
	z.Close() // Stupid important that this gets called, but waiting for a defer is generally late.
}

pause := func(r rune) {
	// pause your app?
}

z.Upsert(runeP, pause)  // toggles pause status
z.Upsert(runeQ, closer) // closes all the things

// start your app up

// z.Run() // blocks

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyFunc

type KeyFunc func(rune)

KeyFunc is a type used for callbacks upon keypresses.

type TtyMap

type TtyMap struct {
	// contains filtered or unexported fields
}

TtyMap is cool, eh?

func New

func New() *TtyMap

New returns a barely initialized TtyMap.

func (*TtyMap) Close

func (z *TtyMap) Close()

Close will signal the Run() loop to end, and close the TTY. You must create a New() if you want to continue mapping the keypresses.

func (*TtyMap) Remove

func (z *TtyMap) Remove(r rune)

Remove will delete the key denoted by the specified rune if it exists. This function is safe to call across goros. This function is safe to call if one is uncertain if the rune has been Upsertted previously.

func (*TtyMap) Run

func (z *TtyMap) Run()

Run opens the TTY and waits for a keypress. Run is intended to be executed in its own goro, but could be run inline. Will only execute once, but may be called concurrently.

func (*TtyMap) Upsert

func (z *TtyMap) Upsert(r rune, f KeyFunc)

Upsert either updates or adds a func(rune) for keypress of the specified rune. This function is safe to call across goros, however if the same rune is used in multiple requests, the "last" one "wins".

Jump to

Keyboard shortcuts

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