escapebox

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

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

Go to latest
Published: Mar 15, 2017 License: BSD-3-Clause Imports: 3 Imported by: 1

README

escapebox

This is a hack around termbox-go which allows detection of custom escape sequences.

Example

A working example is available here: examples/shifttab.go

Usage

I tried to keep the API pretty similar to using termbox directly. The basic idea is:

  1. Initialize escapebox.
  2. Register any custom escape sequences you want detected.
  3. Poll events with escapebox.PollEvent() instead of termbox.PollEvent().
  4. Check the escapebox.Event.Seq field to see if a sequence was detected.
  5. Close escapebox to clean it up.

Taking a closer look, here's how to download escapebox:

go get https://github.com/briansteffens/escapebox

Import it into a go program:

import "github.com/briansteffens/escapebox"

In a termbox application, here's how to initialize escapebox:

escapebox.Init()
defer escapebox.Close()

First you'll probably want some way to tell your sequences apart:

const (
	SeqShiftTab = 1
)

Now you can register your custom sequences. The following means that an escape key followed in rapid succession (< 1 ms) by two events with termbox.Event.Chr = 91 and 90 will map to a SeqShiftTab:

escapebox.Register(SeqShiftTab, 91, 90)

Now you can poll for events:

ev := escapebox.PollEvent()

The escapebox.Event structure returned by escapebox.PollEvent() is identical to the standard termbox.Event structure, except for one added field: an int called Seq. If Seq is set to SeqNone, then it's a standard event and all the usual fields like Ch and Key are set like normal. If Seq is not set to SeqNone, then it's a custom escape sequence matching one of the sequences you registered:

if ev.Seq == SeqShiftTab {
	// The shift+tab sequence was detected
} else if ev.Seq == SeqNone {
	// This is a regular event. Check Type, Ch, and Key to see what it is
	// as if it was a standard termbox event.
}

Documentation

Index

Constants

View Source
const (
	SeqNone = 0
)

Non-standard escape sequences

Variables

View Source
var Events chan Event

Output channel of escapebox.Event instances

Functions

func Close

func Close()

Cleanup escapebox.

func Init

func Init()

Initialize escapebox.

func Register

func Register(seq Sequence, chars ...rune)

Types

type Event

type Event struct {
	termbox.Event
	Seq Sequence
}

Custom version of termbox.Event with extra data for non-standard sequences.

func PollEvent

func PollEvent() Event

Read a single escapebox.Event from the output channel.

type Sequence

type Sequence int

type SequenceMask

type SequenceMask struct {
	Chars []rune
	Seq   Sequence
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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