Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window pixel size #790

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/is_tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crossterm::{
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());

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

#[derive(Debug)]
pub struct WindowSize {
pub rows: u16,
pub columns: u16,
pub width: u16,
pub height: u16,
}

/// Returns the terminal size `[WindowSize]`.
///
/// The width and height in pixels may not be reliably implemented or default to 0.
/// For unix, https://man7.org/linux/man-pages/man4/tty_ioctl.4.html documents them as "unused".
/// For windows it is not implemented.
pub fn window_size() -> io::Result<WindowSize> {
sys::window_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, window_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, window_size, is_raw_mode_enabled, scroll_down,
scroll_up, set_size, set_window_title, size,
};

#[cfg(windows)]
Expand Down
34 changes: 27 additions & 7 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! UNIX related logic for terminal manipulation.

use crate::terminal::sys::file_descriptor::{tty_fd, FileDesc};
use crate::terminal::{
sys::file_descriptor::{tty_fd, FileDesc},
WindowSize,
};
use libc::{
cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDOUT_FILENO, TCSANOW,
TIOCGWINSZ,
Expand All @@ -20,8 +23,19 @@ pub(crate) fn is_raw_mode_enabled() -> bool {
TERMINAL_MODE_PRIOR_RAW_MODE.lock().is_some()
}

impl From<winsize> for WindowSize {
fn from(size: winsize) -> WindowSize {
WindowSize {
columns: size.ws_col,
rows: size.ws_row,
width: size.ws_xpixel,
height: size.ws_ypixel,
}
}
}

#[allow(clippy::useless_conversion)]
pub(crate) fn size() -> io::Result<(u16, u16)> {
pub(crate) fn window_size() -> io::Result<WindowSize> {
// http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc
let mut size = winsize {
ws_row: 0,
Expand All @@ -38,11 +52,17 @@ pub(crate) fn size() -> io::Result<(u16, u16)> {
STDOUT_FILENO
};

if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok()
&& size.ws_col != 0
&& size.ws_row != 0
{
return Ok((size.ws_col, size.ws_row));
if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() {
return Ok(size.into());
}

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

#[allow(clippy::useless_conversion)]
pub(crate) fn size() -> io::Result<(u16, u16)> {
if let Ok(window_size) = window_size() {
return Ok((window_size.columns, window_size.rows));
}

tput_size().ok_or_else(|| std::io::Error::last_os_error().into())
Expand Down
12 changes: 11 additions & 1 deletion src/terminal/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use winapi::{
um::wincon::{SetConsoleTitleW, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT},
};

use crate::{cursor, terminal::ClearType};
use crate::{
cursor,
terminal::{ClearType, WindowSize},
};

/// bits which can't be set in raw mode
const NOT_RAW_MODE_MASK: DWORD = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT;
Expand Down Expand Up @@ -58,6 +61,13 @@ pub(crate) fn size() -> io::Result<(u16, u16)> {
))
}

pub(crate) fn window_size() -> io::Result<WindowSize> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"Window pixel size not implemented for the Windows API.",
))
}

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