Skip to content

Commit 77e1b40

Browse files
authored
feat: support PM and SOS sequences (#31)
Add support for PM (Privacy Message) and SOS (Start of String) as defined in ECMA-48. Signed-off-by: Michał Kotowski <dev@mkotowski.dev>
1 parent 59905af commit 77e1b40

5 files changed

Lines changed: 71 additions & 10 deletions

File tree

main.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ func process(w *colorprofile.Writer, in []byte) error {
100100
// Remove only if not a literal bell
101101
s = strings.TrimSuffix(s, "\\a")
102102
}
103+
// SOS
104+
s = strings.TrimPrefix(s, "\\x98")
105+
s = strings.TrimPrefix(s, "\\x1bX")
106+
// PM
107+
s = strings.TrimPrefix(s, "\\x9e")
108+
s = strings.TrimPrefix(s, "\\x1b^")
103109
// APC
104110
s = strings.TrimPrefix(s, "\\x9f")
105111
s = strings.TrimPrefix(s, "\\x1b_")
@@ -115,19 +121,52 @@ func process(w *colorprofile.Writer, in []byte) error {
115121
}
116122
s = strings.TrimSuffix(s, "\\x1b\\\\")
117123

118-
_, _ = fmt.Fprintf(
119-
w,
120-
"%s%s%s",
121-
t.kindStyle(kind),
122-
t.sequence.Render(s),
123-
t.separator,
124-
)
124+
_, _ = fmt.Fprintf(w, "%s", t.kindStyle(kind))
125125

126126
switch kind {
127127
case "Ctrl":
128-
_, _ = fmt.Fprintln(w, t.explanation.Render(ctrlCodes[seq[0]]))
128+
_, _ = fmt.Fprintf(
129+
w,
130+
"%s%s%s\n",
131+
t.sequence.Render(s),
132+
t.separator,
133+
t.explanation.Render(ctrlCodes[seq[0]]),
134+
)
135+
136+
case "PM":
137+
_, _ = fmt.Fprintf(
138+
w,
139+
"%s%s\n",
140+
t.separator,
141+
t.explanation.Render(fmt.Sprintf("Privacy message %q", s)),
142+
)
143+
144+
case "SOS":
145+
_, _ = fmt.Fprintf(
146+
w,
147+
"%s%s\n",
148+
t.separator,
149+
t.explanation.Render(fmt.Sprintf("Control string %q", s)),
150+
)
151+
129152
case "":
130-
_, _ = fmt.Fprintf(w, "Unknown %q\n", seq)
153+
_, _ = fmt.Fprintf(
154+
w,
155+
"%s%sUnknown %q\n",
156+
t.sequence.Render(s),
157+
t.separator,
158+
seq,
159+
)
160+
161+
default:
162+
// For sequences with own handlers print only the kind and the sequence.
163+
// Explanation will be provided by handlers themselves:
164+
_, _ = fmt.Fprintf(
165+
w,
166+
"%s%s",
167+
t.sequence.Render(s),
168+
t.separator,
169+
)
131170
}
132171
}
133172

@@ -185,6 +224,14 @@ func process(w *colorprofile.Writer, in []byte) error {
185224
seqPrint("OSC", seq)
186225
handle(oscHandlers, p)
187226

227+
case ansi.HasPmPrefix(seq):
228+
flushPrint()
229+
seqPrint("PM", seq)
230+
231+
case ansi.HasSosPrefix(seq):
232+
flushPrint()
233+
seqPrint("SOS", seq)
234+
188235
case ansi.HasApcPrefix(seq):
189236
flushPrint()
190237
seqPrint("APC", seq)

main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ var others = map[string]string{
168168
"esc": fmt.Sprintf("%c", ansi.ESC),
169169
"file sep": fmt.Sprintf("%c", ansi.FS),
170170
"apc": "\x1b_Hello World\x1b\\",
171+
"pm": "\x1b^Hello World\x1b\\",
172+
"sos": "\x1bXHello World\x1b\\",
171173
}
172174

173175
var sgr = map[string]string{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PM : Privacy message "Hello World"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOS : Control string "Hello World"

theme.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type theme struct {
1919
explanation lipgloss.Style
2020

2121
kindColors struct {
22-
apc, csi, ctrl, dcs, esc, osc, text color.Color
22+
apc, csi, ctrl, dcs, esc, osc, pm, sos, text color.Color
2323
}
2424
}
2525

@@ -37,6 +37,8 @@ func (t theme) kindStyle(kind string) lipgloss.Style {
3737
"dcs": base.Foreground(t.kindColors.dcs),
3838
"esc": base.Foreground(t.kindColors.esc),
3939
"osc": base.Foreground(t.kindColors.osc),
40+
"pm": base.Foreground(t.kindColors.pm),
41+
"sos": base.Foreground(t.kindColors.sos),
4042
"text": base.Foreground(t.kindColors.text),
4143
}[kind]
4244

@@ -53,6 +55,10 @@ func (t theme) kindStyle(kind string) lipgloss.Style {
5355
return s.SetString("OSC")
5456
case "apc":
5557
return s.SetString("APC")
58+
case "pm":
59+
return s.SetString("PM")
60+
case "sos":
61+
return s.SetString("SOS")
5662
case "esc":
5763
return s.SetString("ESC")
5864
case "ctrl":
@@ -94,6 +100,8 @@ func charmTheme(hasDarkBG bool) (t theme) {
94100
t.kindColors.dcs = lightDark("#86C867", "#CEE88A")
95101
t.kindColors.esc = lipgloss.Color("#E46FDD")
96102
t.kindColors.osc = lightDark("#43C7E0", "#1CD4F7")
103+
t.kindColors.pm = lightDark("#FF8383", "#DC7272")
104+
t.kindColors.sos = lightDark("#978692", "#6C6068")
97105
t.kindColors.text = lightDark("#978692", "#6C6068")
98106

99107
return t
@@ -114,6 +122,8 @@ func base16Theme(_ bool) theme {
114122
t.kindColors.dcs = lipgloss.Yellow
115123
t.kindColors.esc = lipgloss.Magenta
116124
t.kindColors.osc = lipgloss.Cyan
125+
t.kindColors.pm = lipgloss.BrightRed
126+
t.kindColors.sos = lipgloss.BrightBlack
117127
t.kindColors.text = lipgloss.BrightBlack
118128

119129
return t

0 commit comments

Comments
 (0)