Skip to content

Commit

Permalink
feat: support PM and SOS sequences
Browse files Browse the repository at this point in the history
Add support for PM (Privacy Message) and SOS (Start of String) as
defined in ECMA-48.

Signed-off-by: Michał Kotowski <[email protected]>
  • Loading branch information
mkotowski committed Nov 26, 2024
1 parent 47f744d commit 22d9729
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
65 changes: 56 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func process(w *colorprofile.Writer, in []byte) error {
s = strings.TrimPrefix(s, "\\x9d")
s = strings.TrimPrefix(s, "\\x1b]")
s = strings.TrimSuffix(s, "\\a")
// SOS
s = strings.TrimPrefix(s, "\\x98")
s = strings.TrimPrefix(s, "\\x1bX")
// PM
s = strings.TrimPrefix(s, "\\x9e")
s = strings.TrimPrefix(s, "\\x1b^")
// APC
s = strings.TrimPrefix(s, "\\x9f")
s = strings.TrimPrefix(s, "\\x1b_")
Expand All @@ -108,19 +114,52 @@ func process(w *colorprofile.Writer, in []byte) error {
s = strings.TrimSuffix(s, "\\x9c")
s = strings.TrimSuffix(s, "\\x1b\\\\")

_, _ = fmt.Fprintf(
w,
"%s%s%s",
t.kindStyle(kind),
t.sequence.Render(s),
t.separator,
)
_, _ = fmt.Fprintf(w, "%s", t.kindStyle(kind))

switch kind {
case "Ctrl":
_, _ = fmt.Fprintln(w, t.explanation.Render(ctrlCodes[seq[0]]))
_, _ = fmt.Fprintf(
w,
"%s%s%s\n",
t.sequence.Render(s),
t.separator,
t.explanation.Render(ctrlCodes[seq[0]]),
)

case "PM":
_, _ = fmt.Fprintf(
w,
"%s%s\n",
t.separator,
t.explanation.Render(fmt.Sprintf("Privacy message %q", s)),
)

case "SOS":
_, _ = fmt.Fprintf(
w,
"%s%s\n",
t.separator,
t.explanation.Render(fmt.Sprintf("Control string %q", s)),
)

case "":
_, _ = fmt.Fprintf(w, "Unknown %q\n", seq)
_, _ = fmt.Fprintf(
w,
"%s%sUnknown %q\n",
t.sequence.Render(s),
t.separator,
seq,
)

default:
// For sequences with own handlers print only the kind and the sequence.
// Explanation will be provided by handlers themselves:
_, _ = fmt.Fprintf(
w,
"%s%s",
t.sequence.Render(s),
t.separator,
)
}
}

Expand Down Expand Up @@ -178,6 +217,14 @@ func process(w *colorprofile.Writer, in []byte) error {
seqPrint("OSC", seq)
handle(oscHandlers, p)

case ansi.HasPmPrefix(seq):
flushPrint()
seqPrint("PM", seq)

case ansi.HasSosPrefix(seq):
flushPrint()
seqPrint("SOS", seq)

case ansi.HasApcPrefix(seq):
flushPrint()
seqPrint("APC", seq)
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ var others = map[string]string{
"esc": fmt.Sprintf("%c", ansi.ESC),
"file sep": fmt.Sprintf("%c", ansi.FS),
"apc": "\x1b_Hello World\x1b\\",
"pm": "\x1b^Hello World\x1b\\",
"sos": "\x1bXHello World\x1b\\",
}

var sgr = map[string]string{
Expand Down
1 change: 1 addition & 0 deletions testdata/TestSequences/others/pm.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PM : Privacy message "Hello World"
1 change: 1 addition & 0 deletions testdata/TestSequences/others/sos.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOS : Control string "Hello World"
12 changes: 11 additions & 1 deletion theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type theme struct {
explanation lipgloss.Style

kindColors struct {
apc, csi, ctrl, dcs, esc, osc, text color.Color
apc, csi, ctrl, dcs, esc, osc, pm, sos, text color.Color
}
}

Expand All @@ -37,6 +37,8 @@ func (t theme) kindStyle(kind string) lipgloss.Style {
"dcs": base.Foreground(t.kindColors.dcs),
"esc": base.Foreground(t.kindColors.esc),
"osc": base.Foreground(t.kindColors.osc),
"pm": base.Foreground(t.kindColors.pm),
"sos": base.Foreground(t.kindColors.sos),
"text": base.Foreground(t.kindColors.text),
}[kind]

Expand All @@ -53,6 +55,10 @@ func (t theme) kindStyle(kind string) lipgloss.Style {
return s.SetString("OSC")
case "apc":
return s.SetString("APC")
case "pm":
return s.SetString("PM")
case "sos":
return s.SetString("SOS")
case "esc":
return s.SetString("ESC")
case "ctrl":
Expand Down Expand Up @@ -94,6 +100,8 @@ func charmTheme(hasDarkBG bool) (t theme) {
t.kindColors.dcs = lightDark("#86C867", "#CEE88A")
t.kindColors.esc = lipgloss.Color("#E46FDD")
t.kindColors.osc = lightDark("#43C7E0", "#1CD4F7")
t.kindColors.pm = lightDark("#FF8383", "#DC7272")
t.kindColors.sos = lightDark("#978692", "#6C6068")
t.kindColors.text = lightDark("#978692", "#6C6068")

return t
Expand All @@ -114,6 +122,8 @@ func base16Theme(_ bool) theme {
t.kindColors.dcs = lipgloss.Yellow
t.kindColors.esc = lipgloss.Magenta
t.kindColors.osc = lipgloss.Cyan
t.kindColors.pm = lipgloss.BrightRed
t.kindColors.sos = lipgloss.BrightBlack
t.kindColors.text = lipgloss.BrightBlack

return t
Expand Down

0 comments on commit 22d9729

Please sign in to comment.