terminal

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: MIT Imports: 8 Imported by: 0

README

terminal

Build status Godoc GODEV

Terminal exposes the terminal as a matrix of characters and a stream of events.

Structure

+------------------------------------------------------+
|                      terminal                        |
+-----+--------------+--------------------+------------+
      |              |                    |
      v              v                    v
+-----+-----+  +-----+-----+    +---------+-------------+
|    tty    |  |   input   |    |      framebuffer      |
+-----------+  +-----+-----+    +-----+-----------+-----+
                     |                |           |
                     v                |           v
               +-----+-----+          |     +-----+-----+
               |  events   |          |     |   ansi    |
               +-----------+          |     +-----+-----+
                                      |           |
                                      v           v
                                +-----+-----------+-----+
                                |         area          |
                                +-----------------------+

terminal: Package terminal exposes the terminal as a matrix of characters and a collections of events.

It have two main components the input and the output:

Input

events Package events contains the basic primitives of the input events

input Package input converts a io.Reader (normally a tty) into a sequence of events.

tty Package tty implements the os part of the terminal.

Output

char Package char represent a Char in a terminal.

area Package area implements a matrix of Characters.

ansi Package ansi converts a given area into a stream of ansi sequences to be dump into a terminal.

framebuffer Package framebuffer stores the current terminal state and return the ansi sequences require to transform the current state to the new one.

Example

package main

import (
    "github.com/guillermo/terminal"
    "github.com/guillermo/terminal/char"
    "github.com/guillermo/terminal/events"
)


func main() {
	// An empty Terminal is a valid one
	term := &terminal.Terminal{}

	err := term.Open()
	if err != nil {
		panic(err)
	}

	// Always restore the terminal to the previous state
	defer term.Close()

	// Get the size of the terminal
	rows, cols := term.Size()

	// Rows and Cols start with 1
	term.Set(1, 1, char.C("H"))
    term.Set(rows, cols, char.C("i"))
    // Without calling Sync no changes will be dump to the Output
	term.Sync()

	for {
		// Listen for events
		e := term.NextEvent()
		if ke, ok := e.(events.KeyboardEvent); ok {
			if ke.Key == "q" {
				// EXIT
				break
			}
		}
	}
}

Documentation

Overview

Package terminal exposes the terminal as a matrix of characters and a collections of events.

package main

import (
	"github.com/guillermo/terminal"
	"github.com/guillermo/terminal/char"
	"github.com/guillermo/terminal/events"
)

func main() {
	// An empty Terminal is a valid one
	term := &terminal.Terminal{}

	err := term.Open()
	if err != nil {
		panic(err)
	}

	// Always restore the terminal to the previous state
	defer term.Close()

	// Get the size of the terminal
	rows, cols := term.Size()

	// Rows and Cols start with 1
	term.Set(1, 1, char.C("H"))
	term.Set(rows, cols, char.C("i"))
	term.Sync()

	for {
		// Listen for events
		e := term.NextEvent()
		if ke, ok := e.(events.KeyboardEvent); ok {
			if ke.Key == "q" {
				// EXIT
				break
			}
		}
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File added in v0.0.2

type File interface {
	Fd() uintptr
}

File is the interface used for getting the Fd() of the Output device. For example os.Stdout.Fd() returns the file descriptor needed to ask the OS for the terminal size.

type Terminal

type Terminal struct {
	Input       io.Reader
	Output      io.Writer
	DefaultChar char.Charer
	// contains filtered or unexported fields
}

Terminal holds the state of the current terminal

func (*Terminal) Close

func (t *Terminal) Close() error

Close resets the terminal to the previous state

func (*Terminal) NextEvent

func (t *Terminal) NextEvent() events.Event

NextEvent return the next events.Event. If there are no events available it will block.

func (*Terminal) Open

func (t *Terminal) Open() error

Open opens a terminal.

If Output is a File it will try to open the terminal as a tty.

If Output is nil, os.Stdout will be used.

If Input is nil, os.Stdin will be used.

If DefaultChar is present, it will clear the screen with the background Color of the DefaultChar

func (*Terminal) Set

func (t *Terminal) Set(row, col int, ch char.Charer)

Set changes the character display in the given row/col.

func (*Terminal) Size

func (t *Terminal) Size() (Rows, Cols int)

Size returns the terminal size

func (*Terminal) Sync

func (t *Terminal) Sync()

Sync dump all the changes in the buffer.

Directories

Path Synopsis
Package ansi converts a given area into a stream of ansi sequences to be dump into a terminal
Package ansi converts a given area into a stream of ansi sequences to be dump into a terminal
Package area implements a matrix of Characters
Package area implements a matrix of Characters
Package char represent a Char in a terminal
Package char represent a Char in a terminal
cmd
sequencegenerator
sequencegenerator Generates a sequences.go file containing most possible terminal input.
sequencegenerator Generates a sequences.go file containing most possible terminal input.
Package events contains the basic primitives of the input and output events
Package events contains the basic primitives of the input and output events
Package framebuffer stores the current terminal state and return the ansi sequences require to transform the current state to the new one
Package framebuffer stores the current terminal state and return the ansi sequences require to transform the current state to the new one
Package input converts a io.Reader (normally a tty) into a sequence of events.
Package input converts a io.Reader (normally a tty) into a sequence of events.
Package tty implements the os part of the terminal
Package tty implements the os part of the terminal

Jump to

Keyboard shortcuts

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