-
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tty): improve terminal size detection (#36)
- Loading branch information
Showing
13 changed files
with
122 additions
and
17 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
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
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 |
---|---|---|
|
@@ -3,3 +3,5 @@ pub mod browser; | |
pub mod gfx; | ||
pub mod input; | ||
pub mod output; | ||
|
||
mod utils; |
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 |
---|---|---|
@@ -1,18 +1,38 @@ | ||
use core::mem::MaybeUninit; | ||
use std::io; | ||
use std::{io, str::FromStr}; | ||
|
||
use crate::gfx::Size; | ||
use crate::{gfx::Size, utils::log}; | ||
|
||
pub fn size() -> io::Result<Size> { | ||
let mut ptr = MaybeUninit::<libc::winsize>::uninit(); | ||
|
||
unsafe { | ||
if libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ, ptr.as_mut_ptr()) == 0 { | ||
let size = ptr.assume_init(); | ||
let mut size = ptr.assume_init(); | ||
|
||
if size.ws_col == 0 || size.ws_row == 0 { | ||
let cols = parse_var("COLUMNS").unwrap_or(80); | ||
let rows = parse_var("LINES").unwrap_or(24); | ||
|
||
log::warning!( | ||
"TIOCGWINSZ returned an empty size ({}x{}), defaulting to {}x{}", | ||
size.ws_col, | ||
size.ws_row, | ||
cols, | ||
rows | ||
); | ||
|
||
size.ws_col = cols; | ||
size.ws_row = rows; | ||
} | ||
|
||
Ok(Size::new(size.ws_col as u32, size.ws_row as u32)) | ||
} else { | ||
Err(io::Error::last_os_error()) | ||
} | ||
} | ||
} | ||
|
||
fn parse_var<T: FromStr>(var: &str) -> Option<T> { | ||
std::env::var(var).ok()?.parse().ok() | ||
} |
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,4 @@ | ||
pub mod log; | ||
mod try_block; | ||
|
||
use try_block::*; |
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,58 @@ | ||
use std::path::Path; | ||
|
||
use chrono::prelude::*; | ||
|
||
use crate::utils::try_block; | ||
|
||
macro_rules! debug { | ||
($($args:expr),+) => { | ||
crate::utils::log::write( | ||
"DEBUG", | ||
file!(), | ||
line!(), | ||
&format!($($args),*) | ||
) | ||
}; | ||
} | ||
macro_rules! warning { | ||
($($args:expr),+) => { | ||
crate::utils::log::write( | ||
"WARNING", | ||
file!(), | ||
line!(), | ||
&format!($($args),*) | ||
) | ||
}; | ||
} | ||
macro_rules! error { | ||
($($args:expr),+) => { | ||
crate::utils::log::write( | ||
"ERROR", | ||
file!(), | ||
line!(), | ||
&format!($($args),*) | ||
) | ||
}; | ||
} | ||
|
||
pub(crate) use debug; | ||
pub(crate) use error; | ||
pub(crate) use warning; | ||
|
||
pub fn write(level: &str, file: &str, line: u32, message: &str) { | ||
let date = Utc::now(); | ||
|
||
eprintln!( | ||
"[{:02}{:02}/{:02}{:02}{:02}.{:06}:{}:{}({})] {}", | ||
date.month(), | ||
date.day(), | ||
date.hour(), | ||
date.minute(), | ||
date.second(), | ||
date.nanosecond() / 1000, | ||
level, | ||
try_block!(Path::new(file).file_name()?.to_str()).unwrap_or("default"), | ||
line, | ||
message | ||
); | ||
} |
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,7 @@ | ||
macro_rules! try_block { | ||
($block:expr) => { | ||
(|| $block)() | ||
}; | ||
} | ||
|
||
pub(crate) use try_block; |