Skip to content

Commit

Permalink
font size unix & windows
Browse files Browse the repository at this point in the history
  • Loading branch information
benjajaja committed Jun 12, 2023
1 parent 1efdce7 commit de0e627
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
8 changes: 5 additions & 3 deletions examples/is_tty.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crossterm::{
execute,
terminal::{size, SetSize},
terminal::{font_size, size, SetSize},
tty::IsTty,
};
use std::io::{stdin, stdout};

pub fn main() {
println!("{:?}", size().unwrap());
println!("size: {:?}", size().unwrap());
execute!(stdout(), SetSize(10, 10)).unwrap();
println!("{:?}", size().unwrap());
println!("resized: {:?}", size().unwrap());

println!("font_size: {:?}", font_size().unwrap());

if stdin().is_tty() {
println!("Is TTY");
Expand Down
5 changes: 5 additions & 0 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ pub fn size() -> io::Result<(u16, u16)> {
sys::size()
}

/// Returns the terminal font size in pixels.
pub fn font_size() -> io::Result<(u16, u16)> {
sys::font_size()
}

/// Disables line wrapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableLineWrap;
Expand Down
8 changes: 5 additions & 3 deletions src/terminal/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
#[cfg(feature = "events")]
pub use self::unix::supports_keyboard_enhancement;
#[cfg(unix)]
pub(crate) use self::unix::{disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, size};
pub(crate) use self::unix::{
disable_raw_mode, enable_raw_mode, font_size, is_raw_mode_enabled, size,
};
#[cfg(windows)]
#[cfg(feature = "events")]
pub use self::windows::supports_keyboard_enhancement;
#[cfg(windows)]
pub(crate) use self::windows::{
clear, disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, scroll_down, scroll_up,
set_size, set_window_title, size,
clear, disable_raw_mode, enable_raw_mode, font_size, is_raw_mode_enabled, scroll_down,
scroll_up, set_size, set_window_title, size,
};

#[cfg(windows)]
Expand Down
25 changes: 25 additions & 0 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ pub(crate) fn size() -> io::Result<(u16, u16)> {
tput_size().ok_or_else(|| std::io::Error::last_os_error().into())
}

#[allow(clippy::useless_conversion)]
pub(crate) fn font_size() -> io::Result<(u16, u16)> {
// http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc
let mut size = winsize {
ws_row: 0,
ws_col: 0,
ws_xpixel: 0,
ws_ypixel: 0,
};

let file = File::open("/dev/tty").map(|file| (FileDesc::new(file.into_raw_fd(), true)));
let fd = if let Ok(file) = &file {
file.raw_fd()
} else {
// Fallback to libc::STDOUT_FILENO if /dev/tty is missing
STDOUT_FILENO
};

if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() {
return Ok((size.ws_xpixel / size.ws_col, size.ws_ypixel / size.ws_row));
}

Err(std::io::Error::last_os_error().into())
}

pub(crate) fn enable_raw_mode() -> io::Result<()> {
let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock();

Expand Down
5 changes: 5 additions & 0 deletions src/terminal/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub(crate) fn size() -> io::Result<(u16, u16)> {
))
}

pub(crate) fn font_size() -> io::Result<(u16, u16)> {
let size = ScreenBuffer::current()?.font_info()?.size();
Ok((size.width as u16, size.height as u16))
}

/// Queries the terminal's support for progressive keyboard enhancement.
///
/// This always returns `Ok(false)` on Windows.
Expand Down

0 comments on commit de0e627

Please sign in to comment.