package server import ( "strconv" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" ) // chatShellPlainText renders a raw terminal stream as plain text, as Copy // output and Send to agent show it: escape sequences (colours, cursor moves, // titles, hyperlinks) are removed, a carriage return or backspace redraws the // current line the way a terminal would, and the line erase is applied. It is // not a terminal emulator: full-screen programs render as their final lines. // The result is bounded by protocol.ChatShellRunRetained bytes (its tail). func chatShellPlainText(raw string) string { var r chatShellPlainRenderer for _, c := range raw { r.feed(c) } r.out.WriteString(strings.TrimRight(string(r.line), " ")) text := r.out.String() if len(text) > protocol.ChatShellRunRetained { cut := runeStartAfter([]byte(text), len(text)-protocol.ChatShellRunRetained) text = text[cut:] } return text } type chatShellPlainState int const ( plainText chatShellPlainState = iota plainEscape plainEscapeIntermediate plainCSI // plainString is an OSC, DCS, SOS, PM or APC string, ended by BEL or ST. plainString plainStringEscape ) type chatShellPlainRenderer struct { out strings.Builder line []rune col int state chatShellPlainState params []rune } func (r *chatShellPlainRenderer) feed(c rune) { switch r.state { case plainEscape: r.state = plainText switch { case c == '[': r.state, r.params = plainCSI, r.params[:0] case c == ']' || c == 'P' || c == 'X' || c == '^' || c == '_': r.state = plainString case c >= 0x20 && c <= 0x2f: r.state = plainEscapeIntermediate } case plainEscapeIntermediate: if c < 0x20 || c > 0x2f { r.state = plainText } case plainCSI: switch { case c >= 0x40 && c <= 0x7e: r.state = plainText r.csi(c) case c >= 0x20 && c <= 0x3f: if len(r.params) < 32 { r.params = append(r.params, c) } default: r.state = plainText } case plainString: switch c { case 0x07: r.state = plainText case 0x1b: r.state = plainStringEscape } case plainStringEscape: r.state = plainText default: r.text(c) } } func (r *chatShellPlainRenderer) text(c rune) { switch { case c == 0x1b: r.state = plainEscape case c == 0x9b: r.state, r.params = plainCSI, r.params[:0] case c == '\n': r.out.WriteString(strings.TrimRight(string(r.line), " ")) r.out.WriteByte('\n') r.line, r.col = r.line[:0], 0 case c == '\r': r.col = 0 case c == '\b': r.col = max(r.col-1, 0) case c == '\t': r.put('\t') case c < 0x20 || (c >= 0x7f && c < 0xa0): // Other control characters draw nothing. default: r.put(c) } } func (r *chatShellPlainRenderer) put(c rune) { for len(r.line) < r.col { r.line = append(r.line, ' ') } if r.col < len(r.line) { r.line[r.col] = c } else { r.line = append(r.line, c) } r.col++ } // csi applies the few control sequences that change a line's text. func (r *chatShellPlainRenderer) csi(final rune) { params := string(r.params) n, err := strconv.Atoi(params) if err != nil || n < 1 { n = 1 } switch final { case 'K': switch params { case "", "0": r.line = r.line[:min(r.col, len(r.line))] case "2": r.line = r.line[:0] } case 'G': r.col = min(n-1, protocol.ChatShellTerminalMaxCols) case 'C': r.col = min(r.col+n, protocol.ChatShellTerminalMaxCols) case 'D': r.col = max(r.col-n, 0) } }