main
1package ansi
2
3import (
4 "bytes"
5)
6
7// ScreenPassthrough wraps the given ANSI sequence in a DCS passthrough
8// sequence to be sent to the outer terminal. This is used to send raw escape
9// sequences to the outer terminal when running inside GNU Screen.
10//
11// DCS <data> ST
12//
13// Note: Screen limits the length of string sequences to 768 bytes (since 2014).
14// Use zero to indicate no limit, otherwise, this will chunk the returned
15// string into limit sized chunks.
16//
17// See: https://www.gnu.org/software/screen/manual/screen.html#String-Escapes
18// See: https://git.savannah.gnu.org/cgit/screen.git/tree/src/screen.h?id=c184c6ec27683ff1a860c45be5cf520d896fd2ef#n44
19func ScreenPassthrough(seq string, limit int) string {
20 var b bytes.Buffer
21 b.WriteString("\x1bP")
22 if limit > 0 {
23 for i := 0; i < len(seq); i += limit {
24 end := i + limit
25 if end > len(seq) {
26 end = len(seq)
27 }
28 b.WriteString(seq[i:end])
29 if end < len(seq) {
30 b.WriteString("\x1b\\\x1bP")
31 }
32 }
33 } else {
34 b.WriteString(seq)
35 }
36 b.WriteString("\x1b\\")
37 return b.String()
38}
39
40// TmuxPassthrough wraps the given ANSI sequence in a special DCS passthrough
41// sequence to be sent to the outer terminal. This is used to send raw escape
42// sequences to the outer terminal when running inside Tmux.
43//
44// DCS tmux ; <escaped-data> ST
45//
46// Where <escaped-data> is the given sequence in which all occurrences of ESC
47// (0x1b) are doubled i.e. replaced with ESC ESC (0x1b 0x1b).
48//
49// Note: this needs the `allow-passthrough` option to be set to `on`.
50//
51// See: https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it
52func TmuxPassthrough(seq string) string {
53 var b bytes.Buffer
54 b.WriteString("\x1bPtmux;")
55 for i := 0; i < len(seq); i++ {
56 if seq[i] == ESC {
57 b.WriteByte(ESC)
58 }
59 b.WriteByte(seq[i])
60 }
61 b.WriteString("\x1b\\")
62 return b.String()
63}