-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexamples_test.go
89 lines (72 loc) · 1.92 KB
/
examples_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2022 Robert S. Muhlestein
// SPDX-License-Identifier: Apache-2.0
package term_test
import (
"fmt"
"os"
"github.com/rwxrob/bonzai/term"
"github.com/rwxrob/bonzai/term/esc"
)
func ExampleRed() {
term.Red = "<red>"
term.Reset = "<reset>"
fmt.Println(term.Red + "simply red" + term.Reset)
defer term.AttrOn()
term.AttrOff()
fmt.Println(term.Red + "simply red" + term.Reset)
// Output:
// <red>simply red<reset>
// simply red
}
func ExampleStripNonPrint() {
some := esc.Bold + "not bold" + esc.Reset
fmt.Println(term.StripNonPrint(some))
// Output;
// not bold
}
func ExampleEmphFromLess() {
/*
export LESS_TERMCAP_mb="[35m" # magenta
export LESS_TERMCAP_md="[33m" # yellow
export LESS_TERMCAP_me="" # "0m"
export LESS_TERMCAP_se="" # "0m"
export LESS_TERMCAP_so="[34m" # blue
export LESS_TERMCAP_ue="" # "0m"
export LESS_TERMCAP_us="[4m" # underline
*/
os.Setenv("LESS_TERMCAP_mb", esc.Magenta)
os.Setenv("LESS_TERMCAP_md", esc.Yellow)
os.Setenv("LESS_TERMCAP_me", esc.Reset)
os.Setenv("LESS_TERMCAP_se", esc.Reset)
os.Setenv("LESS_TERMCAP_so", esc.Blue)
os.Setenv("LESS_TERMCAP_ue", esc.Reset)
os.Setenv("LESS_TERMCAP_us", esc.Under)
term.EmphFromLess()
fmt.Printf("%q\n", term.Italic+"italic"+term.Reset)
fmt.Printf("%q\n", term.Bold+"bold"+term.Reset)
fmt.Printf("%q\n", term.BoldItalic+"bolditalic"+term.Reset)
fmt.Printf("%q\n", term.Under+"under"+term.Reset)
// Output:
// "\x1b[4mitalic\x1b[0m"
// "\x1b[33mbold\x1b[0m"
// "\x1b[35mbolditalic\x1b[0m"
// "\x1b[4munder\x1b[0m"
}
func ExampleREPL() {
defer term.TrapPanic()
// both are enclosed in prompt/respond functions
var history []string
hcount := 1
prompt := func(_ string) string {
if hcount > 3 {
panic("All done.")
}
return fmt.Sprintf("%v> ", hcount)
}
respond := func(in string) string {
hcount++
history = append(history, in)
return "okay"
}
term.REPL(prompt, respond)
}