Skip to content

Commit

Permalink
API Cleanup - part 1 (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka authored and TimonPost committed Sep 19, 2019
1 parent 7d47354 commit 41de9a6
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 136 deletions.
10 changes: 1 addition & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,4 @@ script:
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then cargo fmt --all -- --check; fi
- cargo build
- if [ "$TRAVIS_OS_NAME" = "windows" ]; then cargo test --all -- --nocapture --test-threads 1; else cargo test --all --exclude crossterm_winapi -- --nocapture --test-threads 1; fi
- |
pushd examples/program_examples
for d in */ ; do
pushd "$d"
cargo build
if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then cargo fmt --all -- --check; fi
popd
done
popd
- scripts/test-examples.sh
4 changes: 1 addition & 3 deletions crossterm_cursor/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ trait ITerminalCursor: Sync + Send {
/// Goto location (`x`, `y`) in the current terminal window.
fn goto(&self, x: u16, y: u16) -> Result<()>;
/// Get the cursor location `(x, y)` in the current terminal window.
///
/// `(0, 0)` is returned in case of an error.
fn pos(&self) -> (u16, u16);
fn pos(&self) -> Result<(u16, u16)>;
/// Move cursor `n` times up
fn move_up(&self, count: u16) -> Result<()>;
/// Move the cursor `n` times to the right.
Expand Down
18 changes: 13 additions & 5 deletions crossterm_cursor/src/cursor/ansi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ITerminalCursor for AnsiCursor {
Ok(())
}

fn pos(&self) -> (u16, u16) {
fn pos(&self) -> Result<(u16, u16)> {
get_cursor_position()
}

Expand Down Expand Up @@ -115,13 +115,17 @@ mod tests {
fn reset_safe_ansi() {
if try_enable_ansi() {
let cursor = AnsiCursor::new();
let (x, y) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x, y) = pos.unwrap();

assert!(cursor.save_position().is_ok());
assert!(cursor.goto(5, 5).is_ok());
assert!(cursor.reset_position().is_ok());

let (x_saved, y_saved) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x_saved, y_saved) = pos.unwrap();

assert_eq!(x, x_saved);
assert_eq!(y, y_saved);
Expand All @@ -134,10 +138,14 @@ mod tests {
fn goto_ansi() {
if try_enable_ansi() {
let cursor = AnsiCursor::new();
let (x_saved, y_saved) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x_saved, y_saved) = pos.unwrap();

assert!(cursor.goto(5, 5).is_ok());
let (x, y) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x, y) = pos.unwrap();

assert!(cursor.goto(x_saved, y_saved).is_ok());

Expand Down
24 changes: 12 additions & 12 deletions crossterm_cursor/src/cursor/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,32 @@ impl TerminalCursor {
///
/// # Remarks
/// position is 0-based, which means we start counting at 0.
pub fn pos(&self) -> (u16, u16) {
pub fn pos(&self) -> Result<(u16, u16)> {
self.cursor.pos()
}

/// Move the current cursor position `n` times up.
pub fn move_up(&mut self, count: u16) -> &mut TerminalCursor {
self.cursor.move_up(count).unwrap();
self
pub fn move_up(&mut self, count: u16) -> Result<&mut TerminalCursor> {
self.cursor.move_up(count)?;
Ok(self)
}

/// Move the current cursor position `n` times right.
pub fn move_right(&mut self, count: u16) -> &mut TerminalCursor {
self.cursor.move_right(count).unwrap();
self
pub fn move_right(&mut self, count: u16) -> Result<&mut TerminalCursor> {
self.cursor.move_right(count)?;
Ok(self)
}

/// Move the current cursor position `n` times down.
pub fn move_down(&mut self, count: u16) -> &mut TerminalCursor {
self.cursor.move_down(count).unwrap();
self
pub fn move_down(&mut self, count: u16) -> Result<&mut TerminalCursor> {
self.cursor.move_down(count)?;
Ok(self)
}

/// Move the current cursor position `n` times left.
pub fn move_left(&mut self, count: u16) -> &mut TerminalCursor {
pub fn move_left(&mut self, count: u16) -> Result<&mut TerminalCursor> {
self.cursor.move_left(count).unwrap();
self
Ok(self)
}

/// Save cursor position for recall later.
Expand Down
27 changes: 17 additions & 10 deletions crossterm_cursor/src/cursor/winapi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ impl ITerminalCursor for WinApiCursor {
Ok(())
}

fn pos(&self) -> (u16, u16) {
let cursor = Cursor::new().unwrap();
cursor.position().map(Into::into).unwrap_or((0, 0))
fn pos(&self) -> Result<(u16, u16)> {
let cursor = Cursor::new()?;
Ok(cursor.position()?.into())
}

fn move_up(&self, count: u16) -> Result<()> {
let (xpos, ypos) = self.pos();
let (xpos, ypos) = self.pos()?;
self.goto(xpos, ypos - count)?;
Ok(())
}

fn move_right(&self, count: u16) -> Result<()> {
let (xpos, ypos) = self.pos();
let (xpos, ypos) = self.pos()?;
self.goto(xpos + count, ypos)?;
Ok(())
}

fn move_down(&self, count: u16) -> Result<()> {
let (xpos, ypos) = self.pos();
let (xpos, ypos) = self.pos()?;
self.goto(xpos, ypos + count)?;
Ok(())
}

fn move_left(&self, count: u16) -> Result<()> {
let (xpos, ypos) = self.pos();
let (xpos, ypos) = self.pos()?;
self.goto(xpos - count, ypos)?;
Ok(())
}
Expand Down Expand Up @@ -87,7 +87,9 @@ mod tests {
let cursor = WinApiCursor::new();

assert!(cursor.goto(5, 5).is_ok());
let (x, y) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x, y) = pos.unwrap();

assert_eq!(x, 5);
assert_eq!(y, 5);
Expand All @@ -96,13 +98,18 @@ mod tests {
#[test]
fn reset_safe_winapi() {
let cursor = WinApiCursor::new();
let (x, y) = cursor.pos();

let pos = cursor.pos();
assert!(pos.is_ok());
let (x, y) = pos.unwrap();

assert!(cursor.save_position().is_ok());
assert!(cursor.goto(5, 5).is_ok());
assert!(cursor.reset_position().is_ok());

let (x_saved, y_saved) = cursor.pos();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x_saved, y_saved) = pos.unwrap();

assert_eq!(x, x_saved);
assert_eq!(y, y_saved);
Expand Down
32 changes: 8 additions & 24 deletions crossterm_cursor/src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crossterm_utils::{
};

#[cfg(unix)]
pub fn get_cursor_position() -> (u16, u16) {
pub fn get_cursor_position() -> Result<(u16, u16)> {
if unsafe { RAW_MODE_ENABLED } {
pos_raw().unwrap_or((0, 0))
pos_raw()
} else {
pos().unwrap_or((0, 0))
pos()
}
}

Expand Down Expand Up @@ -45,33 +45,17 @@ pub fn pos_raw() -> Result<(u16, u16)> {
stdin.lock().read_until(b'[', &mut vec![])?;

let mut rows = vec![];
stdin.lock().read_until(b';', &mut rows).unwrap();
stdin.lock().read_until(b';', &mut rows)?;

let mut cols = vec![];
stdin.lock().read_until(b'R', &mut cols).unwrap();
stdin.lock().read_until(b'R', &mut cols)?;

// remove delimiter
rows.pop();
cols.pop();

let rows = rows
.into_iter()
.map(|b| (b as char))
.fold(String::new(), |mut acc, n| {
acc.push(n);
acc
})
.parse::<usize>()
.unwrap();
let cols = cols
.into_iter()
.map(|b| (b as char))
.fold(String::new(), |mut acc, n| {
acc.push(n);
acc
})
.parse::<usize>()
.unwrap();
let rows = String::from_utf8(rows)?.parse::<u16>()?;
let cols = String::from_utf8(cols)?.parse::<u16>()?;

Ok(((cols - 1) as u16, (rows - 1) as u16))
Ok((cols - 1, rows - 1))
}
9 changes: 3 additions & 6 deletions crossterm_cursor/src/sys/winapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ use crossterm_utils::Result;
pub use crossterm_winapi::{is_true, Coord, Handle, HandleType, ScreenBuffer};

#[cfg(windows)]
pub fn get_cursor_position() -> (u16, u16) {
if let Ok(cursor) = Cursor::new() {
cursor.position().unwrap().into()
} else {
(0, 0)
}
pub fn get_cursor_position() -> Result<(u16, u16)> {
let cursor = Cursor::new()?;
Ok(cursor.position()?.into())
}

#[cfg(windows)]
Expand Down
18 changes: 9 additions & 9 deletions crossterm_terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,26 @@ use crossterm::terminal::{terminal,ClearType};
let mut terminal = terminal();

// Clear all lines in terminal;
terminal.clear(ClearType::All);
terminal.clear(ClearType::All)?;
// Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorDown);
terminal.clear(ClearType::FromCursorDown)?;
// Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorUp);
terminal.clear(ClearType::FromCursorUp)?;
// Clear current line cells.
terminal.clear(ClearType::CurrentLine);
terminal.clear(ClearType::CurrentLine)?;
// Clear all the cells until next line.
terminal.clear(ClearType::UntilNewLine);
terminal.clear(ClearType::UntilNewLine)?;

// Get terminal size
let (width, height) = terminal.terminal_size();
let (width, height) = terminal.size()?;
print!("X: {}, y: {}", width, height);

// Scroll down, up 10 lines.
terminal.scroll_down(10);
terminal.scroll_up(10);
terminal.scroll_down(10)?;
terminal.scroll_up(10)?;

// Set terminal size (width, height)
terminal.set_size(10,10);
terminal.set_size(10,10)?;

// exit the current process.
terminal.exit();
Expand Down
8 changes: 5 additions & 3 deletions crossterm_terminal/src/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ};

use crossterm_utils::Result;

pub fn exit() {
::std::process::exit(0);
}

/// Get the current terminal size.
pub fn get_terminal_size() -> (u16, u16) {
pub fn get_terminal_size() -> Result<(u16, u16)> {
// http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc
let mut size = winsize {
ws_row: 0,
Expand All @@ -16,8 +18,8 @@ pub fn get_terminal_size() -> (u16, u16) {
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &mut size) };

if r == 0 {
(size.ws_col, size.ws_row)
Ok((size.ws_col, size.ws_row))
} else {
(0, 0)
Err(std::io::Error::last_os_error().into())
}
}
11 changes: 4 additions & 7 deletions crossterm_terminal/src/sys/winapi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crossterm_utils::Result;
use crossterm_winapi::ScreenBuffer;

/// Exit the current process.
Expand All @@ -6,11 +7,7 @@ pub fn exit() {
}

#[cfg(windows)]
pub fn get_terminal_size() -> (u16, u16) {
if let Ok(buffer) = ScreenBuffer::current() {
let size = buffer.info().unwrap().terminal_size();
((size.width + 1) as u16, (size.height + 1) as u16)
} else {
(0, 0)
}
pub fn get_terminal_size() -> Result<(u16, u16)> {
let buffer = ScreenBuffer::current()?;
Ok(buffer.info()?.terminal_size().into())
}
8 changes: 4 additions & 4 deletions crossterm_terminal/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ trait ITerminal {
/// Clear the current cursor by specifying the clear type
fn clear(&self, clear_type: ClearType) -> Result<()>;
/// Get the terminal size (x,y)
fn terminal_size(&self) -> (u16, u16);
fn size(&self) -> Result<(u16, u16)>;
/// Scroll `n` lines up in the current terminal.
fn scroll_up(&self, count: i16) -> Result<()>;
fn scroll_up(&self, count: u16) -> Result<()>;
/// Scroll `n` lines down in the current terminal.
fn scroll_down(&self, count: i16) -> Result<()>;
fn scroll_down(&self, count: u16) -> Result<()>;
/// Resize terminal to the given width and height.
fn set_size(&self, width: i16, height: i16) -> Result<()>;
fn set_size(&self, width: u16, height: u16) -> Result<()>;
}
Loading

0 comments on commit 41de9a6

Please sign in to comment.