Move text face/color parsing functions into new module

No other change.
This commit is contained in:
Johannes Altmanninger
2025-04-13 11:42:33 +02:00
parent 2ce3068465
commit 64c362bb73
4 changed files with 62 additions and 61 deletions

View File

@@ -27,8 +27,8 @@
parse_util_locate_cmdsubst_range, parse_util_slice_length, MaybeParentheses,
};
use crate::path::{path_as_implicit_cd, path_get_cdpath, path_get_path, paths_are_same_file};
use crate::terminal::{parse_text_face, Outputter};
use crate::text_face::{TextFace, TextStyling};
use crate::terminal::Outputter;
use crate::text_face::{parse_text_face, TextFace, TextStyling};
use crate::threads::assert_is_background_thread;
use crate::tokenizer::{variable_assignment_equals_pos, PipeOrRedir};
use crate::wchar::{wstr, WString, L};
@@ -195,7 +195,7 @@ pub(crate) fn parse_text_face_for_highlight(
let Some(var) = maybe_var else {
return (Color::Normal, TextStyling::empty());
};
let (mut color, style) = parse_text_face(var, is_background);
let (mut color, style) = parse_text_face(var.as_list(), is_background);
if color.is_none() {
color = Color::Normal;
}

View File

@@ -123,7 +123,6 @@
signal_check_cancel, signal_clear_cancel, signal_reset_handlers, signal_set_handlers,
signal_set_handlers_once,
};
use crate::terminal::parse_text_face;
use crate::terminal::BufferedOutputter;
use crate::terminal::Output;
use crate::terminal::Outputter;
@@ -143,6 +142,7 @@
SYNCHRONIZED_OUTPUT_SUPPORTED,
};
use crate::termsize::{termsize_invalidate_tty, termsize_last, termsize_update};
use crate::text_face::parse_text_face;
use crate::text_face::TextFace;
use crate::threads::{
assert_is_background_thread, assert_is_main_thread, iothread_service_main_with_timeout,
@@ -1661,7 +1661,7 @@ fn paint_layout(&mut self, reason: &wstr, is_final_rendering: bool) {
.vars()
.get_unless_empty(L!("fish_color_search_match"))
.is_some_and(|var| {
let (color, _flags) = parse_text_face(&var, false);
let (color, _flags) = parse_text_face(var.as_list(), false);
!color.is_none()
});

View File

@@ -2,11 +2,10 @@
use crate::color::{Color, Color24};
use crate::common::ToCString;
use crate::common::{self, escape_string, wcs2string, wcs2string_appending, EscapeStringStyle};
use crate::env::EnvVar;
use crate::future_feature_flags::{self, FeatureFlag};
use crate::global_safety::RelaxedAtomicBool;
use crate::screen::{is_dumb, only_grayscale};
use crate::text_face::{TextFace, TextStyling};
use crate::text_face::TextFace;
use crate::threads::MainThread;
use crate::wchar::prelude::*;
use crate::FLOGF;
@@ -760,60 +759,6 @@ pub fn best_color(candidates: &[Color], support: ColorSupport) -> Color {
result
}
pub fn parse_text_face(var: &EnvVar, is_background: bool) -> (Color, TextStyling) {
let mut style = TextStyling::empty();
let mut candidates: Vec<Color> = Vec::new();
let prefix = L!("--background=");
let mut next_is_background = false;
for next in var.as_list() {
let mut color_name = None;
#[allow(clippy::collapsible_else_if)]
if is_background {
if next_is_background {
color_name = Some(next.as_utfstr());
next_is_background = false;
} else if next.starts_with(prefix) {
// Look for something like "--background=red".
color_name = Some(next.slice_from(prefix.char_count()));
} else if next == "--background" || next == "-b" {
// Without argument attached the next token is the color
// - if it's another option it's an error.
next_is_background = true;
} else if next == "--reverse" || next == "-r" {
// Reverse should be meaningful in either context
style |= TextStyling::REVERSE;
} else if next.starts_with("-b") {
// Look for something like "-bred".
// Yes, that length is hardcoded.
color_name = Some(next.slice_from(2));
}
} else {
if next == "--bold" || next == "-o" {
style |= TextStyling::BOLD;
} else if next == "--underline" || next == "-u" {
style |= TextStyling::UNDERLINE;
} else if next == "--italics" || next == "-i" {
style |= TextStyling::ITALICS;
} else if next == "--dim" || next == "-d" {
style |= TextStyling::DIM;
} else if next == "--reverse" || next == "-r" {
style |= TextStyling::REVERSE;
} else {
color_name = Some(next.as_utfstr());
}
}
if let Some(color) = color_name.and_then(Color::from_wstr) {
candidates.push(color);
}
}
let color = best_color(&candidates, get_color_support());
(color, style)
}
/// The [`Term`] singleton. Initialized via a call to [`setup()`] and surfaced to the outside world via [`term()`].
///
/// We can't just use an [`AtomicPtr<Arc<Term>>`](std::sync::atomic::AtomicPtr) here because there's a race condition when the old Arc

View File

@@ -1,6 +1,8 @@
use bitflags::bitflags;
use crate::color::Color;
use crate::terminal::{best_color, get_color_support};
use crate::wchar::prelude::*;
bitflags! {
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
@@ -84,3 +86,57 @@ pub fn set_reverse(&mut self, reverse: bool) {
self.style.set(TextStyling::REVERSE, reverse)
}
}
pub fn parse_text_face(arguments: &[WString], is_background: bool) -> (Color, TextStyling) {
let mut style = TextStyling::empty();
let mut candidates: Vec<Color> = Vec::new();
let prefix = L!("--background=");
let mut next_is_background = false;
for arg in arguments {
let mut color_name = None;
#[allow(clippy::collapsible_else_if)]
if is_background {
if next_is_background {
color_name = Some(arg.as_utfstr());
next_is_background = false;
} else if arg.starts_with(prefix) {
// Look for something like "--background=red".
color_name = Some(arg.slice_from(prefix.char_count()));
} else if arg == "--background" || arg == "-b" {
// Without argument attached the next token is the color
// - if it's another option it's an error.
next_is_background = true;
} else if arg == "--reverse" || arg == "-r" {
// Reverse should be meaningful in either context
style |= TextStyling::REVERSE;
} else if arg.starts_with("-b") {
// Look for something like "-bred".
// Yes, that length is hardcoded.
color_name = Some(arg.slice_from(2));
}
} else {
if arg == "--bold" || arg == "-o" {
style |= TextStyling::BOLD;
} else if arg == "--underline" || arg == "-u" {
style |= TextStyling::UNDERLINE;
} else if arg == "--italics" || arg == "-i" {
style |= TextStyling::ITALICS;
} else if arg == "--dim" || arg == "-d" {
style |= TextStyling::DIM;
} else if arg == "--reverse" || arg == "-r" {
style |= TextStyling::REVERSE;
} else {
color_name = Some(arg.as_utfstr());
}
}
if let Some(color) = color_name.and_then(Color::from_wstr) {
candidates.push(color);
}
}
let color = best_color(&candidates, get_color_support());
(color, style)
}