Skip to content

Commit

Permalink
Do not set the terminal to raw mode if it isn't actually a terminal
Browse files Browse the repository at this point in the history
It doesn't make sense to try to set a terminal as a raw terminal if it
isn't actually a terminal. Worse, this may break tests that assume they
can simply provide a pipe to a subprocess and expect the terminal will
be unchanged.

See this PR, which needs this patch:
sharkdp/bat#2631
  • Loading branch information
aykevl committed Dec 5, 2023
1 parent e9ca93d commit cfbb47e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pre-release-replacements = [
async-std = "1.9.0"
crossterm = "0.19"
thiserror = "1"
is-terminal = "0.4.9"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["wincon", "winbase", "processenv", "impl-default"] }
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crossterm::terminal;
use is_terminal::IsTerminal;
use std::env;
use std::io::{self, Write};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -179,6 +180,14 @@ pub fn theme(timeout: Duration) -> Result<Theme, Error> {
}

fn from_xterm(term: Terminal, timeout: Duration) -> Result<Rgb, Error> {
if !std::io::stdin().is_terminal()
|| !std::io::stdout().is_terminal()
|| !std::io::stderr().is_terminal()
{
// Not a terminal, so don't try to read the current background color.
return Err(Error::Unsupported);
}

// Query by XTerm control sequence
let query = if term == Terminal::Tmux {
"\x1bPtmux;\x1b\x1b]11;?\x07\x1b\\\x03"
Expand Down

0 comments on commit cfbb47e

Please sign in to comment.