| 1 | # About terminfo [![GoDoc][1]][2] |
| 2 | |
| 3 | Package `terminfo` provides a pure-Go implementation of reading information |
| 4 | from the terminfo database. |
| 5 | |
| 6 | `terminfo` is meant as a replacement for `ncurses` in simple Go programs. |
| 7 | |
| 8 | ## Installing |
| 9 | |
| 10 | Install in the usual Go way: |
| 11 | |
| 12 | ```sh |
| 13 | $ go get -u github.com/xo/terminfo |
| 14 | ``` |
| 15 | |
| 16 | ## Using |
| 17 | |
| 18 | Please see the [GoDoc API listing][2] for more information on using `terminfo`. |
| 19 | |
| 20 | ```go |
| 21 | // _examples/simple/main.go |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "fmt" |
| 27 | "log" |
| 28 | "os" |
| 29 | "os/signal" |
| 30 | "strings" |
| 31 | "sync" |
| 32 | "syscall" |
| 33 | |
| 34 | "github.com/xo/terminfo" |
| 35 | ) |
| 36 | |
| 37 | func main() { |
| 38 | //r := rand.New(nil) |
| 39 | |
| 40 | // load terminfo |
| 41 | ti, err := terminfo.LoadFromEnv() |
| 42 | if err != nil { |
| 43 | log.Fatal(err) |
| 44 | } |
| 45 | |
| 46 | // cleanup |
| 47 | defer func() { |
| 48 | err := recover() |
| 49 | termreset(ti) |
| 50 | if err != nil { |
| 51 | log.Fatal(err) |
| 52 | } |
| 53 | }() |
| 54 | |
| 55 | terminit(ti) |
| 56 | termtitle(ti, "simple example!") |
| 57 | termputs(ti, 3, 3, "Ctrl-C to exit") |
| 58 | maxColors := termcolors(ti) |
| 59 | if maxColors > 256 { |
| 60 | maxColors = 256 |
| 61 | } |
| 62 | for i := 0; i < maxColors; i++ { |
| 63 | termputs(ti, 5+i/16, 5+i%16, ti.Colorf(i, 0, "█")) |
| 64 | } |
| 65 | |
| 66 | // wait for signal |
| 67 | sigs := make(chan os.Signal, 1) |
| 68 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 69 | <-sigs |
| 70 | } |
| 71 | |
| 72 | // terminit initializes the special CA mode on the terminal, and makes the |
| 73 | // cursor invisible. |
| 74 | func terminit(ti *terminfo.Terminfo) { |
| 75 | buf := new(bytes.Buffer) |
| 76 | // set the cursor invisible |
| 77 | ti.Fprintf(buf, terminfo.CursorInvisible) |
| 78 | // enter special mode |
| 79 | ti.Fprintf(buf, terminfo.EnterCaMode) |
| 80 | // clear the screen |
| 81 | ti.Fprintf(buf, terminfo.ClearScreen) |
| 82 | os.Stdout.Write(buf.Bytes()) |
| 83 | } |
| 84 | |
| 85 | // termreset is the inverse of terminit. |
| 86 | func termreset(ti *terminfo.Terminfo) { |
| 87 | buf := new(bytes.Buffer) |
| 88 | ti.Fprintf(buf, terminfo.ExitCaMode) |
| 89 | ti.Fprintf(buf, terminfo.CursorNormal) |
| 90 | os.Stdout.Write(buf.Bytes()) |
| 91 | } |
| 92 | |
| 93 | // termputs puts a string at row, col, interpolating v. |
| 94 | func termputs(ti *terminfo.Terminfo, row, col int, s string, v ...interface{}) { |
| 95 | buf := new(bytes.Buffer) |
| 96 | ti.Fprintf(buf, terminfo.CursorAddress, row, col) |
| 97 | fmt.Fprintf(buf, s, v...) |
| 98 | os.Stdout.Write(buf.Bytes()) |
| 99 | } |
| 100 | |
| 101 | // sl is the status line terminfo. |
| 102 | var sl *terminfo.Terminfo |
| 103 | |
| 104 | // termtitle sets the window title. |
| 105 | func termtitle(ti *terminfo.Terminfo, s string) { |
| 106 | var once sync.Once |
| 107 | once.Do(func() { |
| 108 | if ti.Has(terminfo.HasStatusLine) { |
| 109 | return |
| 110 | } |
| 111 | // load the sl xterm if terminal is an xterm or has COLORTERM |
| 112 | if strings.Contains(strings.ToLower(os.Getenv("TERM")), "xterm") || os.Getenv("COLORTERM") == "truecolor" { |
| 113 | sl, _ = terminfo.Load("xterm+sl") |
| 114 | } |
| 115 | }) |
| 116 | if sl != nil { |
| 117 | ti = sl |
| 118 | } |
| 119 | if !ti.Has(terminfo.HasStatusLine) { |
| 120 | return |
| 121 | } |
| 122 | buf := new(bytes.Buffer) |
| 123 | ti.Fprintf(buf, terminfo.ToStatusLine) |
| 124 | fmt.Fprint(buf, s) |
| 125 | ti.Fprintf(buf, terminfo.FromStatusLine) |
| 126 | os.Stdout.Write(buf.Bytes()) |
| 127 | } |
| 128 | |
| 129 | // termcolors returns the maximum colors available for the terminal. |
| 130 | func termcolors(ti *terminfo.Terminfo) int { |
| 131 | if colors := ti.Num(terminfo.MaxColors); colors > 0 { |
| 132 | return colors |
| 133 | } |
| 134 | return int(terminfo.ColorLevelBasic) |
| 135 | } |
| 136 | ``` |
| 137 | |
| 138 | [1]: https://godoc.org/github.com/xo/terminfo?status.svg |
| 139 | [2]: https://godoc.org/github.com/xo/terminfo |