Skip to content

Commit abb7d9a

Browse files
committed
feat: safe get_dimensions (??)
1 parent df5eeeb commit abb7d9a

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

image/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ image.workspace = true
1515
[target.'cfg(not(windows))'.dependencies]
1616
color_quant = "1.1.0"
1717
base64 = "0.22.1"
18-
nix = { version = "0.30.1", features = ["poll", "term"] }
18+
nix = { version = "0.30.1", features = ["poll", "term", "ioctl"] }
1919
libc = "0.2.177"

image/src/iterm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl super::ImageBackend for ITermBackend {
2121
image: &DynamicImage,
2222
_colors: usize,
2323
) -> Result<String> {
24-
let tty_size = unsafe { get_dimensions() };
24+
let tty_size = get_dimensions()?;
2525
let width_ratio = f64::from(tty_size.ws_col) / f64::from(tty_size.ws_xpixel);
2626
let height_ratio = f64::from(tty_size.ws_row) / f64::from(tty_size.ws_ypixel);
2727

image/src/kitty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl super::ImageBackend for KittyBackend {
7373
image: &DynamicImage,
7474
_colors: usize,
7575
) -> Result<String> {
76-
let tty_size = unsafe { get_dimensions() };
76+
let tty_size = get_dimensions()?;
7777
let width_ratio = f64::from(tty_size.ws_col) / f64::from(tty_size.ws_xpixel);
7878
let height_ratio = f64::from(tty_size.ws_row) / f64::from(tty_size.ws_ypixel);
7979

image/src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use anyhow::Result;
1+
use anyhow::{bail, Result};
22
use image::DynamicImage;
3+
use nix::pty::Winsize;
34

45
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
56
pub enum ImageProtocol {
@@ -50,16 +51,23 @@ pub fn get_image_backend(image_protocol: ImageProtocol) -> Option<Box<dyn ImageB
5051
}
5152

5253
#[cfg(not(windows))]
53-
unsafe fn get_dimensions() -> libc::winsize {
54-
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ};
55-
use std::mem::zeroed;
54+
fn get_dimensions() -> Result<Winsize> {
55+
nix::ioctl_read_bad!(ioctl, nix::libc::TIOCGWINSZ, nix::libc::winsize);
5656

57-
let mut window: winsize = zeroed();
58-
let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
57+
let mut window = Winsize {
58+
ws_col: 0,
59+
ws_row: 0,
60+
ws_xpixel: 0,
61+
ws_ypixel: 0,
62+
};
63+
let result = unsafe {
64+
use std::os::fd::AsRawFd as _;
65+
ioctl(std::io::stdout().as_raw_fd(), &mut window)?
66+
};
5967

6068
if result == -1 {
61-
zeroed()
69+
bail!("ioctl error!")
6270
} else {
63-
window
71+
Ok(window)
6472
}
6573
}

image/src/sixel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl SixelBackend {
6666
impl super::ImageBackend for SixelBackend {
6767
#[allow(clippy::map_entry)]
6868
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> Result<String> {
69-
let tty_size = unsafe { get_dimensions() };
69+
let tty_size = get_dimensions()?;
7070
let cw = tty_size.ws_xpixel / tty_size.ws_col;
7171
let lh = tty_size.ws_ypixel / tty_size.ws_row;
7272
let width_ratio = 1.0 / cw as f64;

0 commit comments

Comments
 (0)