Skip to content

Commit

Permalink
API Cleanup - part 3 (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka authored and TimonPost committed Sep 20, 2019
1 parent 60cd127 commit 5216ecb
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 163 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @TimonPost
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ cursor.goto(5,20);
// Print at X: 5 Y: 20.
print!("Yea!");
// Reset back to X: 5 Y: 5.
cursor.reset_position();
cursor.restore_position();
// Print 'Back' at X: 5 Y: 5.
print!("Back");

Expand Down
2 changes: 1 addition & 1 deletion crossterm_cursor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ cursor.goto(5,20);
// Print at X: 5 Y: 20.
print!("Yea!");
// Reset back to X: 5 Y: 5.
cursor.reset_position();
cursor.restore_position();
// Print 'Back' at X: 5 Y: 5.
print!("Back");

Expand Down
2 changes: 1 addition & 1 deletion crossterm_cursor/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait ITerminalCursor: Sync + Send {
/// is stored program based not per instance of the cursor struct.
fn save_position(&self) -> Result<()>;
/// Return to saved cursor position
fn reset_position(&self) -> Result<()>;
fn restore_position(&self) -> Result<()>;
/// Hide the terminal cursor.
fn hide(&self) -> Result<()>;
/// Show the terminal cursor
Expand Down
48 changes: 20 additions & 28 deletions crossterm_cursor/src/cursor/ansi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn get_move_left_ansi(count: u16) -> String {
format!(csi!("{}D"), count)
}

pub static SAFE_POS_ANSI: &'static str = csi!("s");
pub static RESET_POS_ANSI: &'static str = csi!("u");
pub static SAVE_POS_ANSI: &'static str = csi!("s");
pub static RESTORE_POS_ANSI: &'static str = csi!("u");
pub static HIDE_ANSI: &'static str = csi!("?25l");
pub static SHOW_ANSI: &'static str = csi!("?25h");
pub static BLINK_ON_ANSI: &'static str = csi!("?12h");
Expand Down Expand Up @@ -75,12 +75,12 @@ impl ITerminalCursor for AnsiCursor {
}

fn save_position(&self) -> Result<()> {
write_cout!(SAFE_POS_ANSI)?;
write_cout!(SAVE_POS_ANSI)?;
Ok(())
}

fn reset_position(&self) -> Result<()> {
write_cout!(RESET_POS_ANSI)?;
fn restore_position(&self) -> Result<()> {
write_cout!(RESTORE_POS_ANSI)?;
Ok(())
}

Expand Down Expand Up @@ -111,45 +111,37 @@ mod tests {
// TODO - Test is ingored, because it's stalled on Travis CI
#[test]
#[ignore]
fn reset_safe_ansi() {
fn test_ansi_save_restore_position() {
if try_enable_ansi() {
let cursor = AnsiCursor::new();
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 (saved_x, saved_y) = cursor.pos().unwrap();

let pos = cursor.pos();
assert!(pos.is_ok());
let (x_saved, y_saved) = pos.unwrap();
cursor.save_position().unwrap();
cursor.goto(saved_x + 1, saved_y + 1).unwrap();
cursor.restore_position().unwrap();

assert_eq!(x, x_saved);
assert_eq!(y, y_saved);
let (x, y) = cursor.pos().unwrap();

assert_eq!(x, saved_x);
assert_eq!(y, saved_y);
}
}

// TODO - Test is ingored, because it's stalled on Travis CI
#[test]
#[ignore]
fn goto_ansi() {
fn test_ansi_goto() {
if try_enable_ansi() {
let cursor = AnsiCursor::new();
let pos = cursor.pos();
assert!(pos.is_ok());
let (x_saved, y_saved) = pos.unwrap();

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

assert!(cursor.goto(x_saved, y_saved).is_ok());
cursor.goto(saved_x + 1, saved_y + 1).unwrap();
assert_eq!(cursor.pos().unwrap(), (saved_x + 1, saved_y + 1));

assert_eq!(x, 5);
assert_eq!(y, 5);
cursor.goto(saved_x, saved_y).unwrap();
assert_eq!(cursor.pos().unwrap(), (saved_x, saved_y));
}
}

Expand Down
32 changes: 16 additions & 16 deletions crossterm_cursor/src/cursor/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl TerminalCursor {
}

/// Return to saved cursor position
pub fn reset_position(&self) -> Result<()> {
self.cursor.reset_position()
pub fn restore_position(&self) -> Result<()> {
self.cursor.restore_position()
}

/// Hide de cursor in the console.
Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct Goto(pub u16, pub u16);
impl Command for Goto {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::get_goto_ansi(self.0, self.1)
}

Expand All @@ -149,7 +149,7 @@ pub struct Up(pub u16);
impl Command for Up {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::get_move_up_ansi(self.0)
}

Expand All @@ -167,7 +167,7 @@ pub struct Down(pub u16);
impl Command for Down {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::get_move_down_ansi(self.0)
}

Expand All @@ -185,7 +185,7 @@ pub struct Left(pub u16);
impl Command for Left {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::get_move_left_ansi(self.0)
}

Expand All @@ -203,7 +203,7 @@ pub struct Right(pub u16);
impl Command for Right {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::get_move_right_ansi(self.0)
}

Expand All @@ -223,8 +223,8 @@ pub struct SavePos;
impl Command for SavePos {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
ansi_cursor::SAFE_POS_ANSI
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::SAVE_POS_ANSI
}

#[cfg(windows)]
Expand All @@ -241,13 +241,13 @@ pub struct ResetPos;
impl Command for ResetPos {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
ansi_cursor::RESET_POS_ANSI
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::RESTORE_POS_ANSI
}

#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
WinApiCursor::new().reset_position()
WinApiCursor::new().restore_position()
}
}

Expand All @@ -259,7 +259,7 @@ pub struct Hide;
impl Command for Hide {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::HIDE_ANSI
}

Expand All @@ -277,7 +277,7 @@ pub struct Show;
impl Command for Show {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::SHOW_ANSI
}

Expand All @@ -298,7 +298,7 @@ pub struct BlinkOn;
impl Command for BlinkOn {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::BLINK_ON_ANSI
}

Expand All @@ -319,7 +319,7 @@ pub struct BlinkOff;
impl Command for BlinkOff {
type AnsiType = &'static str;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_cursor::BLINK_OFF_ANSI
}

Expand Down
38 changes: 17 additions & 21 deletions crossterm_cursor/src/cursor/winapi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl ITerminalCursor for WinApiCursor {
Ok(())
}

fn reset_position(&self) -> Result<()> {
Cursor::reset_to_saved_position()?;
fn restore_position(&self) -> Result<()> {
Cursor::restore_cursor_pos()?;
Ok(())
}

Expand All @@ -83,35 +83,31 @@ mod tests {
use super::{ITerminalCursor, WinApiCursor};

#[test]
fn goto_winapi() {
fn test_winapi_goto() {
let cursor = WinApiCursor::new();

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

assert_eq!(x, 5);
assert_eq!(y, 5);
cursor.goto(saved_x + 1, saved_y + 1).unwrap();
assert_eq!(cursor.pos().unwrap(), (saved_x + 1, saved_y + 1));

cursor.goto(saved_x, saved_y).unwrap();
assert_eq!(cursor.pos().unwrap(), (saved_x, saved_y));
}

#[test]
fn reset_safe_winapi() {
fn test_winapi_save_and_restore() {
let cursor = WinApiCursor::new();

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

assert!(cursor.save_position().is_ok());
assert!(cursor.goto(5, 5).is_ok());
assert!(cursor.reset_position().is_ok());
cursor.save_position().unwrap();
cursor.goto(saved_x + 1, saved_y + 1).unwrap();
cursor.restore_position().unwrap();

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

assert_eq!(x, x_saved);
assert_eq!(y, y_saved);
assert_eq!(x, saved_x);
assert_eq!(y, saved_y);
}
}
2 changes: 1 addition & 1 deletion crossterm_cursor/src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn show_cursor(show_cursor: bool) -> Result<()> {
}

pub fn pos() -> Result<(u16, u16)> {
unix::into_raw_mode()?;
unix::enable_raw_mode()?;
let pos = pos_raw();
unix::disable_raw_mode()?;
pos
Expand Down
6 changes: 3 additions & 3 deletions crossterm_cursor/src/sys/winapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Cursor {

unsafe {
if !is_true(SetConsoleCursorPosition(
**self.screen_buffer.get_handle(),
**self.screen_buffer.handle(),
position,
)) {
Err(io::Error::last_os_error())?;
Expand All @@ -85,7 +85,7 @@ impl Cursor {

unsafe {
if !is_true(SetConsoleCursorInfo(
**self.screen_buffer.get_handle(),
**self.screen_buffer.handle(),
&cursor_info,
)) {
Err(io::Error::last_os_error())?;
Expand All @@ -95,7 +95,7 @@ impl Cursor {
}

/// Reset to saved cursor position
pub fn reset_to_saved_position() -> Result<()> {
pub fn restore_cursor_pos() -> Result<()> {
let cursor = Cursor::new()?;

unsafe {
Expand Down
2 changes: 1 addition & 1 deletion crossterm_screen/src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl RawModeCommand {

/// Enables raw mode.
pub fn enable(&mut self) -> Result<()> {
crossterm_utils::sys::unix::into_raw_mode()?;
crossterm_utils::sys::unix::enable_raw_mode()?;
Ok(())
}

Expand Down
10 changes: 5 additions & 5 deletions crossterm_style/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl TerminalColor {
/// # Remarks
///
/// This does not always provide a good result.
pub fn get_available_color_count(&self) -> u16 {
pub fn available_color_count(&self) -> u16 {
env::var("TERM")
.map(|x| if x.contains("256color") { 256 } else { 8 })
.unwrap_or(8)
Expand All @@ -90,7 +90,7 @@ pub struct SetFg(pub Color);
impl Command for SetFg {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_color::get_set_fg_ansi(self.0)
}

Expand All @@ -108,7 +108,7 @@ pub struct SetBg(pub Color);
impl Command for SetBg {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_color::get_set_bg_ansi(self.0)
}

Expand All @@ -126,7 +126,7 @@ pub struct SetAttr(pub Attribute);
impl Command for SetAttr {
type AnsiType = String;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
ansi_color::get_set_attr_ansi(self.0)
}

Expand All @@ -148,7 +148,7 @@ where
{
type AnsiType = StyledObject<D>;

fn get_ansi_code(&self) -> Self::AnsiType {
fn ansi_code(&self) -> Self::AnsiType {
self.0.clone()
}

Expand Down
Loading

0 comments on commit 5216ecb

Please sign in to comment.