Skip to content

Commit c7f8075

Browse files
committed
feat: replace get_dimensions with tcgetwinsize
1 parent 6dacb42 commit c7f8075

File tree

6 files changed

+14
-34
lines changed

6 files changed

+14
-34
lines changed

Cargo.lock

Lines changed: 5 additions & 4 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ image.workspace = true
1616
color_quant = "1.1.0"
1717
base64 = "0.22.1"
1818
nix = { version = "0.30.1", features = ["poll", "term", "ioctl"] }
19+
rustix = { version = "1.1.2", features = ["termios"] }

image/src/iterm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::get_dimensions;
21
use anyhow::Result;
32
use base64::{engine, Engine};
43
use image::{imageops::FilterType, DynamicImage};
4+
use rustix::termios::tcgetwinsize;
55
use std::env;
66
use std::io::Cursor;
77

@@ -21,7 +21,7 @@ impl super::ImageBackend for ITermBackend {
2121
image: &DynamicImage,
2222
_colors: usize,
2323
) -> Result<String> {
24-
let tty_size = get_dimensions()?;
24+
let tty_size = tcgetwinsize(std::io::stdin())?;
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::get_dimensions;
21
use anyhow::{Context as _, Result};
32
use base64::{engine, Engine};
43
use image::{imageops::FilterType, DynamicImage};
54

65
use nix::poll::{poll, PollFd, PollFlags, PollTimeout};
76
use nix::sys::termios::{tcgetattr, tcsetattr, LocalFlags, SetArg};
87
use nix::unistd::read;
8+
use rustix::termios::tcgetwinsize;
99

1010
use std::io::{stdout, Write};
1111
use std::os::fd::AsFd as _;
@@ -73,7 +73,7 @@ impl super::ImageBackend for KittyBackend {
7373
image: &DynamicImage,
7474
_colors: usize,
7575
) -> Result<String> {
76-
let tty_size = get_dimensions()?;
76+
let tty_size = tcgetwinsize(std::io::stdin())?;
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: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::Result;
22
use image::DynamicImage;
3-
use nix::pty::Winsize;
3+
44

55
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
66
pub enum ImageProtocol {
@@ -49,25 +49,3 @@ pub fn get_image_backend(image_protocol: ImageProtocol) -> Option<Box<dyn ImageB
4949
let backend = None;
5050
backend
5151
}
52-
53-
#[cfg(not(windows))]
54-
fn get_dimensions() -> Result<Winsize> {
55-
nix::ioctl_read_bad!(ioctl, nix::libc::TIOCGWINSZ, nix::libc::winsize);
56-
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-
};
67-
68-
if result == -1 {
69-
bail!("ioctl error!")
70-
} else {
71-
Ok(window)
72-
}
73-
}

image/src/sixel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::get_dimensions;
21
use anyhow::{Context as _, Result};
32
use color_quant::NeuQuant;
43
use image::{
@@ -9,6 +8,7 @@ use image::{
98
use nix::poll::{poll, PollFd, PollFlags, PollTimeout};
109
use nix::sys::termios::{tcgetattr, tcsetattr, LocalFlags, SetArg};
1110
use nix::unistd::read;
11+
use rustix::termios::tcgetwinsize;
1212

1313
use std::io::{stdout, Write};
1414
use std::os::fd::AsFd;
@@ -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 = get_dimensions()?;
69+
let tty_size = tcgetwinsize(std::io::stdin())?;
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)