package termstate import ( "fmt" "strings" ) // dump describes everything a snapshot restores, normalised where two // states look and behave the same: a never-written cell and a written space // of the same pen, trailing blanks, a soft wrap into an empty row, and the // content of the screen not in use. func dump(t *Terminal) string { var b strings.Builder fmt.Fprintf(&b, "size %dx%d alt=%v region=%d-%d title=%q shape=%d\n", t.cols, t.rows, t.scr == &t.alt, t.top, t.bottom, t.title, t.cursorShape) c := t.cur fmt.Fprintf(&b, "cursor %d,%d wrap=%v origin=%v pen=%+v charsets=%c%c shift=%d\n", c.x, c.y, c.wrap, c.origin, c.pen, c.charsets[0], c.charsets[1], c.shift) fmt.Fprintf(&b, "modes %+v\n", t.m) for _, s := range []*screen{&t.main, &t.alt} { if s.savedValid { fmt.Fprintf(&b, "saved %d,%d pen=%+v\n", s.saved.x, s.saved.y, s.saved.pen) } else { b.WriteString("saved none\n") } } fmt.Fprintf(&b, "tabs %v\n", t.tabs) var main []*line for i := 0; i < t.scrollbackLen(); i++ { main = append(main, t.scrollbackLine(i)) } fmt.Fprintf(&b, "scrollback %d\n", t.scrollbackLen()) main = append(main, t.main.lines...) for i, l := range main { cont := l.cont && i+1 < len(main) && !blankLine(main[i+1]) b.WriteString(dumpLine(l, cont)) } { b.WriteString("alt\n") for _, l := range t.alt.lines { b.WriteString(dumpLine(l, false)) } } return b.String() } func dumpLine(l *line, cont bool) string { cells := make([]Cell, len(l.cells)) copy(cells, l.cells) for i := range cells { if cells[i].R == 0 && cells[i].Kind == cellNarrow { cells[i].R = ' ' } } for len(cells) > 0 && cells[len(cells)-1] == (Cell{R: ' '}) { cells = cells[:len(cells)-1] } var b strings.Builder if cont { b.WriteString("+") } pen := Attr{} for x, c := range cells { if c.A != pen { fmt.Fprintf(&b, "{%x/%x/%x}", c.A.Fg, c.A.Bg, c.A.Flags) pen = c.A } switch c.Kind { case cellWideTail: b.WriteString("~") default: b.WriteRune(c.R) } if m := l.marks[x]; m != "" { b.WriteString("<" + m + ">") } } b.WriteString("|\n") return b.String() } func text(t *Terminal) string { return t.Text() } // history is the scrollback's text, oldest first. func history(t *Terminal) []string { var lines []string for i := 0; i < t.scrollbackLen(); i++ { var b strings.Builder for _, c := range t.scrollbackLine(i).cells { if c.R != 0 && c.Kind != cellWideTail { b.WriteRune(c.R) } } lines = append(lines, b.String()) } return lines } // blankLine: nothing but spaces and never-written cells of the default pen. func blankLine(l *line) bool { for _, c := range l.cells { if c != (Cell{}) && c != (Cell{R: ' '}) { return false } } return len(l.marks) == 0 }