| 1 |
package terminal |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// CSI (Control Sequence Introducer) |
| 9 |
// ESC+[ |
| 10 |
type csiEscape struct { |
| 11 |
buf []byte |
| 12 |
args []int |
| 13 |
mode byte |
| 14 |
priv bool |
| 15 |
} |
| 16 |
|
| 17 |
func (c *csiEscape) reset() { |
| 18 |
c.buf = c.buf[:0] |
| 19 |
c.args = c.args[:0] |
| 20 |
c.mode = 0 |
| 21 |
c.priv = false |
| 22 |
} |
| 23 |
|
| 24 |
func (c *csiEscape) put(b byte) bool { |
| 25 |
c.buf = append(c.buf, b) |
| 26 |
if b >= 0x40 && b <= 0x7E || len(c.buf) >= 256 { |
| 27 |
c.parse() |
| 28 |
return true |
| 29 |
} |
| 30 |
return false |
| 31 |
} |
| 32 |
|
| 33 |
func (c *csiEscape) parse() { |
| 34 |
c.mode = c.buf[len(c.buf)-1] |
| 35 |
if len(c.buf) == 1 { |
| 36 |
return |
| 37 |
} |
| 38 |
s := string(c.buf) |
| 39 |
c.args = c.args[:0] |
| 40 |
if s[0] == '?' { |
| 41 |
c.priv = true |
| 42 |
s = s[1:] |
| 43 |
} |
| 44 |
s = s[:len(s)-1] |
| 45 |
ss := strings.Split(s, ";") |
| 46 |
for _, p := range ss { |
| 47 |
i, err := strconv.Atoi(p) |
| 48 |
if err != nil { |
| 49 |
//t.logf("invalid CSI arg '%s'\n", p) |
| 50 |
break |
| 51 |
} |
| 52 |
c.args = append(c.args, i) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func (c *csiEscape) arg(i, def int) int { |
| 57 |
if i >= len(c.args) || i < 0 { |
| 58 |
return def |
| 59 |
} |
| 60 |
return c.args[i] |
| 61 |
} |
| 62 |
|
| 63 |
// maxarg takes the maximum of arg(i, def) and def |
| 64 |
func (c *csiEscape) maxarg(i, def int) int { |
| 65 |
return max(c.arg(i, def), def) |
| 66 |
} |
| 67 |
|
| 68 |
func (t *State) handleCSI() { |
| 69 |
c := &t.csi |
| 70 |
switch c.mode { |
| 71 |
default: |
| 72 |
goto unknown |
| 73 |
case '@': // ICH - insert <n> blank char |
| 74 |
t.insertBlanks(c.arg(0, 1)) |
| 75 |
case 'A': // CUU - cursor <n> up |
| 76 |
t.moveTo(t.cur.x, t.cur.y-c.maxarg(0, 1)) |
| 77 |
case 'B', 'e': // CUD, VPR - cursor <n> down |
| 78 |
t.moveTo(t.cur.x, t.cur.y+c.maxarg(0, 1)) |
| 79 |
case 'c': // DA - device attributes |
| 80 |
if c.arg(0, 0) == 0 { |
| 81 |
// TODO: write vt102 id |
| 82 |
} |
| 83 |
case 'C', 'a': // CUF, HPR - cursor <n> forward |
| 84 |
t.moveTo(t.cur.x+c.maxarg(0, 1), t.cur.y) |
| 85 |
case 'D': // CUB - cursor <n> backward |
| 86 |
t.moveTo(t.cur.x-c.maxarg(0, 1), t.cur.y) |
| 87 |
case 'E': // CNL - cursor <n> down and first col |
| 88 |
t.moveTo(0, t.cur.y+c.arg(0, 1)) |
| 89 |
case 'F': // CPL - cursor <n> up and first col |
| 90 |
t.moveTo(0, t.cur.y-c.arg(0, 1)) |
| 91 |
case 'g': // TBC - tabulation clear |
| 92 |
switch c.arg(0, 0) { |
| 93 |
// clear current tab stop |
| 94 |
case 0: |
| 95 |
t.tabs[t.cur.x] = false |
| 96 |
// clear all tabs |
| 97 |
case 3: |
| 98 |
for i := range t.tabs { |
| 99 |
t.tabs[i] = false |
| 100 |
} |
| 101 |
default: |
| 102 |
goto unknown |
| 103 |
} |
| 104 |
case 'G', '`': // CHA, HPA - Move to <col> |
| 105 |
t.moveTo(c.arg(0, 1)-1, t.cur.y) |
| 106 |
case 'H', 'f': // CUP, HVP - move to <row> <col> |
| 107 |
t.moveAbsTo(c.arg(1, 1)-1, c.arg(0, 1)-1) |
| 108 |
case 'I': // CHT - cursor forward tabulation <n> tab stops |
| 109 |
n := c.arg(0, 1) |
| 110 |
for i := 0; i < n; i++ { |
| 111 |
t.putTab(true) |
| 112 |
} |
| 113 |
case 'J': // ED - clear screen |
| 114 |
// TODO: sel.ob.x = -1 |
| 115 |
switch c.arg(0, 0) { |
| 116 |
case 0: // below |
| 117 |
t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) |
| 118 |
if t.cur.y < t.rows-1 { |
| 119 |
t.clear(0, t.cur.y+1, t.cols-1, t.rows-1) |
| 120 |
} |
| 121 |
case 1: // above |
| 122 |
if t.cur.y > 1 { |
| 123 |
t.clear(0, 0, t.cols-1, t.cur.y-1) |
| 124 |
} |
| 125 |
t.clear(0, t.cur.y, t.cur.x, t.cur.y) |
| 126 |
case 2: // all |
| 127 |
t.clear(0, 0, t.cols-1, t.rows-1) |
| 128 |
default: |
| 129 |
goto unknown |
| 130 |
} |
| 131 |
case 'K': // EL - clear line |
| 132 |
switch c.arg(0, 0) { |
| 133 |
case 0: // right |
| 134 |
t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) |
| 135 |
case 1: // left |
| 136 |
t.clear(0, t.cur.y, t.cur.x, t.cur.y) |
| 137 |
case 2: // all |
| 138 |
t.clear(0, t.cur.y, t.cols-1, t.cur.y) |
| 139 |
} |
| 140 |
case 'S': // SU - scroll <n> lines up |
| 141 |
t.ScrollUp(t.top, c.arg(0, 1)) |
| 142 |
case 'T': // SD - scroll <n> lines down |
| 143 |
t.ScrollDown(t.top, c.arg(0, 1)) |
| 144 |
case 'L': // IL - insert <n> blank lines |
| 145 |
t.insertBlankLines(c.arg(0, 1)) |
| 146 |
case 'l': // RM - reset mode |
| 147 |
t.setMode(c.priv, false, c.args) |
| 148 |
case 'M': // DL - delete <n> lines |
| 149 |
t.deleteLines(c.arg(0, 1)) |
| 150 |
case 'X': // ECH - erase <n> chars |
| 151 |
t.clear(t.cur.x, t.cur.y, t.cur.x+c.arg(0, 1)-1, t.cur.y) |
| 152 |
case 'P': // DCH - delete <n> chars |
| 153 |
t.deleteChars(c.arg(0, 1)) |
| 154 |
case 'Z': // CBT - cursor backward tabulation <n> tab stops |
| 155 |
n := c.arg(0, 1) |
| 156 |
for i := 0; i < n; i++ { |
| 157 |
t.putTab(false) |
| 158 |
} |
| 159 |
case 'd': // VPA - move to <row> |
| 160 |
t.moveAbsTo(t.cur.x, c.arg(0, 1)-1) |
| 161 |
case 'h': // SM - set terminal mode |
| 162 |
t.setMode(c.priv, true, c.args) |
| 163 |
case 'm': // SGR - terminal attribute (color) |
| 164 |
t.setAttr(c.args) |
| 165 |
case 'r': // DECSTBM - set scrolling region |
| 166 |
if c.priv { |
| 167 |
goto unknown |
| 168 |
} else { |
| 169 |
t.setScroll(c.arg(0, 1)-1, c.arg(1, t.rows)-1) |
| 170 |
t.moveAbsTo(0, 0) |
| 171 |
} |
| 172 |
case 's': // DECSC - save cursor position (ANSI.SYS) |
| 173 |
t.saveCursor() |
| 174 |
case 'u': // DECRC - restore cursor position (ANSI.SYS) |
| 175 |
t.restoreCursor() |
| 176 |
} |
| 177 |
return |
| 178 |
unknown: // TODO: get rid of this goto |
| 179 |
t.logf("unknown CSI sequence '%c'\n", c.mode) |
| 180 |
// TODO: c.dump() |
| 181 |
} |