package termstate import "strconv" // Snapshot returns an escape sequence stream that brings a terminal of the // same size, from any state, to this one: it resets the terminal (RIS and // ED 3), draws the scrollback and the main screen row by row (soft-wrapped // rows wrap again), switches to the alternate screen and draws it when that // is in use, then restores the saved cursors, tab stops, scroll region, // cursor (including a pending wrap), modes, character sets, pen and title. // It ends with the start of a sequence still unfinished in the output, so // the bytes written next complete it. // // partial is how many of the newest bytes written are an unfinished UTF-8 // character, which the stream leaves out: the caller sends those bytes again // after it. func (t *Terminal) Snapshot() (out []byte, partial int) { w := &snapWriter{t: t} w.raw("\x1bc\x1b[3J") if t.title != "" { w.raw("\x1b]2;" + t.title + "\x07") } if t.scr != &t.alt && (t.alt.savedValid || !emptyRows(t.alt.lines)) { // The alternate screen not in use: what a switch with 47 or 1047 // (which do not clear it) would show again. w.raw("\x1b[?47h") w.altScreen() if t.alt.savedValid { w.saved(&t.alt, "\x1b7") } w.raw("\x1b[?47l\x1b[H") } w.mainScreen() if t.scr == &t.alt { if t.main.savedValid { // 1049 saves the main screen's cursor as it switches. w.saved(&t.main, "\x1b[?1049h") } else { // Nothing saved: switch without saving (the reset cleared it). w.raw("\x1b[?47h") } w.altScreen() if t.alt.savedValid { w.saved(&t.alt, "\x1b7") } } else if t.main.savedValid { w.saved(&t.main, "\x1b7") } w.tabs() if t.top != 0 || t.bottom != t.rows-1 { w.raw("\x1b[" + strconv.Itoa(t.top+1) + ";" + strconv.Itoa(t.bottom+1) + "r") } w.cursor() w.modes() w.pen(t.cur.pen) seq, partial := t.p.pending() w.b = append(w.b, seq...) return w.b, partial } type snapWriter struct { t *Terminal b []byte cur Attr } func (w *snapWriter) raw(s string) { w.b = append(w.b, s...) } func (w *snapWriter) num(n int) { w.b = strconv.AppendInt(w.b, int64(n), 10) } // goTo is CUP to row, col (zero-based, absolute). func (w *snapWriter) goTo(row, col int) { w.raw("\x1b[") w.num(row + 1) w.b = append(w.b, ';') w.num(col + 1) w.b = append(w.b, 'H') } // mainScreen writes the scrollback then the main screen's rows, each after // a line break unless the row before wrapped into it. func (w *snapWriter) mainScreen() { t := w.t n := t.scrollbackLen() total := n + len(t.main.lines) at := func(i int) *line { if i < n { return t.scrollbackLine(i) } return t.main.lines[i-n] } for i := 0; i < total; i++ { l := at(i) wraps := l.cont && i+1 < total w.row(l, wraps) if wraps && at(i+1).empty() { // Wrap into the empty row with a space, then erase it again. w.pen(Attr{}) w.raw(" \b\x1b[K") } if !wraps && i+1 < total { if w.cur.Bg != 0 { // A scrolled-in row takes the pen's background. w.pen(Attr{}) } w.raw("\r\n") } } } // altScreen draws each row of the alternate screen at its position. func (w *snapWriter) altScreen() { for y, l := range w.t.alt.lines { if len(l.cells) == 0 { continue } w.goTo(y, 0) w.row(l, false) } } // row writes l's cells from the cursor at its start; full writes every // column (blank ones as spaces), so the next character wraps. func (w *snapWriter) row(l *line, full bool) { n := len(l.cells) if full { n = w.t.cols } else { for n > 0 && l.cells[n-1] == (Cell{}) { n-- } } for x := 0; x < n; x++ { var c Cell if x < len(l.cells) { c = l.cells[x] } if c.Kind == cellWideTail { continue } w.cell(l, x, c) } } func (w *snapWriter) cell(l *line, x int, c Cell) { w.pen(c.A) if c.R == 0 { w.b = append(w.b, ' ') } else { w.b = append(w.b, string(c.R)...) } if l.marks != nil { w.b = append(w.b, l.marks[x]...) } } // saved places the cursor, pen and character sets of a saved cursor of // screen s and writes then (which saves them), then sets ASCII again for // what follows. (Origin mode saved with a cursor is not restored.) func (w *snapWriter) saved(s *screen, then string) { c := s.saved w.place(s.lines[c.y], c.y, c.x, c.wrap, false) w.pen(c.pen) w.charsets(c) w.raw(then) if c.charsets != [2]byte{'B', 'B'} || c.shift != 0 { w.raw("\x1b(B\x1b)B\x0f") } } func (w *snapWriter) charsets(c cursor) { if g := c.charsets[0]; g != 'B' { w.raw("\x1b(" + string(rune(g))) } if g := c.charsets[1]; g != 'B' { w.raw("\x1b)" + string(rune(g))) } if c.shift == 1 { w.raw("\x0e") } } // tabs sets the tab stops when they are not the default ones. func (w *snapWriter) tabs() { t := w.t custom := false for x, set := range t.tabs { if set != (x > 0 && x%8 == 0) { custom = true break } } if !custom { return } w.raw("\x1b[3g") for x, set := range t.tabs { if set { w.goTo(0, x) w.raw("\x1bH") } } } // cursor places the cursor (in origin mode, after setting it). func (w *snapWriter) cursor() { t := w.t row := t.cur.y if t.cur.origin { w.raw("\x1b[?6h") row -= t.top } w.place(t.scr.lines[t.cur.y], row, t.cur.x, t.cur.wrap, true) } // place moves the cursor to column x of row (a CUP row) showing l. A // pending wrap is recreated by writing the last column's cell again; with // repeat, the cell before the cursor is written again when it holds the // last character written, so a repeat (REP) that follows repeats it. func (w *snapWriter) place(l *line, row, x int, wrap, repeat bool) { t := w.t at := func(x int) Cell { if x < len(l.cells) { return l.cells[x] } return Cell{} } if wrap { x = t.cols - 1 if at(x).Kind == cellWideTail && x > 0 { x-- } w.goTo(row, x) w.cell(l, x, at(x)) return } last := x - 1 if last > 0 && at(last).Kind == cellWideTail { last-- } c := at(max(last, 0)) if !repeat || last < 0 || c.R == 0 || c.R != t.lastChar || (c.Kind == cellWide && last+2 != x) { w.goTo(row, x) return } w.goTo(row, last) w.cell(l, last, c) } func (w *snapWriter) modes() { t := w.t m := t.m flag := func(on bool, s string) { if on { w.raw(s) } } flag(!m.autowrap, "\x1b[?7l") flag(m.insert, "\x1b[4h") flag(m.newline, "\x1b[20h") flag(m.appCursor, "\x1b[?1h") flag(m.appKeypad, "\x1b=") flag(m.reverse, "\x1b[?5h") flag(m.cursorBlink, "\x1b[?12h") flag(!m.cursorVisible, "\x1b[?25l") for _, mode := range []int{m.mouse, m.mouseEncoding} { if mode != 0 { w.raw("\x1b[?") w.num(mode) w.b = append(w.b, 'h') } } flag(m.focus, "\x1b[?1004h") flag(m.bracketedPaste, "\x1b[?2004h") flag(m.altScroll, "\x1b[?1007h") if t.cursorShape != 0 { w.raw("\x1b[") w.num(t.cursorShape) w.raw(" q") } w.charsets(t.cur) } // pen switches the written pen to a. func (w *snapWriter) pen(a Attr) { if a == w.cur { return } w.cur = a w.raw("\x1b[0") flags := []struct { bit uint16 code string }{ {attrBold, ";1"}, {attrFaint, ";2"}, {attrItalic, ";3"}, {attrBlink, ";5"}, {attrInverse, ";7"}, {attrHidden, ";8"}, {attrStrike, ";9"}, {attrOverline, ";53"}, } for _, f := range flags { if a.Flags&f.bit != 0 { w.raw(f.code) } } switch u := a.underline(); u { case 0: case 1: w.raw(";4") default: w.raw(";4:") w.num(u) } w.color(a.Fg, 30, 90, "38") w.color(a.Bg, 40, 100, "48") w.b = append(w.b, 'm') } func (w *snapWriter) color(c Color, base, bright int, extended string) { switch c & colorKind { case colorIndexed: n := int(c & 0xFF) w.b = append(w.b, ';') switch { case n < 8: w.num(base + n) case n < 16: w.num(bright + n - 8) default: w.raw(extended + ";5;") w.num(n) } case colorRGB: w.raw(";" + extended + ";2;") w.num(int(c>>16) & 0xFF) w.b = append(w.b, ';') w.num(int(c>>8) & 0xFF) w.b = append(w.b, ';') w.num(int(c) & 0xFF) } } func emptyRows(lines []*line) bool { for _, l := range lines { if len(l.cells) > 0 { return false } } return true }