Terminal cursor positioning, visibility and text manipulation.
The purpose of this library is to help move the terminal cursor around and manipulate text by using intuitive method calls.
TTY::Cursor provides independent cursor movement component for TTY toolkit.
Add this line to your application's Gemfile:
gem 'tty-cursor'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tty-cursor
TTY::Cursor is just a module hence you can reference it for later like so:
cursor = TTY::Cursor
and to move the cursor current position by 5 rows up and 2 columns right do:
print cursor.up(5) + cursor.forward(2)
or call move
to move cursor relative to current position:
print cursor.move(5, 2)
to remove text from the current line do:
print cursor.clear_line
All methods in this section allow to position the cursor around the terminal viewport.
Cursor movement will be bounded by the current viewport into the buffer. Scrolling (if available) will not occur.
Set the cursor absolute position to x
and y
coordinate, where x
is the column of the y
line.
If no row/column parameters are provided, the cursor will move to the home position, at the upper left of the screen:
cursor.move_to
Move cursor by x columns and y rows relative to its current position.
Move the cursor up by n
rows; the default n is 1
.
Move the cursor down by n
rows; the default n is 1
.
Move the cursor forward by n
columns; the default n is 1
.
Move the cursor backward by n
columns; the default n is 1
.
Cursor moves to <n>
th position horizontally in the current line.
Cursor moves to the <n>
th position vertically in the current column.
Move the cursor down to the beginning of the next line.
Move the cursor up to the beginning of the previous line.
Save current cursor position.
Restore cursor position after a save cursor was called.
Query current cursor position
The following methods control the visibility of the cursor.
Show the cursor.
Hide the cursor.
To hide the cursor for the duration of the block do:
cursor.invisible { ... }
By default standard output will be used but you can change that by passing a different stream that responds to print
call:
cursor.invisible($stderr) { .... }
All methods in this section provide APIs to modify text buffer contents.
Erase <n>
characters from the current cursor position by overwriting them with space character.
Erase the entire current line and return cursor to beginning of the line.
Erase from the beginning of the line up to and including the current position.
Erase from the current position (inclusive) to the end of the line/display.
Erase n
rows in given direction; the default direction is :up
.
cursor.clear_lines(5, :down)
Erase the screen with the background colour and moves the cursor to home.
Erase the screen from the current line down to the bottom of the screen.
Erase the screen from the current line up to the top of the screen.
Scroll display down one line.
Scroll display up one line.
- Fork it ( https://github.com/piotrmurach/tty-cursor/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Everyone interacting in the Strings::Inflection project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Copyright (c) 2015 Piotr Murach. See LICENSE for further details.