Repository: starship/starship
File: src/config.rs (Rust)

Code before:
                let (token, col_fg) = if token.as_str().starts_with("fg:") {
                    (token.trim_start_matches("fg:").to_owned(), true)
                } else if token.as_str().starts_with("bg:") {
                    (token.trim_start_matches("bg:").to_owned(), false)
                } else {
                    (token, true) // Bare colors are assumed to color the foreground
                };

                match token.as_str() {
                    "underline" => Some(style.underline()),
                    "bold" => Some(style.bold()),
                    "italic" => Some(style.italic()),
                    "dimmed" => Some(style.dimmed()),
                    "inverted" => Some(style.reverse()),
                    // When the string is supposed to be a color:
                    // Decide if we yield none, reset background or set color.
                    color_string => {
                        if color_string == "none" && col_fg {
                            None // fg:none yields no style.
                        } else {
                            // Ensure that get_palette is only called once so only one warning is emitted
                            // when attempting to retrieve a nonexistent palette
                            // Assumes that only one palette will be used for every time starship is run
                            static PALETTE: once_cell::sync::OnceCell<Option<Palette>> =
                                once_cell::sync::OnceCell::new();

                            // Either bg or valid color or both.
                            let parsed = parse_color_string(
                                color_string,
                                PALETTE
                                    .get_or_init(|| {
                                        context.and_then(|x| {
                                            get_palette(
                                                &x.root_config.palettes,
                                                x.root_config.palette.as_deref(),
                                            )
                                            .cloned()
                                        })
                                    })
                                    .as_ref(),
                            );
                            // bg + invalid color = reset the background to default.
                            if !col_fg && parsed.is_none() {
                                let mut new_style = style;
                                new_style.background = Option::None;
                                Some(new_style)
                            } else {
                                // Valid color, apply color to either bg or fg
                                parsed.map(|ansi_color| {
                                    if col_fg {
                                        style.fg(ansi_color)

Review comment (refactor):
I'd still rather not have a `static` for this, because this might have hard-to-trace effects on tests. Let's just handle the duplicate messages later.

Code after:
                // If col_fg is true, color the foreground. If it's false, color the background.
                let (token, col_fg) = if token.as_str().starts_with("fg:") {
                    (token.trim_start_matches("fg:").to_owned(), true)
                } else if token.as_str().starts_with("bg:") {
                    (token.trim_start_matches("bg:").to_owned(), false)
                } else {
                    (token, true) // Bare colors are assumed to color the foreground
                };

                match token.as_str() {
                    "underline" => Some(style.underline()),
                    "bold" => Some(style.bold()),
                    "italic" => Some(style.italic()),
                    "dimmed" => Some(style.dimmed()),
                    "inverted" => Some(style.reverse()),
                    "blink" => Some(style.blink()),
                    "hidden" => Some(style.hidden()),
                    "strikethrough" => Some(style.strikethrough()),
                    // When the string is supposed to be a color:
                    // Decide if we yield none, reset background or set color.
                    color_string => {
                        if color_string == "none" && col_fg {
                            None // fg:none yields no style.
                        } else {
                            // Either bg or valid color or both.
                            let parsed = parse_color_string(
                                color_string,
                                context.and_then(|x| {
                                    get_palette(
                                        &x.root_config.palettes,
                                        x.root_config.palette.as_deref(),
                                    )
                                }),
                            );
                            // bg + invalid color = reset the background to default.
                            if !col_fg && parsed.is_none() {
                                let mut new_style = style;
                                new_style.background = Option::None;
                                Some(new_style)
                            } else {
                                // Valid color, apply color to either bg or fg
                                parsed.map(|ansi_color| {
                                    if col_fg {
                                        style.fg(ansi_color)
                                    } else {
                                        style.on(ansi_color)
                                    }
                                })
                            }
                        }
                    }