diff --git a/doc_src/terminal-compatibility.rst b/doc_src/terminal-compatibility.rst index 239bb21f9..630d5d944 100644 --- a/doc_src/terminal-compatibility.rst +++ b/doc_src/terminal-compatibility.rst @@ -165,13 +165,17 @@ Optional Commands - - Select background color from 24-bit RGB colors. - + * - ``\e[49m`` + - + - Reset background color to the terminal's default. + - * - ``\e[58:2:: Ps : Ps : Ps m`` (note: colons not semicolons) - Su - Select underline color from 24-bit RGB colors. - kitty * - ``\e[59m`` - Su - - Reset underline color (follow foreground color). + - Reset underline color to the default (follow the foreground color). - kitty * - ``\e[ Ps S`` - indn diff --git a/src/builtins/set_color.rs b/src/builtins/set_color.rs index 4cc0918b4..dfa0eaaa2 100644 --- a/src/builtins/set_color.rs +++ b/src/builtins/set_color.rs @@ -3,7 +3,7 @@ use super::prelude::*; use crate::color::Color; use crate::common::str2wcstring; -use crate::terminal::TerminalCommand::DefaultUnderlineColor; +use crate::terminal::TerminalCommand::{DefaultBackgroundColor, DefaultUnderlineColor}; use crate::terminal::{Output, Outputter, Paintable}; use crate::text_face::{ self, parse_text_face_and_options, PrintColorsArgs, SpecifiedTextFace, TextFace, TextStyling, @@ -121,7 +121,7 @@ pub fn set_color(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) - // Here's some automagic behavior: if either of foreground or background are "normal", // reset all colors/attributes. Same if foreground is "reset" (undocumented). - if is_reset || [fg, bg].iter().any(|c| c.is_some_and(|c| c.is_normal())) { + if is_reset || fg.is_some_and(|fg| fg.is_normal()) { outp.reset_text_face(); } @@ -140,7 +140,9 @@ pub fn set_color(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) - } } if let Some(bg) = bg { - if !bg.is_normal() { + if bg.is_normal() { + outp.write_command(DefaultBackgroundColor); + } else { outp.write_color(Paintable::Background, bg); } } diff --git a/src/terminal.rs b/src/terminal.rs index 054fc9551..744e3bb86 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -70,6 +70,7 @@ pub(crate) enum TerminalCommand<'a> { // Colors SelectPaletteColor(Paintable, u8), SelectRgbColor(Paintable, Color24), + DefaultBackgroundColor, DefaultUnderlineColor, // Cursor Movement @@ -163,6 +164,7 @@ fn write(out: &mut impl Output, sequence: &'static [u8]) -> bool { ClearToEndOfScreen => ti(self, b"\x1b[J", |term| &term.clr_eos), SelectPaletteColor(paintable, idx) => palette_color(self, paintable, idx), SelectRgbColor(paintable, rgb) => rgb_color(self, paintable, rgb), + DefaultBackgroundColor => write(self, b"\x1b[49m"), DefaultUnderlineColor => write(self, b"\x1b[59m"), CursorUp => ti(self, b"\x1b[A", |term| &term.cursor_up), CursorDown => ti(self, b"\n", |term| &term.cursor_down), @@ -511,9 +513,9 @@ pub(crate) fn set_text_face(&mut self, face: TextFace) { let mut last_bg_set = false; use TerminalCommand::{ - DefaultUnderlineColor, EnterBoldMode, EnterCurlyUnderlineMode, EnterDimMode, - EnterItalicsMode, EnterReverseMode, EnterStandoutMode, EnterUnderlineMode, - ExitAttributeMode, ExitItalicsMode, ExitUnderlineMode, + DefaultBackgroundColor, DefaultUnderlineColor, EnterBoldMode, EnterCurlyUnderlineMode, + EnterDimMode, EnterItalicsMode, EnterReverseMode, EnterStandoutMode, + EnterUnderlineMode, ExitAttributeMode, ExitItalicsMode, ExitUnderlineMode, }; // Removes all styles that are individually resettable. @@ -572,14 +574,7 @@ pub(crate) fn set_text_face(&mut self, face: TextFace) { if !bg.is_none() && bg != self.last.bg { if bg.is_normal() { - self.write_command(ExitAttributeMode); - if !self.last.fg.is_normal() { - self.write_color(Paintable::Foreground, self.last.fg); - } - if !self.last.underline_color.is_normal() && !self.last.underline_color.is_none() { - self.write_color(Paintable::Underline, self.last.underline_color); - } - self.last.style = TextStyling::default(); + self.write_command(DefaultBackgroundColor); } else { assert!(!bg.is_special()); self.write_color(Paintable::Background, bg); diff --git a/tests/checks/set_color.fish b/tests/checks/set_color.fish index 25d14d768..7e7ad28ec 100644 --- a/tests/checks/set_color.fish +++ b/tests/checks/set_color.fish @@ -14,7 +14,7 @@ string escape (set_color --background=reset) # CHECKERR: set_color: Unknown color 'reset' string escape (set_color --bold red --background=normal) -# CHECK: \e\[m\e\[1m\e\[31m +# CHECK: \e\[1m\e\[31m\e\[49m string escape (set_color --bold red --background=blue) # CHECK: \e\[1m\e\[31m\e\[44m