Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for erase left and erase line in term.ANSI #4246

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions .release-notes/rfc75.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Add support for erase left and erase line in term.ANSI

We've implemented [RFC #75](https://github.com/ponylang/rfcs/blob/main/text/0075-ansi-erase.md) that set out a means of updating `ANSI` in the `term` package to support not just ANSI erase right, but also erase left, and erase line.

Existing functionality continues to work as is. All existing erase right functionality will continue to work without breakage. The `erase` method has been augmented to take an option parameter for the direction to erase. `EraseRight` is the default, thus keeping existing behavior. `EraseLeft` and `EraseLine` options have been added as well.

To erase left, one would do:

```pony
ANSI.erase(EraseLeft)
```

To erase right, you could do either of the following:

```pony
ANSI.erase()
ANSI.erase(EraseRight)
```

And finally, to erase an entire line:

```pony
ANSI.erase(EraseLine)
```
24 changes: 19 additions & 5 deletions packages/term/ansi.pony
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ primitive ANSI
"""
"\x1B[H\x1B[2J"

fun erase(): String =>
"""
Erases everything to the right of the cursor on the line the cursor is on.
"""
"\x1B[0K"
fun erase(direction: _EraseDirection = EraseRight): String =>
"""
Erases content. The direction to erase is dictated by the `direction`
parameter. Use `EraseLeft` to erase everything from the cursor to the
beginning of the line. Use `EraseLine` to erase the entire line. Use
`EraseRight` to erase everything from the cursor to the end of the line.
The default direction is `EraseRight`.
"""
match direction
| EraseRight => "\x1B[0K"
| EraseLeft => "\x1B[1K"
| EraseLine => "\x1B[2K"
end

fun reset(): String =>
"""
Expand Down Expand Up @@ -287,3 +295,9 @@ primitive ANSI
Bright grey background.
"""
"\x1B[47m"

primitive EraseLeft
primitive EraseLine
primitive EraseRight

type _EraseDirection is (EraseLeft | EraseLine | EraseRight)