| 1 |
package terminal |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// STR sequences are similar to CSI sequences, but have string arguments (and |
| 9 |
// as far as I can tell, don't really have a name; STR is the name I took from |
| 10 |
// suckless which I imagine comes from rxvt or xterm). |
| 11 |
type strEscape struct { |
| 12 |
typ rune |
| 13 |
buf []rune |
| 14 |
args []string |
| 15 |
} |
| 16 |
|
| 17 |
func (s *strEscape) reset() { |
| 18 |
s.typ = 0 |
| 19 |
s.buf = s.buf[:0] |
| 20 |
s.args = nil |
| 21 |
} |
| 22 |
|
| 23 |
func (s *strEscape) put(c rune) { |
| 24 |
// TODO: improve allocs with an array backed slice; bench first |
| 25 |
if len(s.buf) < 256 { |
| 26 |
s.buf = append(s.buf, c) |
| 27 |
} |
| 28 |
// Going by st, it is better to remain silent when the STR sequence is not |
| 29 |
// ended so that it is apparent to users something is wrong. The length sanity |
| 30 |
// check ensures we don't absorb the entire stream into memory. |
| 31 |
// TODO: see what rxvt or xterm does |
| 32 |
} |
| 33 |
|
| 34 |
func (s *strEscape) parse() { |
| 35 |
s.args = strings.Split(string(s.buf), ";") |
| 36 |
} |
| 37 |
|
| 38 |
func (s *strEscape) arg(i, def int) int { |
| 39 |
if i >= len(s.args) || i < 0 { |
| 40 |
return def |
| 41 |
} |
| 42 |
i, err := strconv.Atoi(s.args[i]) |
| 43 |
if err != nil { |
| 44 |
return def |
| 45 |
} |
| 46 |
return i |
| 47 |
} |
| 48 |
|
| 49 |
func (s *strEscape) argString(i int, def string) string { |
| 50 |
if i >= len(s.args) || i < 0 { |
| 51 |
return def |
| 52 |
} |
| 53 |
return s.args[i] |
| 54 |
} |
| 55 |
|
| 56 |
func (t *State) handleSTR() { |
| 57 |
s := &t.str |
| 58 |
s.parse() |
| 59 |
|
| 60 |
switch s.typ { |
| 61 |
case ']': // OSC - operating system command |
| 62 |
switch d := s.arg(0, 0); d { |
| 63 |
case 0, 1, 2: |
| 64 |
title := s.argString(1, "") |
| 65 |
if title != "" { |
| 66 |
t.setTitle(title) |
| 67 |
} |
| 68 |
case 4: // color set |
| 69 |
if len(s.args) < 3 { |
| 70 |
break |
| 71 |
} |
| 72 |
// setcolorname(s.arg(1, 0), s.argString(2, "")) |
| 73 |
case 104: // color reset |
| 74 |
// TODO: complain about invalid color, redraw, etc. |
| 75 |
// setcolorname(s.arg(1, 0), nil) |
| 76 |
default: |
| 77 |
t.logf("unknown OSC command %d\n", d) |
| 78 |
// TODO: s.dump() |
| 79 |
} |
| 80 |
case 'k': // old title set compatibility |
| 81 |
title := s.argString(0, "") |
| 82 |
if title != "" { |
| 83 |
t.setTitle(title) |
| 84 |
} |
| 85 |
default: |
| 86 |
// TODO: Ignore these codes instead of complain? |
| 87 |
// 'P': // DSC - device control string |
| 88 |
// '_': // APC - application program command |
| 89 |
// '^': // PM - privacy message |
| 90 |
|
| 91 |
t.logf("unhandled STR sequence '%c'\n", s.typ) |
| 92 |
// t.str.dump() |
| 93 |
} |
| 94 |
} |