Skip to content

Commit

Permalink
Use terminal-trx
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jan 18, 2024
1 parent 49327bb commit ea203b1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 100 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ thiserror = "1.0.56"
[target.'cfg(unix)'.dependencies]
libc = "0.2.151"
mio = { version = "0.8.10", features = ["os-poll", "os-ext"], default-features = false }
terminal-trx = { git = "https://github.com/bash/terminal-trx" }

[workspace]
members = [
Expand Down
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 tty;
pub(crate) use tty::*;
mod raw_tty;
pub(crate) use raw_tty::*;
#[cfg(not(target_os = "macos"))]
Expand Down
82 changes: 0 additions & 82 deletions src/os/unix/tty.rs

This file was deleted.

36 changes: 20 additions & 16 deletions src/xterm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::os::{poll_read, run_in_raw_mode, tty, Tty};
use crate::os::{poll_read, run_in_raw_mode};
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};

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

Expand Down Expand Up @@ -48,33 +49,36 @@ fn parse_response(response: String, prefix: &str) -> Result<Color> {
.ok_or_else(|| Error::Parse(response))
}

fn query_color_raw(q: &str, options: QueryOptions, terminal: TerminalKind) -> Result<String> {
if let TerminalKind::Unsupported = terminal {
fn query_color_raw(q: &str, options: QueryOptions, kind: TerminalKind) -> Result<String> {
if let TerminalKind::Unsupported = kind {
return Err(Error::UnsupportedTerminal);
}

let mut tty = tty()?;
run_in_raw_mode(tty.as_raw_fd(), move || match terminal {
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 = 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)
}
}
})
}

fn estimate_timeout(tty: &mut Tty, max_timeout: Duration) -> Result<Duration> {
fn estimate_timeout(tty: &mut TerminalLock<'_>, 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 Tty, query: &str, timeout: Duration) -> Result<(String, Duration)> {
fn query(tty: &mut TerminalLock<'_>, query: &str, timeout: Duration) -> Result<(String, Duration)> {
let mut buffer = vec![0; 100];

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

0 comments on commit ea203b1

Please sign in to comment.