Skip to content

Commit

Permalink
refactor(term): move ansi subpackages to ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 6, 2024
1 parent 218372c commit 871d2af
Show file tree
Hide file tree
Showing 27 changed files with 683 additions and 484 deletions.
16 changes: 7 additions & 9 deletions exp/term/ansi/sys/background.go → exp/term/ansi/background.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sys
package ansi

import (
"image/color"

"github.com/charmbracelet/x/exp/term/ansi/internal"
)

// SetForegroundColor returns a sequence that sets the default terminal
Expand All @@ -16,14 +14,14 @@ import (
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
func SetForegroundColor(c color.Color) string {
return "\x1b" + "]" + "10;" + internal.ColorToHexString(c) + "\x07"
return "\x1b]10;" + colorToHexString(c) + "\x07"
}

// RequestForegroundColor is a sequence that requests the current default
// terminal foreground color.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
const RequestForegroundColor = "\x1b" + "]" + "10;" + "?" + "\x07"
const RequestForegroundColor = "\x1b]10;?\x07"

// SetBackgroundColor returns a sequence that sets the default terminal
// background color.
Expand All @@ -35,14 +33,14 @@ const RequestForegroundColor = "\x1b" + "]" + "10;" + "?" + "\x07"
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
func SetBackgroundColor(c color.Color) string {
return "\x1b" + "]" + "11;" + internal.ColorToHexString(c) + "\x07"
return "\x1b]11;" + colorToHexString(c) + "\x07"
}

// RequestBackgroundColor is a sequence that requests the current default
// terminal background color.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
const RequestBackgroundColor = "\x1b" + "]" + "11;" + "?" + "\x07"
const RequestBackgroundColor = "\x1b]11;?\x07"

// SetCursorColor returns a sequence that sets the terminal cursor color.
//
Expand All @@ -53,11 +51,11 @@ const RequestBackgroundColor = "\x1b" + "]" + "11;" + "?" + "\x07"
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
func SetCursorColor(c color.Color) string {
return "\x1b" + "]" + "12;" + internal.ColorToHexString(c) + "\x07"
return "\x1b]12;" + colorToHexString(c) + "\x07"
}

// RequestCursorColor is a sequence that requests the current terminal cursor
// color.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
const RequestCursorColor = "\x1b" + "]" + "12;" + "?" + "\x07"
const RequestCursorColor = "\x1b]12;?\x07"
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package sys_test
package ansi_test

import (
"testing"

"github.com/charmbracelet/x/exp/term/ansi/style"
"github.com/charmbracelet/x/exp/term/ansi/sys"
"github.com/charmbracelet/x/exp/term/ansi"
)

func TestSetForegroundColorNil(t *testing.T) {
s := sys.SetForegroundColor(nil)
s := ansi.SetForegroundColor(nil)
if s != "\x1b]10;\x07" {
t.Errorf("Unexpected string for SetForegroundColor: got %q", s)
}
}

func TestStringImplementations(t *testing.T) {
foregroundColor := sys.SetForegroundColor(style.BrightMagenta)
backgroundColor := sys.SetBackgroundColor(style.ExtendedColor(255))
cursorColor := sys.SetCursorColor(style.TrueColor(0xffeeaa))
foregroundColor := ansi.SetForegroundColor(ansi.BrightMagenta)
backgroundColor := ansi.SetBackgroundColor(ansi.ExtendedColor(255))
cursorColor := ansi.SetCursorColor(ansi.TrueColor(0xffeeaa))

if foregroundColor != "\x1b]10;#ff00ff\x07" {
t.Errorf("Unexpected string for SetForegroundColor: got %q",
Expand Down
6 changes: 3 additions & 3 deletions exp/term/ansi/sys/clipboard.go → exp/term/ansi/clipboard.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sys
package ansi

import "encoding/base64"

Expand All @@ -21,7 +21,7 @@ func SetClipboard(c byte, d string) string {
if d != "" {
d = base64.StdEncoding.EncodeToString([]byte(d))
}
return "\x1b" + "]" + "52;" + string(c) + ";" + d + "\x07"
return "\x1b]52;" + string(c) + ";" + d + "\x07"
}

// ResetClipboard returns a sequence for resetting the clipboard.
Expand All @@ -37,5 +37,5 @@ func ResetClipboard(c byte) string {
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
func RequestClipboard(c byte) string {
return "\x1b" + "]" + "52;" + string(c) + ";" + "?" + "\x07"
return "\x1b]52;" + string(c) + ";?\x07"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package sys_test
package ansi_test

import (
"testing"

"github.com/charmbracelet/x/exp/term/ansi/sys"
"github.com/charmbracelet/x/exp/term/ansi"
)

func TestClipboardNewClipboard(t *testing.T) {
Expand All @@ -16,25 +16,25 @@ func TestClipboardNewClipboard(t *testing.T) {
{'p', "Ansi Test", "\x1b]52;p;QW5zaSBUZXN0\x07"},
{'c', "", "\x1b]52;c;\x07"},
{'p', "?", "\x1b]52;p;Pw==\x07"},
{sys.SystemClipboard, "test", "\x1b]52;c;dGVzdA==\x07"},
{ansi.SystemClipboard, "test", "\x1b]52;c;dGVzdA==\x07"},
}
for _, tp := range tt {
cb := sys.SetClipboard(tp.name, tp.data)
cb := ansi.SetClipboard(tp.name, tp.data)
if cb != tp.expect {
t.Errorf("SetClipboard(%q, %q) = %q, want %q", tp.name, tp.data, cb, tp.expect)
}
}
}

func TestClipboardReset(t *testing.T) {
cb := sys.ResetClipboard(sys.PrimaryClipboard)
cb := ansi.ResetClipboard(ansi.PrimaryClipboard)
if cb != "\x1b]52;p;\x07" {
t.Errorf("Unexpected clipboard reset: %q", cb)
}
}

func TestClipboardRequest(t *testing.T) {
cb := sys.RequestClipboard(sys.PrimaryClipboard)
cb := ansi.RequestClipboard(ansi.PrimaryClipboard)
if cb != "\x1b]52;p;?\x07" {
t.Errorf("Unexpected clipboard request: %q", cb)
}
Expand Down
2 changes: 1 addition & 1 deletion exp/term/ansi/style/color.go → exp/term/ansi/color.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package style
package ansi

import (
"image/color"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package style
package ansi

import (
"image/color"
"testing"

"github.com/charmbracelet/x/exp/term/ansi/internal"
)

func TestRGBAToHex(t *testing.T) {
Expand Down Expand Up @@ -37,7 +35,7 @@ func TestColorToHexString(t *testing.T) {
}

for _, c := range cases {
got := internal.ColorToHexString(c.color)
got := colorToHexString(c.color)
if got != c.want {
t.Errorf("colorToHexString(%v): got %v, want %v", c.color, got, c.want)
}
Expand Down
6 changes: 3 additions & 3 deletions exp/term/ansi/ctrl/ctrl.go → exp/term/ansi/ctrl.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package ctrl
package ansi

// RequestXTVersion is a control sequence that requests the terminal's XTVERSION. It responds with a DSR sequence identifying the version.
//
// CSI > Ps q
// DCS > | text ST
//
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
const RequestXTVersion = "\x1b[" + ">0q"
const RequestXTVersion = "\x1b[>0q"

// RequestPrimaryDeviceAttributes is a control sequence that requests the
// terminal's primary device attributes (DA1).
//
// CSI c
//
// See https://vt100.net/docs/vt510-rm/DA1.html
const RequestPrimaryDeviceAttributes = "\x1b[" + "c"
const RequestPrimaryDeviceAttributes = "\x1b[c"
159 changes: 159 additions & 0 deletions exp/term/ansi/cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package ansi

import "strconv"

// SaveCursor (DECSC) is an escape sequence that saves the current cursor
// position.
//
// See: https://vt100.net/docs/vt510-rm/DECSC.html
const SaveCursor = "\x1b7"

// RestoreCursor (DECRC) is an escape sequence that restores the cursor
// position.
//
// See: https://vt100.net/docs/vt510-rm/DECRC.html
const RestoreCursor = "\x1b8"

// CursorUp (CUU) returns a sequence for moving the cursor up n cells.
//
// CSI n A
//
// See: https://vt100.net/docs/vt510-rm/CUU.html
func CursorUp(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "A"
}

// CursorUp1 is a sequence for moving the cursor up one cell.
//
// This is equivalent to CursorUp(1).
const CursorUp1 = "\x1b[A"

// CursorDown (CUD) returns a sequence for moving the cursor down n cells.
//
// CSI n B
//
// See: https://vt100.net/docs/vt510-rm/CUD.html
func CursorDown(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "B"
}

// CursorDown1 is a sequence for moving the cursor down one cell.
//
// This is equivalent to CursorDown(1).
const CursorDown1 = "\x1b[B"

// CursorRight (CUF) returns a sequence for moving the cursor right n cells.
//
// CSI n C
//
// See: https://vt100.net/docs/vt510-rm/CUF.html
func CursorRight(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "C"
}

// CursorRight1 is a sequence for moving the cursor right one cell.
//
// This is equivalent to CursorRight(1).
const CursorRight1 = "\x1b[C"

// CursorLeft (CUB) returns a sequence for moving the cursor left n cells.
//
// CSI n D
//
// See: https://vt100.net/docs/vt510-rm/CUB.html
func CursorLeft(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "D"
}

// CursorLeft1 is a sequence for moving the cursor left one cell.
//
// This is equivalent to CursorLeft(1).
const CursorLeft1 = "\x1b[D"

// CursorNextLine (CNL) returns a sequence for moving the cursor to the
// beginning of the next line n times.
//
// CSI n E
//
// See: https://vt100.net/docs/vt510-rm/CNL.html
func CursorNextLine(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "E"
}

// CursorPreviousLine (CPL) returns a sequence for moving the cursor to the
// beginning of the previous line n times.
//
// CSI n F
//
// See: https://vt100.net/docs/vt510-rm/CPL.html
func CursorPreviousLine(n int) string {
var s string
if n > 1 {
s = strconv.Itoa(n)
}
return "\x1b[" + s + "F"
}

// MoveCursor (CUP) returns a sequence for moving the cursor to the given row
// and column.
//
// CSI n ; m H
//
// See: https://vt100.net/docs/vt510-rm/CUP.html
func MoveCursor(row, col int) string {
var r, c string
if row > 1 {
r = strconv.Itoa(row)
}
if col > 1 {
c = strconv.Itoa(col)
}
return "\x1b[" + r + ";" + c + "H"
}

// MoveCursorZero is a sequence for moving the cursor to the upper left corner
// of the screen.
// This is equivalent to MoveCursor(1, 1).
const MoveCursorZero = "\x1b[1;1H"

// SaveCursorPosition (SCP or SCOSC) is a sequence for saving the cursor
// position.
//
// CSI s
//
// This acts like Save, except the page number where the cursor is located is
// not saved.
//
// See: https://vt100.net/docs/vt510-rm/SCOSC.html
const SaveCursorPosition = "\x1b[s"

// RestoreCursorPosition (RCP or SCORC) is a sequence for restoring the cursor
// position.
//
// CSI u
//
// This acts like Restore, except the cursor stays on the same page where the
// cursor was saved.
//
// See: https://vt100.net/docs/vt510-rm/SCORC.html
const RestoreCursorPosition = "\x1b[u"
Loading

0 comments on commit 871d2af

Please sign in to comment.