Skip to content

Commit

Permalink
Merge pull request #834 from gwenn/win_term
Browse files Browse the repository at this point in the history
Use is_unsupported_term on both unix and windows
  • Loading branch information
gwenn authored Dec 17, 2024
2 parents 964fbad + fcd5e6c commit 5910f9e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
31 changes: 31 additions & 0 deletions src/tty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! This module implements and describes common TTY methods & traits
/// Unsupported Terminals that don't support RAW mode
const UNSUPPORTED_TERM: [&str; 3] = ["dumb", "cons25", "emacs"];

use crate::config::{Behavior, BellStyle, ColorMode, Config};
use crate::highlight::Highlighter;
use crate::keys::KeyEvent;
Expand Down Expand Up @@ -200,6 +203,22 @@ pub trait Term {
fn set_cursor_visibility(&mut self, visible: bool) -> Result<Option<Self::CursorGuard>>;
}

/// Check TERM environment variable to see if current term is in our
/// unsupported list
fn is_unsupported_term() -> bool {
match std::env::var("TERM") {
Ok(term) => {
for iter in &UNSUPPORTED_TERM {
if (*iter).eq_ignore_ascii_case(&term) {
return true;
}
}
false
}
Err(_) => false,
}
}

// If on Windows platform import Windows TTY module
// and re-export into mod.rs scope
#[cfg(all(windows, not(target_arch = "wasm32")))]
Expand All @@ -218,3 +237,15 @@ pub use self::unix::*;
mod test;
#[cfg(any(test, target_arch = "wasm32"))]
pub use self::test::*;

#[cfg(test)]
mod test_ {
#[test]
fn test_unsupported_term() {
std::env::set_var("TERM", "xterm");
assert!(!super::is_unsupported_term());

std::env::set_var("TERM", "dumb");
assert!(super::is_unsupported_term());
}
}
30 changes: 1 addition & 29 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ use crate::layout::{Layout, Position, Unit};
use crate::line_buffer::LineBuffer;
use crate::{error, Cmd, ReadlineError, Result};

/// Unsupported Terminals that don't support RAW mode
const UNSUPPORTED_TERM: [&str; 3] = ["dumb", "cons25", "emacs"];

const BRACKETED_PASTE_ON: &str = "\x1b[?2004h";
const BRACKETED_PASTE_OFF: &str = "\x1b[?2004l";

Expand Down Expand Up @@ -70,22 +67,6 @@ fn get_win_size(fd: RawFd) -> (Unit, Unit) {
}
}

/// Check TERM environment variable to see if current term is in our
/// unsupported list
fn is_unsupported_term() -> bool {
match std::env::var("TERM") {
Ok(term) => {
for iter in &UNSUPPORTED_TERM {
if (*iter).eq_ignore_ascii_case(&term) {
return true;
}
}
false
}
Err(_) => false,
}
}

/// Return whether or not STDIN, STDOUT or STDERR is a TTY
fn is_a_tty(fd: RawFd) -> bool {
isatty(fd).unwrap_or(false)
Expand Down Expand Up @@ -1357,7 +1338,7 @@ impl Term for PosixTerminal {
false,
)
};
let unsupported = is_unsupported_term();
let unsupported = super::is_unsupported_term();
let sigwinch = if !unsupported && is_in_a_tty && is_out_a_tty {
Some(SigWinCh::install_sigwinch_handler()?)
} else {
Expand Down Expand Up @@ -1686,15 +1667,6 @@ mod test {
assert_eq!(0, pos.row);
}

#[test]
fn test_unsupported_term() {
std::env::set_var("TERM", "xterm");
assert!(!super::is_unsupported_term());

std::env::set_var("TERM", "dumb");
assert!(super::is_unsupported_term());
}

#[test]
fn test_send() {
fn assert_send<T: Send>() {}
Expand Down
3 changes: 1 addition & 2 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,8 @@ impl Term for Console {
})
}

/// Checking for an unsupported TERM in windows is a no-op
fn is_unsupported(&self) -> bool {
false
super::is_unsupported_term()
}

fn is_input_tty(&self) -> bool {
Expand Down

0 comments on commit 5910f9e

Please sign in to comment.