-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: added functions to get terminal size (#4)
- Loading branch information
Showing
9 changed files
with
290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ doc() { | |
} | ||
|
||
if [[ "$#" == "0" ]]; then | ||
clean | ||
#clean | ||
format | ||
compile | ||
test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
/// `Size` is the struct for storing the size of the current terminal. | ||
#[derive(Debug)] | ||
pub struct Size { | ||
/// The column number of the terminal. | ||
pub col: u16, | ||
/// The row number of the terminal. | ||
pub row: u16, | ||
} | ||
|
||
#[cfg(unix)] | ||
mod unix; | ||
#[cfg(unix)] | ||
pub use self::unix::{term_cols, term_size}; | ||
|
||
#[cfg(windows)] | ||
mod windows; | ||
#[cfg(windows)] | ||
pub use self::windows::{term_cols, term_size}; | ||
|
||
#[cfg(not(any(unix, windows)))] | ||
mod unknown; | ||
#[cfg(not(any(unix, windows)))] | ||
pub use self::unknown::{term_cols, term_size}; | ||
|
||
#[cfg(test)] | ||
mod test_of_term_cols { | ||
use super::*; | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_get_terminal_cols() { | ||
match term_cols() { | ||
Ok(c) => println!("term cols = {}", c), | ||
Err(e) => { | ||
println!("term cols error = {}", e.to_string()); | ||
assert_eq!(e.raw_os_error().unwrap(), 25); // NOTTY | ||
} | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
#[test] | ||
fn test_get_terminal_cols() { | ||
match term_cols() { | ||
Ok(c) => println!("term cols = {}", c), | ||
Err(e) => { | ||
println!("term cols error = {}", e.to_string()); | ||
assert_eq!(e.raw_os_error().unwrap() & 0xffff, 6); // Invalid Handler | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(any(unix, windows)))] | ||
#[test] | ||
fn test_get_terminal_cols() { | ||
match term_cols() { | ||
Ok(cols) => assert!(false), | ||
Err(e) => assert_eq!(e.kind(), ErrorKind::Unsupported), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_of_term_size { | ||
use super::*; | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_get_terminal_size() { | ||
match term_size() { | ||
Ok(sz) => println!("term size = {} x {}", sz.col, sz.row), | ||
Err(e) => { | ||
println!("term size error = {}", e.to_string()); | ||
assert_eq!(e.raw_os_error().unwrap(), 25); // NOTTY | ||
} | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
#[test] | ||
fn test_get_terminal_size() { | ||
match term_size() { | ||
Ok(sz) => println!("term size = {} x {}", sz.col, sz.row), | ||
Err(e) => { | ||
println!("term size error = {}", e.to_string()); | ||
assert_eq!(e.raw_os_error().unwrap() & 0xffff, 6); // Invalid Handler | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(any(unix, windows)))] | ||
#[test] | ||
fn test_get_terminal_cols() { | ||
match term_cols() { | ||
Ok(cols) => assert!(false), | ||
Err(e) => assert_eq!(e.kind(), ErrorKind::Unsupported), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
use super::Size; | ||
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ}; | ||
use std::io; | ||
|
||
pub fn term_cols() -> Result<u16, io::Error> { | ||
let mut ws = winsize { | ||
ws_row: 0, | ||
ws_col: 0, | ||
ws_xpixel: 0, | ||
ws_ypixel: 0, | ||
}; | ||
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut ws) }; | ||
match r { | ||
0 => Ok(ws.ws_col as u16), | ||
_ => Err(io::Error::last_os_error()), | ||
} | ||
} | ||
|
||
pub fn term_size() -> Result<Size, io::Error> { | ||
let mut ws = winsize { | ||
ws_row: 0, | ||
ws_col: 0, | ||
ws_xpixel: 0, | ||
ws_ypixel: 0, | ||
}; | ||
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut ws) }; | ||
match r { | ||
0 => Ok(Size { | ||
col: ws.ws_col as u16, | ||
row: ws.ws_row as u16, | ||
}), | ||
_ => Err(io::Error::last_os_error()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
use super::Size; | ||
use std::io; | ||
|
||
pub fn term_cols() -> Result<u16, io::Error> { | ||
Error::new(ErrorKind::Unsupported) | ||
} | ||
|
||
pub fn term_size() -> Result<u16, io::Error> { | ||
Error::new(ErrorKind::Unsupported) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
use super::Size; | ||
use std::io; | ||
|
||
use windows::Win32::System::Console::{ | ||
GetConsoleScreenBufferInfo, GetStdHandle, CONSOLE_CHARACTER_ATTRIBUTES, | ||
CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT, STD_OUTPUT_HANDLE, | ||
}; | ||
|
||
pub fn term_cols() -> Result<u16, io::Error> { | ||
let mut bi = CONSOLE_SCREEN_BUFFER_INFO { | ||
dwSize: COORD { X: 0, Y: 0 }, | ||
dwCursorPosition: COORD { X: 0, Y: 0 }, | ||
wAttributes: CONSOLE_CHARACTER_ATTRIBUTES(0), | ||
srWindow: SMALL_RECT { | ||
Left: 0, | ||
Top: 0, | ||
Right: 0, | ||
Bottom: 0, | ||
}, | ||
dwMaximumWindowSize: COORD { X: 0, Y: 0 }, | ||
}; | ||
|
||
let h = match unsafe { GetStdHandle(STD_OUTPUT_HANDLE) } { | ||
Ok(h) => h, | ||
Err(e) => return Err(io::Error::from_raw_os_error(e.code().0)), | ||
}; | ||
|
||
match unsafe { GetConsoleScreenBufferInfo(h, &mut bi) } { | ||
Ok(_) => Ok((bi.srWindow.Right - bi.srWindow.Left + 1) as u16), | ||
Err(e) => Err(io::Error::from_raw_os_error(e.code().0)), | ||
} | ||
} | ||
|
||
pub fn term_size() -> Result<Size, io::Error> { | ||
let mut bi = CONSOLE_SCREEN_BUFFER_INFO { | ||
dwSize: COORD { X: 0, Y: 0 }, | ||
dwCursorPosition: COORD { X: 0, Y: 0 }, | ||
wAttributes: CONSOLE_CHARACTER_ATTRIBUTES(0), | ||
srWindow: SMALL_RECT { | ||
Left: 0, | ||
Top: 0, | ||
Right: 0, | ||
Bottom: 0, | ||
}, | ||
dwMaximumWindowSize: COORD { X: 0, Y: 0 }, | ||
}; | ||
|
||
let h = match unsafe { GetStdHandle(STD_OUTPUT_HANDLE) } { | ||
Ok(h) => h, | ||
Err(e) => return Err(io::Error::from_raw_os_error(e.code().0)), | ||
}; | ||
|
||
match unsafe { GetConsoleScreenBufferInfo(h, &mut bi) } { | ||
Ok(_) => Ok(Size { | ||
col: (bi.srWindow.Right - bi.srWindow.Left + 1) as u16, | ||
row: (bi.srWindow.Bottom - bi.srWindow.Top + 1) as u16, | ||
}), | ||
Err(e) => Err(io::Error::from_raw_os_error(e.code().0)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use linebreak::{term_cols, term_size}; | ||
use std::env; | ||
|
||
#[test] | ||
fn it_should_get_terminal_column_number() { | ||
let cols = term_cols(); | ||
if env::var("CI").is_err() { | ||
assert!(cols > 0); | ||
} else { | ||
assert_eq!(cols, 80); | ||
} | ||
} | ||
|
||
#[test] | ||
fn it_should_get_terminal_size() { | ||
let size = term_size(); | ||
if env::var("CI").is_err() { | ||
assert!(size.col > 0); | ||
assert!(size.row > 0); | ||
} else { | ||
assert_eq!(size.col, 80); | ||
assert_eq!(size.row, 24); | ||
} | ||
} |