#cursor #platform-independent #terminal #cursor-position #console #caret

term_cursor

A crate for handling terminal cursor movement in a platform independent way

7 releases

Uses old Rust 2015

0.2.1 Jun 11, 2018
0.2.0 Apr 25, 2018
0.1.4 Dec 5, 2017
0.1.1 Jul 6, 2017
0.1.0 Jun 30, 2017

#676 in Command-line interface

Download history 121/week @ 2023-11-26 166/week @ 2023-12-03 212/week @ 2023-12-10 33/week @ 2023-12-17 13/week @ 2023-12-24 250/week @ 2023-12-31 282/week @ 2024-01-07 134/week @ 2024-01-14 105/week @ 2024-01-21 148/week @ 2024-01-28 215/week @ 2024-02-04 44/week @ 2024-02-11 137/week @ 2024-02-18 148/week @ 2024-02-25 144/week @ 2024-03-03 102/week @ 2024-03-10

536 downloads per month
Used in 3 crates

MIT license

14KB
223 lines

term_cursor

A pure-rust crate for manipulating the position of the terminal cursor! Also allows for clearing the screen!

Usage

    extern crate term_cursor as cursor;

    fn main() {
        // Clear the screen. Does not reset the cursor position!
        print!("{}", cursor::Clear);
        // Position the cursor at column 5 and row 10 and print "Hello world!".
        print!("{}Hello world!", cursor::Goto(5, 10));
        // Go up a line. Does not reset the column of the cursor!
        print!("{}I'm above", cursor::Up(1));

        // Let's do the same thing again, with the second API.
        cursor::clear().expect("Clear failed");
        cursor::set_pos(5, 10).expect("Setting the cursor position failed");
        print!("Hello world!");
        let (x, _y) = cursor::get_pos().expect("Getting the cursor position failed");
        cursor::set_pos(x, 9).expect("Set failed again");
        print!("I'm above");

        // To finish off the example, move the cursor down 2 lines.
        // That's where the command prompt will return once the program finishes.
        // We don't the command prompt to overprint our stuff!
        print!("{}", cursor::Goto(0, 12));
    }

Caveats

The 2D coordinate system of term_cursor is in the range: x in 0..WIDTH and y in 0..HEIGHT, where WIDTH and HEIGHT are the dimensions of the terminal buffer in characters.

Positioning the cursor and printing text out of bounds is UNDEFINED BEHAVIOUR! Your text might wrap, negative indicies might get turned into positive indicies, or you program might just crash. It is completely platform dependent.

It is your duty to ensure that all drawing happens within bounds. To retrieve the dimensions of the terminal, I recommend the crate term_size.

Dependencies

~74–290KB