Skip to content

Commit

Permalink
fix: more tests, bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Oct 30, 2024
1 parent 2d7a55a commit e4dc3cd
Show file tree
Hide file tree
Showing 40 changed files with 147 additions and 104 deletions.
23 changes: 9 additions & 14 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,27 @@ func handleCursor(p *ansi.Parser) (string, error) {
count = ansi.Param(p.Params[0]).Param()
}

if count == 0 {
// Default value is 1
count = 1
}

cmd := ansi.Cmd(p.Cmd)
isPrivate := cmd.Marker() == '?'
switch cmd.Command() {
case 'A':
// CUU - Cursor Up
return fmt.Sprintf("Cursor up %d", count), nil
return fmt.Sprintf("Cursor up %d", default1(count)), nil
case 'B':
// CUD - Cursor Down
return fmt.Sprintf("Cursor down %d", count), nil
return fmt.Sprintf("Cursor down %d", default1(count)), nil
case 'C':
// CUF - Cursor Forward
return fmt.Sprintf("Cursor right %d", count), nil
return fmt.Sprintf("Cursor right %d", default1(count)), nil
case 'D':
// CUB - Cursor Back
return fmt.Sprintf("Cursor left %d", count), nil
return fmt.Sprintf("Cursor left %d", default1(count)), nil
case 'E':
return fmt.Sprintf("Cursor next line %d", count), nil
return fmt.Sprintf("Cursor next line %d", default1(count)), nil
case 'F':
return fmt.Sprintf("Cursor previous line %d", count), nil
return fmt.Sprintf("Cursor previous line %d", default1(count)), nil
case 'H':
row := count
row := default1(count)
col := 1
if p.ParamsLen > 1 {
col = p.Params[1]
Expand All @@ -56,14 +51,14 @@ func handleCursor(p *ansi.Parser) (string, error) {
case 'u':
return "Restore cursor position", nil
case 'q':
return fmt.Sprintf("Set cursor style %s", descCursorStyle(count)), nil
return fmt.Sprintf("Set cursor style %s", descCursorStyle(default1(count))), nil
}
return "", errUnhandled
}

func descCursorStyle(i int) string {
switch i {
case 1:
case 0, 1:
return "Blinking block"
case 2:

Check failure on line 63 in cursor.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 2, in <case> detected (mnd)

Check failure on line 63 in cursor.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 2, in <case> detected (mnd)

Check failure on line 63 in cursor.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 2, in <case> detected (mnd)
return "Steady block"
Expand Down
7 changes: 7 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ func printf(format string, v ...any) handlerFn {
return fmt.Sprintf(format, v...), nil
}
}

func default1(i int) int {
if i == 0 {
return 1
}
return i
}
19 changes: 7 additions & 12 deletions line.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,25 @@ func handleLine(parser *ansi.Parser) (string, error) {
count = ansi.Param(parser.Params[0]).Param()
}

if count == 0 {
// Default value is 1
count = 1
}

cmd := ansi.Cmd(parser.Cmd)
switch cmd.Command() {
case 'K':
switch count {
case 1:
case 0:
return "Erase line right", nil
case 2:
case 1:
return "Erase line left", nil
case 3:
case 2:
return "Erase entire line", nil
}
case 'L':
return fmt.Sprintf("CSI %d L: Insert %[1]d blank lines", count), nil
return fmt.Sprintf("Insert %d blank lines", default1(count)), nil
case 'M':
return fmt.Sprintf("CSI %d M: Delete %[1]d lines", count), nil
return fmt.Sprintf("Delete %d lines", default1(count)), nil
case 'S':
return fmt.Sprintf("CSI %d S: Scroll up %[1]d lines", count), nil
return fmt.Sprintf("Scroll up %d lines", default1(count)), nil
case 'T':
return fmt.Sprintf("CSI %d T: Scroll down %[1]d lines", count), nil
return fmt.Sprintf("Scroll down %d lines", default1(count)), nil
}
return "", errUnhandled
}
111 changes: 66 additions & 45 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/hex"
"strings"
"testing"

Expand All @@ -12,33 +13,35 @@ import (

var cursor = map[string]string{
// cursor
"save": ansi.SaveCursor,
"restore": ansi.RestoreCursor,
"request pos": ansi.RequestCursorPosition,
"request cursor pos": ansi.RequestExtendedCursorPosition,
"up 1": ansi.CursorUp1,
"up": ansi.CursorUp(5),
"down 1": ansi.CursorDown1,
"down": ansi.CursorDown(3),
"right 1": ansi.CursorRight1,
"right": ansi.CursorRight(3),
"left 1": ansi.CursorLeft1,
"left": ansi.CursorLeft(3),
"next line": ansi.CursorNextLine(3),
"previous line": ansi.CursorPreviousLine(3),
"set pos": ansi.SetCursorPosition(10, 20),
"origin": ansi.CursorOrigin,
"save pos": ansi.SaveCursorPosition,
"restore pos": ansi.RestoreCursorPosition,
"style 0": ansi.SetCursorStyle(0),
"style 1": ansi.SetCursorStyle(1),
"style 2": ansi.SetCursorStyle(2),
"style 3": ansi.SetCursorStyle(3),
"style 4": ansi.SetCursorStyle(4),
"style 5": ansi.SetCursorStyle(5),
"style 6": ansi.SetCursorStyle(6),
"style 7": ansi.SetCursorStyle(7),
"pointer shape": ansi.SetPointerShape("crosshair"),
"save": ansi.SaveCursor,
"restore": ansi.RestoreCursor,
"request pos": ansi.RequestCursorPosition,
"request extended pos": ansi.RequestExtendedCursorPosition,
"invalid request extended pos": strings.Replace(ansi.RequestExtendedCursorPosition, "6", "7", 1),
"up 1": ansi.CursorUp1,
"up": ansi.CursorUp(5),
"down 1": ansi.CursorDown1,
"down": ansi.CursorDown(3),
"right 1": ansi.CursorRight1,
"right": ansi.CursorRight(3),
"left 1": ansi.CursorLeft1,
"left": ansi.CursorLeft(3),
"next line": ansi.CursorNextLine(3),
"previous line": ansi.CursorPreviousLine(3),
"set pos": ansi.SetCursorPosition(10, 20),
"origin": ansi.CursorOrigin,
"save pos": ansi.SaveCursorPosition,
"restore pos": ansi.RestoreCursorPosition,
"style 0": ansi.SetCursorStyle(0),
"style 1": ansi.SetCursorStyle(1),
"style 2": ansi.SetCursorStyle(2),
"style 3": ansi.SetCursorStyle(3),
"style 4": ansi.SetCursorStyle(4),
"style 5": ansi.SetCursorStyle(5),
"style 6": ansi.SetCursorStyle(6),
"style 7": ansi.SetCursorStyle(7),
"pointer shape": ansi.SetPointerShape("crosshair"),
"invalid pointer shape": strings.Replace(ansi.SetPointerShape(""), ";", "", 1),
}

var screen = map[string]string{
Expand Down Expand Up @@ -70,6 +73,9 @@ var mode = map[string]string{
"enable cursor visibility": ansi.ShowCursor,
"disable cursor visibility": ansi.HideCursor,
"request cursor visibility": ansi.RequestCursorVisibility,
"enable mouse": ansi.EnableMouse,
"disable mouse": ansi.DisableMouse,
"request mouse": ansi.RequestMouse,
"enable mouse hilite": ansi.EnableMouseHilite,
"disable mouse hilite": ansi.DisableMouseHilite,
"request mouse hilite": ansi.RequestMouseHilite,
Expand Down Expand Up @@ -100,26 +106,32 @@ var mode = map[string]string{
"enable win32 input": ansi.EnableWin32Input,
"disable win32 input": ansi.DisableWin32Input,
"request win32 input": ansi.RequestWin32Input,
"invalid": strings.Replace(ansi.ShowCursor, "25", "27", 1),
"non private": strings.Replace(ansi.ShowCursor, "?", "", 1),
}

var kitty = map[string]string{
"set all mode 1": ansi.KittyKeyboard(ansi.KittyAllFlags, 1),
"set all mode 2": ansi.KittyKeyboard(ansi.KittyAllFlags, 2),
"set all mode 3": ansi.KittyKeyboard(ansi.KittyAllFlags, 3),
"request": ansi.RequestKittyKeyboard,
"disable": ansi.DisableKittyKeyboard,
"pop": ansi.PopKittyKeyboard(2),
"push 1": ansi.PushKittyKeyboard(1),
"push 2": ansi.PushKittyKeyboard(2),
"push 4": ansi.PushKittyKeyboard(4),
"push 8": ansi.PushKittyKeyboard(8),
"push 16": ansi.PushKittyKeyboard(16),
"set all mode 1": ansi.KittyKeyboard(ansi.KittyAllFlags, 1),
"set all mode 2": ansi.KittyKeyboard(ansi.KittyAllFlags, 2),
"set all mode 3": ansi.KittyKeyboard(ansi.KittyAllFlags, 3),
"set invalid mode": ansi.KittyKeyboard(ansi.KittyAllFlags, 4),
"request": ansi.RequestKittyKeyboard,
"disable": ansi.DisableKittyKeyboard,
"pop": ansi.PopKittyKeyboard(2),
"push 1": ansi.PushKittyKeyboard(1),
"push 2": ansi.PushKittyKeyboard(2),
"push 4": ansi.PushKittyKeyboard(4),
"push 8": ansi.PushKittyKeyboard(8),
"push 16": ansi.PushKittyKeyboard(16),
}

var others = map[string]string{
"request primary device attrs": ansi.RequestPrimaryDeviceAttributes,
"request xt version": ansi.RequestXTVersion,
"termcap": ansi.RequestTermcap("bw", "ccc"),
"invalid termcap": strings.Replace(ansi.RequestTermcap("a"), hex.EncodeToString([]byte("a")), "", 1),
"invalid termcap hex": strings.Replace(ansi.RequestTermcap("a"), hex.EncodeToString([]byte("a")), "a", 1),
"invalid xt": strings.Replace(ansi.RequestXTVersion, "0", "1", 1),
}

var sgr = map[string]string{
Expand All @@ -140,19 +152,24 @@ var sgr = map[string]string{
}

var title = map[string]string{
"set": ansi.SetWindowTitle("hello"),
"set icon": ansi.SetIconName("terminal"),
"set both": ansi.SetIconNameWindowTitle("terminal"),
"set": ansi.SetWindowTitle("hello"),
"set icon": ansi.SetIconName("terminal"),
"set both": ansi.SetIconNameWindowTitle("terminal"),
"invalid": strings.Replace(ansi.SetWindowTitle("hello"), ";hello", "", 1),
"invalid cmd": strings.Replace(ansi.SetWindowTitle("hello"), "2", "5", 1),
}

var hyperlink = map[string]string{
"uri only": ansi.SetHyperlink("https://charm.sh"),
"full": ansi.SetHyperlink("https://charm.sh", "my title"),
"reset": ansi.ResetHyperlink("my title"),
"uri only": ansi.SetHyperlink("https://charm.sh"),
"full": ansi.SetHyperlink("https://charm.sh", "my title"),
"reset": ansi.ResetHyperlink("my title"),
"multiple params": ansi.SetHyperlink("https://charm.sh", "my title", "some description"),
"invalid": strings.Replace(ansi.ResetHyperlink(), ";", "", 1),
}

var notify = map[string]string{
"notify": ansi.Notify("notification body"),
"notify": ansi.Notify("notification body"),
"invalid": strings.Replace(ansi.Notify(""), ";", "", 1),
}

var termcolor = map[string]string{
Expand All @@ -165,13 +182,17 @@ var termcolor = map[string]string{
"reset bg": ansi.ResetBackgroundColor,
"reset fg": ansi.ResetForegroundColor,
"reset cursor": ansi.ResetCursorColor,
"invalid set": strings.Replace(ansi.SetBackgroundColor(ansi.Black), ";", "", 1),
"invalid reset": strings.Replace(ansi.ResetBackgroundColor, "111", "111;1", 1),
}

var clipboard = map[string]string{
"request system": ansi.RequestSystemClipboard,
"request primary": ansi.RequestPrimaryClipboard,
"set system": ansi.SetSystemClipboard("hello"),
"set primary": ansi.SetPrimaryClipboard("hello"),
"incomplete": strings.Replace(ansi.RequestPrimaryClipboard, ";?", "", 1),
"invalid": strings.Replace(ansi.SetPrimaryClipboard("hello"), "=", "", 1),
}

func TestSequences(t *testing.T) {
Expand Down
25 changes: 7 additions & 18 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,20 @@ import (
)

func handleMode(parser *ansi.Parser) (string, error) {
if parser.ParamsLen == 0 {
// Invalid, ignore
return "", errInvalid
}

mode := modeDesc(ansi.Param(parser.Params[0]).Param())
cmd := ansi.Cmd(parser.Cmd)
isPrivate := cmd.Marker() == '?'
private := ""
if cmd.Marker() == '?' {
private = "private "
}
switch cmd.Command() {
case 'p':
// DECRQM - Request Mode
if isPrivate {
return fmt.Sprintf("Request private mode %q", mode), nil
}
return fmt.Sprintf("Request mode %q", mode), nil
return fmt.Sprintf("Request %smode %q", private, mode), nil
case 'h':
if isPrivate {
return fmt.Sprintf("Enable private mode %q", mode), nil
}
return fmt.Sprintf("Enable mode %q", mode), nil
return fmt.Sprintf("Enable %smode %q", private, mode), nil
case 'l':
if isPrivate {
return fmt.Sprintf("Disable private mode %q", mode), nil
}
return fmt.Sprintf("Disable mode %q", mode), nil
return fmt.Sprintf("Disable %smode %q", private, mode), nil
}
return "", errUnhandled
}
Expand Down
4 changes: 0 additions & 4 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func handleScreen(parser *ansi.Parser) (string, error) {
return "Erase entire display", nil
}
case 'r':
if count == 0 {
// Default value is 1
count = 1
}
top := count
bottom := 0
if parser.ParamsLen > 1 {
Expand Down
18 changes: 18 additions & 0 deletions sgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ func handleSgr(parser *ansi.Parser) (string, error) { //nolint:unparam
str += "Invisible"
case 9:
str += "Crossed-out"
case 21:
str += "No bold"
case 22:
str += "Normal intensity"
case 23:
str += "No italic"
case 24:
str += "No underline"
case 25:
str += "No blink"
case 27:
str += "No reverse"
case 28:
str += "No conceal"
case 29:
str += "No crossed-out"
case 30, 31, 32, 33, 34, 35, 36, 37:
str += fmt.Sprintf("Foreground color: %s", basicColors[int(param)-30])
case 38:
Expand All @@ -65,6 +81,8 @@ func handleSgr(parser *ansi.Parser) (string, error) { //nolint:unparam
str += fmt.Sprintf("Background color: %d", readColor(&i, parser.Params))
case 49:
str += "Default background color"
case 58, 59:
str += fmt.Sprintf("Underline color: %d", readColor(&i, parser.Params))
case 90, 91, 92, 93, 94, 95, 96, 97:
str += fmt.Sprintf("Bright foreground color: %s", basicColors[int(param)-90])
case 100, 101, 102, 103, 104, 105, 106, 107:
Expand Down
1 change: 1 addition & 0 deletions testdata/TestSequences/clipboard/incomplete.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC "\x1b]52;p\a": invalid sequence
1 change: 1 addition & 0 deletions testdata/TestSequences/clipboard/invalid.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC "\x1b]52;p;aGVsbG8\a": illegal base64 data at input byte 4
1 change: 1 addition & 0 deletions testdata/TestSequences/cursor/invalid_pointer_shape.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC "\x1b]22\a": invalid sequence
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSI "\x1b[?7n": invalid sequence
1 change: 1 addition & 0 deletions testdata/TestSequences/cursor/request_extended_pos.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSI "\x1b[?6n": Request extended cursor position
1 change: 1 addition & 0 deletions testdata/TestSequences/hyperlink/invalid.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC "\x1b]8;\a": invalid sequence
1 change: 1 addition & 0 deletions testdata/TestSequences/hyperlink/multiple_params.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC "\x1b]8;my title:some description;https://charm.sh\a": Set hyperlink, my title, some description to "https://charm.sh"
1 change: 1 addition & 0 deletions testdata/TestSequences/kitty/set_invalid_flags.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSI "\x1b[=32;4u": Set "" Kitty keyboard flags to "Unknown mode"
1 change: 1 addition & 0 deletions testdata/TestSequences/kitty/set_invalid_mode.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSI "\x1b[=31;4u": Set "Disambiguate escape codes, Report event types, Report alternate keys, Report all keys as escape codes, Report associated text" Kitty keyboard flags to "Unknown mode"
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/delete.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[5M": CSI 5 M: Delete 5 lines
CSI "\x1b[5M": Delete 5 lines
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/entire.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[2K": Erase line left
CSI "\x1b[2K": Erase entire line
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/insert.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[3L": CSI 3 L: Insert 3 blank lines
CSI "\x1b[3L": Insert 3 blank lines
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/left.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[1K": Erase line right
CSI "\x1b[1K": Erase line left
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/scroll_down.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[12T": CSI 12 T: Scroll down 12 lines
CSI "\x1b[12T": Scroll down 12 lines
2 changes: 1 addition & 1 deletion testdata/TestSequences/line/scroll_up.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CSI "\x1b[12S": CSI 12 S: Scroll up 12 lines
CSI "\x1b[12S": Scroll up 12 lines
Loading

0 comments on commit e4dc3cd

Please sign in to comment.