Skip to content

Commit

Permalink
Use raw mode from terminal-trx
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jan 19, 2024
1 parent ea203b1 commit 1f0cbd5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/os/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use libc::c_int;
use std::io;

mod raw_tty;
pub(crate) use raw_tty::*;
#[cfg(not(target_os = "macos"))]
mod poll;
#[cfg(not(target_os = "macos"))]
Expand Down
45 changes: 0 additions & 45 deletions src/os/unix/raw_tty.rs

This file was deleted.

38 changes: 18 additions & 20 deletions src/xterm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::os::{poll_read, run_in_raw_mode};
use crate::os::poll_read;
use crate::terminal::TerminalKind;
use crate::{Color, Error, QueryOptions, Result};
use std::cmp::{max, min};
use std::io::{Read, Write as _};
use std::os::fd::AsRawFd;
use std::str::from_utf8;
use std::time::{Duration, Instant};
use terminal_trx::{terminal, TerminalLock};
use terminal_trx::{terminal, Transceive};

const MIN_TIMEOUT: Duration = Duration::from_millis(100);

Expand Down Expand Up @@ -55,30 +53,30 @@ fn query_color_raw(q: &str, options: QueryOptions, kind: TerminalKind) -> Result
}

let mut tty = terminal()?;
run_in_raw_mode(tty.as_raw_fd(), move || {
let mut tty = tty.lock()?;
match kind {
TerminalKind::Unsupported => unreachable!(),
TerminalKind::Supported => Ok(query(&mut tty, q, options.max_timeout)?.0),
TerminalKind::Unknown => {
// We use a well-supported sequence such as CSI C to measure the latency.
// this is to avoid mixing up the case where the terminal is slow to respond
// (e.g. because we're connected via SSH and have a slow connection)
// with the case where the terminal does not support querying for colors.
let timeout = estimate_timeout(&mut tty, options.max_timeout)?;
Ok(query(&mut tty, q, timeout)?.0)
}
let mut tty = tty.lock()?;
let mut tty = tty.enable_raw_mode()?;

match kind {
TerminalKind::Unsupported => unreachable!(),
TerminalKind::Supported => Ok(query(&mut tty, q, options.max_timeout)?.0),
TerminalKind::Unknown => {
// We use a well-supported sequence such as CSI C to measure the latency.
// this is to avoid mixing up the case where the terminal is slow to respond
// (e.g. because we're connected via SSH and have a slow connection)
// with the case where the terminal does not support querying for colors.
let timeout = estimate_timeout(&mut tty, options.max_timeout)?;
Ok(query(&mut tty, q, timeout)?.0)
}
})
}
}

fn estimate_timeout(tty: &mut TerminalLock<'_>, max_timeout: Duration) -> Result<Duration> {
fn estimate_timeout(tty: &mut dyn Transceive, max_timeout: Duration) -> Result<Duration> {
let (_, latency) = query(tty, "\x1b[c", max_timeout)?;
let timeout = latency * 2; // We want to be in the same ballpark as the latency of our test query. Factor 2 is mostly arbitrary.
Ok(min(max(timeout, MIN_TIMEOUT), max_timeout))
}

fn query(tty: &mut TerminalLock<'_>, query: &str, timeout: Duration) -> Result<(String, Duration)> {
fn query(tty: &mut dyn Transceive, query: &str, timeout: Duration) -> Result<(String, Duration)> {
let mut buffer = vec![0; 100];

write!(tty, "{}", query)?;
Expand Down

0 comments on commit 1f0cbd5

Please sign in to comment.