From cfc052a65aabeb5a91076a1aea26fcb2901a4047 Mon Sep 17 00:00:00 2001 From: Justine Date: Tue, 7 Feb 2023 18:55:04 +0100 Subject: [PATCH] RGB support --- sqishrc.yaml | 6 ++++-- src/shell/config.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/sqishrc.yaml b/sqishrc.yaml index 1a9517e..5211355 100644 --- a/sqishrc.yaml +++ b/sqishrc.yaml @@ -19,6 +19,8 @@ #WHITE #CYAN #LBLACK (light black, a dark gray in fact) -#Will definitely add RGB support, stay tuned. -prompt: "$COLORLBLACK_ !$HISTNUMBER_$COLORCYAN_[$USER_@$HOSTNAME_] $DIR_> $COLORRESET_" +#RGB ! +#RGB is defined as $RGB|red|green|blue_ +#For example $RGB|108|84|30_ gives you an ugly shade of brown +prompt: "$COLORLBLACK_ !$HISTNUMBER_$COLORCYAN_[$USER_@$HOSTNAME_]$RGB|125|0|125_$DIR_> $COLORRESET_" diff --git a/src/shell/config.rs b/src/shell/config.rs index 355a574..ada3782 100644 --- a/src/shell/config.rs +++ b/src/shell/config.rs @@ -2,6 +2,7 @@ use users::{get_user_by_uid, get_current_uid}; use termion::color; use std::{env, fs}; use std::collections::HashMap; +use regex::Regex; extern crate dirs; @@ -50,6 +51,7 @@ impl SqishConf { hotkeys: HashMap::new(), }; + out_conf.handle_rgb(); out_conf.handle_colors(); return Ok(out_conf); @@ -94,6 +96,7 @@ impl SqishConf { prompt = prompt.to_owned(); self.promptline = prompt; self.handle_colors(); + self.handle_rgb(); } @@ -117,7 +120,49 @@ impl SqishConf { prompt = prompt.replace("$COLORRESET_", &reset); let promptown = prompt.to_owned(); self.promptline = promptown; - } + fn handle_rgb(&mut self) { + let re = Regex::new(r"\$RGB[^_]*_").unwrap(); + let promptline_copy = &self.promptline.clone(); + let matches = re.find_iter(promptline_copy); + //println!("Matches : {:?}", &matches.count()); + + for m in matches { + //Copy of the text used for parsing by replacement + let mut matchtext = String::from(m.as_str()); + //Keep a copy of the original... + let matchtext_copy = matchtext.clone(); + + //Start replacing... + matchtext = matchtext.replace("$RGB", ""); + matchtext = matchtext.replace("_", ""); + matchtext = matchtext.replace("|", " "); + matchtext = String::from(matchtext.trim()); + let matchvalues = matchtext + .split_whitespace() + .collect::>(); + //We now have a Vector of values that must + //converted to u8 before being used by termion + //println!("MATCH {:?}", matchvalues); + + //Get the color... + let red: u8 = matchvalues[0].parse().unwrap_or(108); + let green: u8 = matchvalues[1].parse().unwrap_or(84); + let blue: u8 = matchvalues[0].parse().unwrap_or(30); + + let color = color::Fg(color::Rgb { + 0: red, + 1: green, + 2: blue, + }); + let color_display = format!("{}", color); + //...and replace in promptline + let out_str = self.promptline.replace(&matchtext_copy, &color_display); + let out_str = out_str.to_owned(); + *&mut self.promptline = out_str; + } + } + + }