Skip to content

aymanbagabas/go-vte

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-vte

Go Report Card License MIT Go Doc

A GO version of https://github.com/alacritty/vte.

The pkg vtparse implements a state machine that mirrors the behaviour of DEC (Digital Equipment Corporation) VT hardware terminals. The state machine was originally described by Paul Williams; more information can be found here: http://www.vt100.net/emu/dec_ansi_parser.

The pkg utf8 implements a state machine that reads UTF-8.

Install

go get -u github.com/danielgatis/go-vte

And then import the package in your code:

import "github.com/danielgatis/go-vte/vtparser"

Example

An example described below is one of the use cases.

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"

	"github.com/danielgatis/go-vte/vtparser"
)

type dispatcher struct{}

func (p *dispatcher) Print(r rune) {
	fmt.Printf("[Print] %c\n", r)
}

func (p *dispatcher) Execute(b byte) {
	fmt.Printf("[Execute] %02x\n", b)
}

func (p *dispatcher) Put(b byte) {
	fmt.Printf("[Put] %02x\n", b)
}

func (p *dispatcher) Unhook() {
	fmt.Printf("[Unhook]\n")
}

func (p *dispatcher) Hook(params []int64, intermediates []byte, ignore bool, r rune) {
	fmt.Printf("[Hook] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
}

func (p *dispatcher) OscDispatch(params [][]byte, bellTerminated bool) {
	fmt.Printf("[OscDispatch] params=%v, bellTerminated=%v\n", params, bellTerminated)
}

func (p *dispatcher) CsiDispatch(params []int64, intermediates []byte, ignore bool, r rune) {
	fmt.Printf("[CsiDispatch] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
}

func (p *dispatcher) EscDispatch(intermediates []byte, ignore bool, b byte) {
	fmt.Printf("[EscDispatch] intermediates=%v, ignore=%v, byte=%02x\n", intermediates, ignore, b)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	dispatcher := &dispatcher{}
	parser := vtparser.New(
		dispatcher.Print,
		dispatcher.Execute,
		dispatcher.Put,
		dispatcher.Unhook,
		dispatcher.Hook,
		dispatcher.OscDispatch,
		dispatcher.CsiDispatch,
		dispatcher.EscDispatch,
	)
	
	buff := make([]byte, 2048)

	for {
		n, err := reader.Read(buff)

		if err != nil {
			if err == io.EOF {
				return
			}

			fmt.Printf("Err %v:", err)
			return
		}

		for _, b := range buff[:n] {
			parser.Advance(b)
		}
	}
}
❯ echo -ne "Hello\nWorld" | go run main.go
[Print] H
[Print] e
[Print] l
[Print] l
[Print] o
[Execute] 0a
[Print] W
[Print] o
[Print] r
[Print] l
[Print] d

License

Copyright (c) 2020-present Daniel Gatis

Licensed under MIT License

About

A library to parse terminal escape sequences (almost) exactly how the real hardware does.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 97.0%
  • Ruby 2.4%
  • HTML 0.6%