~mna/zzcsi

ref: v0.1.0 zzcsi/example_test.go -rw-r--r-- 1.2 KiB
3a39bd0aMartin Angers add decode args, test, benchmark, example 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package zzcsi_test

import (
	"fmt"
	"io"

	"git.sr.ht/~mna/zzcsi"
)

func ExampleCSI_FuncString() {
	bold := zzcsi.ChAttr.FuncString(zzcsi.ChAttrBold)
	norm := zzcsi.ChAttr.FuncString(zzcsi.ChAttrNoBoldDim)
	blueBlink := zzcsi.ChAttr.FuncString(zzcsi.ChAttrFgBlue, zzcsi.ChAttrBlink)
	defNoBlink := zzcsi.ChAttr.FuncString(zzcsi.ChAttrFgDef, zzcsi.ChAttrNoBlink)
	fmt.Printf("%sHello%s, %sworld!%s\n", bold, norm, blueBlink, defNoBlink)
}

func ExampleDecodeArgs() {
	// Let's pretend that term is a terminal in raw mode
	var term io.ReadWriter

	// a typical usage would be to send a CSI function to get the cursor
	// position:
	//
	fn := zzcsi.DevStat.Func(zzcsi.DevStatCurPos)
	fmt.Fprint(term, fn)
	//
	// and then get the reply using the zzterm package, e.g.:
	//
	//  for {
	//  	k, err := input.ReadKey(term)
	//  	if err != nil {
	//  		log.Panic(err)
	//  	}
	//
	//  	switch k.Type() {
	//    case zzterm.KeyESCSeq:
	//      b := input.Bytes()
	//  	}
	//  }
	//
	// Let's pretend that's how we got that reply byte sequence:
	b := []byte("\x1b[12;33R")
	var row, col uint64
	n := zzcsi.DecodeArgs(b, &row, &col)
	if n != 2 {
		// oops, did not get both arguments, fail
	}
	// otherwise use row and col
}