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

BREAKING: refactor modes and cursor #217

merged 12 commits into from
Oct 24, 2024

Conversation

aymanbagabas
Copy link
Member

@aymanbagabas aymanbagabas commented Oct 17, 2024

  • Implement ANSI mode types
  • Fix the order of ansi.MoveCursor
  • Rename ansi.MoveCursor to ansi.SetCursorPosition
  • Implement ansi.Method to respect the cell widths on various ansi operations
  • Rename ansi.EraseDisplay constants

TODO:

  • Add tests

This adds a new type `Method` to the `ansi` package that represents how
to calculate the cell widths in the terminal. The default is to use
`GraphemeWidth` which keeps the current behavior. In order to use
`WcWidth`, you can call the member functions on the `Method` type.

```go
ansi.StringWidth("👩‍👩‍👧‍👧") // 2
ansi.GraphemeWidth.StringWidth("👩‍👩‍👧‍👧") // 2
ansi.WcWidth.StringWidth("👩‍👩‍👧‍👧") // 6
```
ansi/parser_decode.go Outdated Show resolved Hide resolved
ansi/cursor.go Outdated
@@ -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!

This complicates the API and makes it harder to understand. In practice,
it only matters during rendering.
MoveCursor indicates that the cursor is moved from one position to
another relative to where it is currently. But the actual behavior of
the function is to set the cursor to a specific position in the screen.
@aymanbagabas aymanbagabas changed the title BREAKING: Method cell calculation and refactor modes and cursor BREAKING: refactor modes and cursor Oct 23, 2024
@aymanbagabas aymanbagabas merged commit ad25fd0 into main Oct 24, 2024
5 of 7 checks passed
@aymanbagabas aymanbagabas deleted the ansi/refa branch October 24, 2024 14:59
aymanbagabas added a commit that referenced this pull request Jan 23, 2025
This commit introduces a new type, Method, that represents how the
renderer should calculate the display width of cells. It adds
helper methods to the Method type that allow users to use either
WcWidth, which uses go-runewidth, or GraphemeWidth, which uses
uniseg, to calculate the width of strings and text.

Methods like `ansi.StringWidth`, `ansi.Truncate`, `ansi.TruncateLeft`,
and `ansi.Cut` now have variants that are aware of wide characters
and runes. These new methods are prefixed with `Wc` and use
go-runewidth to calculate the width of strings.

Related: #217
aymanbagabas added a commit that referenced this pull request Jan 23, 2025
This commit introduces a new type, Method, that represents how the
renderer should calculate the display width of cells. It adds
helper methods to the Method type that allow users to use either
WcWidth, which uses go-runewidth, or GraphemeWidth, which uses
uniseg, to calculate the width of strings and text.

Methods like `ansi.StringWidth`, `ansi.Truncate`, `ansi.TruncateLeft`,
and `ansi.Cut` now have variants that are aware of wide characters
and runes. These new methods are prefixed with `Wc` and use
go-runewidth to calculate the width of strings.

Related: #217
aymanbagabas added a commit that referenced this pull request Jan 24, 2025
* feat(ansi): define Method and make wcwidth aware variants

This commit introduces a new type, Method, that represents how the
renderer should calculate the display width of cells. It adds
helper methods to the Method type that allow users to use either
WcWidth, which uses go-runewidth, or GraphemeWidth, which uses
uniseg, to calculate the width of strings and text.

Methods like `ansi.StringWidth`, `ansi.Truncate`, `ansi.TruncateLeft`,
and `ansi.Cut` now have variants that are aware of wide characters
and runes. These new methods are prefixed with `Wc` and use
go-runewidth to calculate the width of strings.

Related: #217

* feat(ansi): add WcStringWidth function test

* refactor: rename wc functions to be more consistent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants