-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add cursor movement to IO.ANSI #7396
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
Changes from 11 commits
a247c7d
12bfa2c
13ec4f0
16332ab
ee54a77
dc8c4d6
0b74e75
a3628b1
d4e0204
4d23cb2
7f9ad71
d057335
87350ba
0c60956
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,6 +170,32 @@ defmodule IO.ANSI do | |
@doc "Sends cursor home." | ||
defsequence(:home, "", "H") | ||
|
||
@doc """ | ||
Sends cursor to the absolute position specified by `line` and `column`. | ||
|
||
Line `0` and column `0` would mean the top left corner. | ||
""" | ||
@spec cursor(integer, integer) :: String.t() | ||
def cursor(line, column) | ||
when is_integer(line) and line >= 0 and is_integer(column) and column >= 0, | ||
do: "\e[#{line};#{column}H" | ||
|
||
@doc "Sends cursor `lines` up." | ||
@spec cursor_up(integer) :: String.t() | ||
def cursor_up(lines \\ 1) when is_integer(lines) and lines > 0, do: "\e[#{lines}A" | ||
|
||
@doc "Sends cursor `lines` down." | ||
@spec cursor_down(integer) :: String.t() | ||
def cursor_down(lines \\ 1) when is_integer(lines) and lines > 0, do: "\e[#{lines}B" | ||
|
||
@doc "Sends cursor `columns` to the left." | ||
@spec cursor_left(integer) :: String.t() | ||
def cursor_left(columns \\ 1) when is_integer(columns) and columns > 0, do: "\e[#{columns}C" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sends cursor |
||
@doc "Sends cursor `columns` to the right." | ||
@spec cursor_right(integer) :: String.t() | ||
def cursor_right(columns \\ 1) when is_integer(columns) and columns > 0, do: "\e[#{columns}D" | ||
|
||
@doc "Clears screen." | ||
defsequence(:clear, "2", "J") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,4 +155,53 @@ defmodule IO.ANSITest do | |
IO.ANSI.color_background(5, -1, 1) | ||
end | ||
end | ||
|
||
test "cursor/2" do | ||
assert IO.ANSI.cursor(0, 0) == "\e[0;0H" | ||
assert IO.ANSI.cursor(11, 12) == "\e[11;12H" | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
IO.ANSI.cursor(-1, 5) | ||
end | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
IO.ANSI.cursor(5, -1) | ||
end | ||
end | ||
|
||
test "cursor_up/1" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😁 so it is |
||
assert IO.ANSI.cursor_up() == "\e[1A" | ||
assert IO.ANSI.cursor_up(12) == "\e[12A" | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also test the value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Wouldn't it be best to have a guard that the value is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we go that way, the typespec should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had the guard as |
||
IO.ANSI.cursor_up(-1) | ||
end | ||
end | ||
|
||
test "cursor_down/1" do | ||
assert IO.ANSI.cursor_down() == "\e[1B" | ||
assert IO.ANSI.cursor_down(2) == "\e[2B" | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
IO.ANSI.cursor_down(-1) | ||
end | ||
end | ||
|
||
test "cursor_left/1" do | ||
assert IO.ANSI.cursor_left() == "\e[1C" | ||
assert IO.ANSI.cursor_left(3) == "\e[3C" | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
IO.ANSI.cursor_left(-1) | ||
end | ||
end | ||
|
||
test "cursor_right/0" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
assert IO.ANSI.cursor_right() == "\e[1D" | ||
assert IO.ANSI.cursor_right(4) == "\e[4D" | ||
|
||
assert_raise FunctionClauseError, fn -> | ||
IO.ANSI.cursor_right(-1) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@whatyouhide this is how
mix format
formatted it but it looks a little ugly. Think I should make ais_positive_integer
guard / do we have one somewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't but this formatting is fine. When
when
gets split on its own line, we just use thedo
/end
syntax: