Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: refactor modes and cursor #217

Merged
merged 12 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansi/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func CursorPreviousLine(n int) string {
// CSI n ; m H
//
// See: https://vt100.net/docs/vt510-rm/CUP.html
func MoveCursor(row, col int) string {
func MoveCursor(col, row int) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change the order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a common pattern to use the x, y int or col, row int than the opposite, it was confusing and led me to swap the values sometimes. It was implemented this way because the ANSI sequence (CUP) takes a row then a column in that order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok that totally makes sense. Thanks for explaining!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok that totally makes sense. Thanks for explaining!

if row < 0 {
row = 0
}
Expand Down
7 changes: 6 additions & 1 deletion ansi/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/charmbracelet/x/ansi

go 1.18

require github.com/rivo/uniseg v0.4.7
require (
github.com/charmbracelet/x/wcwidth v0.0.0-20241022174419-46d9bb99a691
github.com/rivo/uniseg v0.4.7
)

require golang.org/x/text v0.19.0 // indirect
4 changes: 4 additions & 0 deletions ansi/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/charmbracelet/x/wcwidth v0.0.0-20241022174419-46d9bb99a691 h1:XxrWKvvGJaroJNEgoGGBfCGn/s/67TvhJVTF265WwfA=
github.com/charmbracelet/x/wcwidth v0.0.0-20241022174419-46d9bb99a691/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
25 changes: 25 additions & 0 deletions ansi/method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ansi

// Method is a type that represents the how to calculate the cell widths in the
// terminal. The default is to use [WcWidth]. Some terminals use grapheme
// clustering by default. Some support mode 2027 to tell the terminal to use
// mode 2027 instead of wcwidth.
type Method uint8

// Display width modes.
const (
WcWidth Method = iota
GraphemeWidth
)

// String returns the string representation of the Method.
func (m Method) String() string {
switch m {
case WcWidth:
return "WcWidth"
case GraphemeWidth:
return "GraphemeWidth"
default:
return "Unknown"
}
}
122 changes: 70 additions & 52 deletions ansi/mode.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ansi

import "strconv"

// This file define uses multiple sequences to set (SM), reset (RM), and request
// (DECRQM) different ANSI and DEC modes.
//
Expand All @@ -21,110 +23,126 @@ package ansi
// Where Pa is the mode number, and Ps is the mode value.
// See: https://vt100.net/docs/vt510-rm/DECRPM.html

// Mode represents an ANSI terminal mode.
type Mode int

// String returns the mode as a string.
func (m Mode) String() string {
return strconv.Itoa(int(m))
}

// PrivateMode represents a private DEC terminal mode.
type PrivateMode int

// String returns the private mode as a string.
func (m PrivateMode) String() string {
return "?" + strconv.Itoa(int(m))
}

// Application Cursor Keys (DECCKM) is a mode that determines whether the
// cursor keys send ANSI cursor sequences or application sequences.
//
// See: https://vt100.net/docs/vt510-rm/DECCKM.html
const (
CursorKeysMode = "?1"
CursorKeysMode = PrivateMode(1)

EnableCursorKeys = "\x1b[" + CursorKeysMode + "h"
DisableCursorKeys = "\x1b[" + CursorKeysMode + "l"
RequestCursorKeys = "\x1b[" + CursorKeysMode + "$p"
EnableCursorKeys = "\x1b[?1h"
DisableCursorKeys = "\x1b[?1l"
RequestCursorKeys = "\x1b[?1$p"
)

// Text Cursor Enable Mode (DECTCEM) is a mode that shows/hides the cursor.
//
// See: https://vt100.net/docs/vt510-rm/DECTCEM.html
const (
CursorVisibilityMode = "?25"
CursorEnableMode = PrivateMode(25)

ShowCursor = "\x1b[" + CursorVisibilityMode + "h"
HideCursor = "\x1b[" + CursorVisibilityMode + "l"
RequestCursorVisibility = "\x1b[" + CursorVisibilityMode + "$p"
ShowCursor = "\x1b[?25h"
HideCursor = "\x1b[?25l"
RequestCursorVisibility = "\x1b[?25$p"
)

// VT Mouse Tracking is a mode that determines whether the mouse reports on
// button press and release.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
const (
MouseMode = "?1000"
MouseMode = PrivateMode(1000)

EnableMouse = "\x1b[" + MouseMode + "h"
DisableMouse = "\x1b[" + MouseMode + "l"
RequestMouse = "\x1b[" + MouseMode + "$p"
EnableMouse = "\x1b[?1000h"
DisableMouse = "\x1b[?1000l"
RequestMouse = "\x1b[?1000$p"
)

// VT Hilite Mouse Tracking is a mode that determines whether the mouse reports on
// button presses, releases, and highlighted cells.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
const (
MouseHiliteMode = "?1001"
MouseHiliteMode = PrivateMode(1001)

EnableMouseHilite = "\x1b[" + MouseHiliteMode + "h"
DisableMouseHilite = "\x1b[" + MouseHiliteMode + "l"
RequestMouseHilite = "\x1b[" + MouseHiliteMode + "$p"
EnableMouseHilite = "\x1b[?1001h"
DisableMouseHilite = "\x1b[?1001l"
RequestMouseHilite = "\x1b[?1001$p"
)

// Cell Motion Mouse Tracking is a mode that determines whether the mouse
// reports on button press, release, and motion events.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
const (
MouseCellMotionMode = "?1002"
MouseCellMotionMode = PrivateMode(1002)

EnableMouseCellMotion = "\x1b[" + MouseCellMotionMode + "h"
DisableMouseCellMotion = "\x1b[" + MouseCellMotionMode + "l"
RequestMouseCellMotion = "\x1b[" + MouseCellMotionMode + "$p"
EnableMouseCellMotion = "\x1b[?1002h"
DisableMouseCellMotion = "\x1b[?1002l"
RequestMouseCellMotion = "\x1b[?1002$p"
)

// All Mouse Tracking is a mode that determines whether the mouse reports on
// button press, release, motion, and highlight events.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
const (
MouseAllMotionMode = "?1003"
MouseAllMotionMode = PrivateMode(1003)

EnableMouseAllMotion = "\x1b[" + MouseAllMotionMode + "h"
DisableMouseAllMotion = "\x1b[" + MouseAllMotionMode + "l"
RequestMouseAllMotion = "\x1b[" + MouseAllMotionMode + "$p"
EnableMouseAllMotion = "\x1b[?1003h"
DisableMouseAllMotion = "\x1b[?1003l"
RequestMouseAllMotion = "\x1b[?1003$p"
)

// Report Focus is a mode that makes the terminal report focus-in and focus-out events.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-FocusIn_FocusOut
const (
ReportFocusMode = "?1004"
ReportFocusMode = PrivateMode(1004)

EnableReportFocus = "\x1b[" + ReportFocusMode + "h"
DisableReportFocus = "\x1b[" + ReportFocusMode + "l"
RequestReportFocus = "\x1b[" + ReportFocusMode + "$p"
EnableReportFocus = "\x1b[?1004h"
DisableReportFocus = "\x1b[?1004l"
RequestReportFocus = "\x1b[?1004$p"
)

// SGR Mouse Extension is a mode that determines whether the mouse reports events
// formatted with SGR parameters.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
const (
MouseSgrExtMode = "?1006"
MouseSgrExtMode = PrivateMode(1006)

EnableMouseSgrExt = "\x1b[" + MouseSgrExtMode + "h"
DisableMouseSgrExt = "\x1b[" + MouseSgrExtMode + "l"
RequestMouseSgrExt = "\x1b[" + MouseSgrExtMode + "$p"
EnableMouseSgrExt = "\x1b[?1006h"
DisableMouseSgrExt = "\x1b[?1006l"
RequestMouseSgrExt = "\x1b[?1006$p"
)

// Alternate Screen Buffer is a mode that determines whether the alternate screen
// buffer is active.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
const (
AltScreenBufferMode = "?1049"
AltScreenBufferMode = PrivateMode(1049)

EnableAltScreenBuffer = "\x1b[" + AltScreenBufferMode + "h"
DisableAltScreenBuffer = "\x1b[" + AltScreenBufferMode + "l"
RequestAltScreenBuffer = "\x1b[" + AltScreenBufferMode + "$p"
EnableAltScreenBuffer = "\x1b[?1049h"
DisableAltScreenBuffer = "\x1b[?1049l"
RequestAltScreenBuffer = "\x1b[?1049$p"
)

// Bracketed Paste Mode is a mode that determines whether pasted text is
Expand All @@ -133,23 +151,23 @@ const (
// See: https://cirw.in/blog/bracketed-paste
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Bracketed-Paste-Mode
const (
BracketedPasteMode = "?2004"
BracketedPasteMode = PrivateMode(2004)

EnableBracketedPaste = "\x1b[" + BracketedPasteMode + "h"
DisableBracketedPaste = "\x1b[" + BracketedPasteMode + "l"
RequestBracketedPaste = "\x1b[" + BracketedPasteMode + "$p"
EnableBracketedPaste = "\x1b[?2004h"
DisableBracketedPaste = "\x1b[?2004l"
RequestBracketedPaste = "\x1b[?2004$p"
)

// Synchronized Output Mode is a mode that determines whether output is
// synchronized with the terminal.
//
// See: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
const (
SyncdOutputMode = "?2026"
SyncdOutputMode = PrivateMode(2026)

EnableSyncdOutput = "\x1b[" + SyncdOutputMode + "h"
DisableSyncdOutput = "\x1b[" + SyncdOutputMode + "l"
RequestSyncdOutput = "\x1b[" + SyncdOutputMode + "$p"
EnableSyncdOutput = "\x1b[?2026h"
DisableSyncdOutput = "\x1b[?2026l"
RequestSyncdOutput = "\x1b[?2026$p"
)

// Grapheme Clustering Mode is a mode that determines whether the terminal
Expand All @@ -159,21 +177,21 @@ const (
//
// See: https://github.com/contour-terminal/terminal-unicode-core
const (
GraphemeClusteringMode = "?2027"
GraphemeClusteringMode = PrivateMode(2027)

EnableGraphemeClustering = "\x1b[" + GraphemeClusteringMode + "h"
DisableGraphemeClustering = "\x1b[" + GraphemeClusteringMode + "l"
RequestGraphemeClustering = "\x1b[" + GraphemeClusteringMode + "$p"
EnableGraphemeClustering = "\x1b[?2027h"
DisableGraphemeClustering = "\x1b[?2027l"
RequestGraphemeClustering = "\x1b[?2027$p"
)

// Win32Input is a mode that determines whether input is processed by the
// Win32 console and Conpty.
//
// See: https://github.com/microsoft/terminal/blob/main/doc/specs/%234999%20-%20Improved%20keyboard%20handling%20in%20Conpty.md
const (
Win32InputMode = "?9001"
Win32InputMode = PrivateMode(9001)

EnableWin32Input = "\x1b[" + Win32InputMode + "h"
DisableWin32Input = "\x1b[" + Win32InputMode + "l"
RequestWin32Input = "\x1b[" + Win32InputMode + "$p"
EnableWin32Input = "\x1b[?9001h"
DisableWin32Input = "\x1b[?9001l"
RequestWin32Input = "\x1b[?9001$p"
)
Loading
Loading