From efb06b8baa5885305c12f3b6071fce440ba83d0f Mon Sep 17 00:00:00 2001 From: Max Sharnoff Date: Sat, 10 Oct 2020 08:32:18 +0100 Subject: [PATCH 1/3] improve formatting for color parsing docs (#496) --- src/style/ansi.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/style/ansi.rs b/src/style/ansi.rs index 7b789fb..46bbb06 100644 --- a/src/style/ansi.rs +++ b/src/style/ansi.rs @@ -90,13 +90,14 @@ impl Colored { /// This is the string that would appear within an `ESC [ m` escape sequence, as found in /// various configuration files. /// - /// For example: `38;5;0 -> ForegroundColor(Black)`, - /// `38;5;26 -> ForegroundColor(AnsiValue(26))` - /// `48;2;50;60;70 -> BackgroundColor(Rgb(50, 60, 70))` - /// `49 -> BackgroundColor(Reset)` - /// Invalid sequences map to None. + /// For example: + /// * `38;5;0 -> ForegroundColor(Black)`, + /// * `38;5;26 -> ForegroundColor(AnsiValue(26))` + /// * `48;2;50;60;70 -> BackgroundColor(Rgb(50, 60, 70))` + /// * `49 -> BackgroundColor(Reset)` + /// Invalid sequences map to `None`. /// - /// Currently, 3/4 bit color values aren't supported so return None. + /// Currently, 3/4 bit color values aren't supported so return `None`. /// /// See also: [Color::parse_ansi](enum.Color.html#method.parse_ansi) pub fn parse_ansi(ansi: &str) -> Option { @@ -124,10 +125,13 @@ impl Colored { impl<'a> Color { /// Parses an ANSI color sequence. - /// For example: `5;0 -> Black`, `5;26 -> AnsiValue(26)`, `2;50;60;70 -> Rgb(50, 60, 70)`. - /// Invalid sequences map to None. + /// For example: + /// * `5;0 -> Black`, + /// * `5;26 -> AnsiValue(26)`, + /// * `2;50;60;70 -> Rgb(50, 60, 70)`. + /// Invalid sequences map to `None`. /// - /// Currently, 3/4 bit color values aren't supported so return None. + /// Currently, 3/4 bit color values aren't supported so return `None`. /// /// See also: [Colored::parse_ansi](enum.Colored.html#method.parse_ansi) pub fn parse_ansi(ansi: &str) -> Option { From 4b1c8579142da89d0189f85aefe457d643c71491 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 10 Oct 2020 16:03:12 -0400 Subject: [PATCH 2/3] Fix enabling ANSI support when stdout is redirected. (#497) --- src/ansi_support.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansi_support.rs b/src/ansi_support.rs index b473fb8..cf1d97b 100644 --- a/src/ansi_support.rs +++ b/src/ansi_support.rs @@ -1,4 +1,4 @@ -use crossterm_winapi::ConsoleMode; +use crossterm_winapi::{ConsoleMode, Handle}; use winapi::um::wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING; use lazy_static::lazy_static; @@ -17,7 +17,7 @@ use crate::Result; pub(crate) fn set_virtual_terminal_processing(yes: bool) -> Result<()> { let mask = ENABLE_VIRTUAL_TERMINAL_PROCESSING; - let console_mode = ConsoleMode::new()?; + let console_mode = ConsoleMode::from(Handle::current_out_handle()?); let old_mode = console_mode.mode()?; let new_mode = if yes { From aa8436e41a5b60d022d7b2c99f292e0cd7b611ce Mon Sep 17 00:00:00 2001 From: Andrea Jemmett <1787979+acidghost@users.noreply.github.com> Date: Sat, 24 Oct 2020 16:38:03 +0200 Subject: [PATCH 3/3] Use `tty_fd` for set/get terminal attributes (#501) --- src/terminal/sys/unix.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 5fddd62..2274c1e 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -1,16 +1,16 @@ //! UNIX related logic for terminal manipulation. use std::{io, mem, process, sync::Mutex}; -use crate::event::sys::unix::file_descriptor::FileDesc; +use crate::event::sys::unix::file_descriptor::{tty_fd, FileDesc}; use lazy_static::lazy_static; use libc::{ - cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDIN_FILENO, - STDOUT_FILENO, TCSANOW, TIOCGWINSZ, + cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDOUT_FILENO, TCSANOW, + TIOCGWINSZ, }; use crate::error::{ErrorKind, Result}; use std::fs::File; -use std::os::unix::io::IntoRawFd; +use std::os::unix::io::{IntoRawFd, RawFd}; lazy_static! { // Some(Termios) -> we're in the raw mode and this is the previous mode @@ -54,11 +54,13 @@ pub(crate) fn enable_raw_mode() -> Result<()> { return Ok(()); } - let mut ios = get_terminal_attr()?; + let tty = tty_fd()?; + let fd = tty.raw_fd(); + let mut ios = get_terminal_attr(fd)?; let original_mode_ios = ios; raw_terminal_attr(&mut ios); - set_terminal_attr(&ios)?; + set_terminal_attr(fd, &ios)?; // Keep it last - set the original mode only if we were able to switch to the raw mode *original_mode = Some(original_mode_ios); @@ -70,7 +72,8 @@ pub(crate) fn disable_raw_mode() -> Result<()> { let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock().unwrap(); if let Some(original_mode_ios) = original_mode.as_ref() { - set_terminal_attr(original_mode_ios)?; + let tty = tty_fd()?; + set_terminal_attr(tty.raw_fd(), original_mode_ios)?; // Keep it last - remove the original mode only if we were able to switch back *original_mode = None; } @@ -116,16 +119,16 @@ fn raw_terminal_attr(termios: &mut Termios) { unsafe { cfmakeraw(termios) } } -fn get_terminal_attr() -> Result { +fn get_terminal_attr(fd: RawFd) -> Result { unsafe { let mut termios = mem::zeroed(); - wrap_with_result(tcgetattr(STDIN_FILENO, &mut termios))?; + wrap_with_result(tcgetattr(fd, &mut termios))?; Ok(termios) } } -fn set_terminal_attr(termios: &Termios) -> Result { - wrap_with_result(unsafe { tcsetattr(STDIN_FILENO, TCSANOW, termios) }) +fn set_terminal_attr(fd: RawFd, termios: &Termios) -> Result { + wrap_with_result(unsafe { tcsetattr(fd, TCSANOW, termios) }) } pub fn wrap_with_result(result: i32) -> Result {