| Crates.io | rpos |
| lib.rs | rpos |
| version | 0.3.2 |
| created_at | 2022-09-24 07:29:47.537337+00 |
| updated_at | 2026-01-25 14:49:58.083108+00 |
| description | Cursor Manager on Table |
| homepage | |
| repository | https://github.com/kohbis/rpos |
| max_upload_size | |
| id | 672925 |
| size | 23,424 |
A cursor manager on table for Rust.
rpos provides a simple and intuitive way to manage cursor position on a 2D table with bounds checking. The cursor can navigate within the table using directional movements (up, down, left, right) or by setting specific positions.
Add this to your Cargo.toml:
[dependencies]
rpos = "0.3.1"
use rpos::table::Table;
fn main() {
// Create a 3x4 table (height=3, width=4)
let mut table = Table::new(3, 4).unwrap();
// Get current cursor position (0-indexed)
assert_eq!(table.cursor.current(), (0, 0));
}
use rpos::table::Table;
fn main() {
let mut table = Table::new(3, 4).unwrap();
// Move cursor down
table.cursor.down();
assert_eq!(table.cursor.current(), (1, 0));
// Move cursor right
table.cursor.right();
assert_eq!(table.cursor.current(), (1, 1));
// Move cursor up
table.cursor.up();
assert_eq!(table.cursor.current(), (0, 1));
// Move cursor left
table.cursor.left();
assert_eq!(table.cursor.current(), (0, 0));
}
use rpos::table::Table;
fn main() {
let mut table = Table::new(3, 4).unwrap();
// Set cursor to specific position (line, column)
table.cursor.set(2, 3).unwrap();
assert_eq!(table.cursor.current(), (2, 3));
// Set only the line
table.cursor.set_line(1).unwrap();
assert_eq!(table.cursor.current(), (1, 3));
// Set only the column
table.cursor.set_column(0).unwrap();
assert_eq!(table.cursor.current(), (1, 0));
}
The cursor automatically stays within table bounds:
use rpos::table::Table;
fn main() {
let mut table = Table::new(3, 4).unwrap();
// Cursor won't go beyond boundaries
table.cursor.up(); // Already at top, stays at (0, 0)
table.cursor.left(); // Already at left edge, stays at (0, 0)
assert_eq!(table.cursor.current(), (0, 0));
// Setting out-of-bounds position returns an error
let result = table.cursor.set(10, 10);
assert!(result.is_err());
}
| Method | Description |
|---|---|
Table::new(height, width) |
Creates a new table with specified dimensions |
| Method | Description |
|---|---|
current() |
Returns current position as (line, column) |
set(line, column) |
Sets cursor to specific position |
set_line(line) |
Sets cursor line (row) |
set_column(column) |
Sets cursor column |
up() |
Moves cursor up one position |
down() |
Moves cursor down one position |
left() |
Moves cursor left one position |
right() |
Moves cursor right one position |
MIT License - see LICENSE for details.