diff --git a/README.md b/README.md index f2bb7ac2..509c6392 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,34 @@ set -gx MCFLY_PROMPT "❯" ``` Note that only single-character-prompts are allowed. setting `MCFLY_PROMPT` to `""` will reset it to the default prompt. +### Custom Menubar Colors +To change the background color of the menubar, set `MCFLY_MENU_BACKGROUND` (default: `blue`). +To change the foreground color of the menubar, set `MCFLY_MENU_FOREGROUND` (default: `white`). + +bash / zsh: +```bash +export MCFLY_MENU_BACKGROUND=green +export MCFLY_MENU_FOREGROUND=black +``` + +fish: +```bash +set -gx MCFLY_MENU_BACKGROUND green +set -gx MCFLY_MENU_FOREGROUND black +``` + +The following colors are supported: +| Light | Dark | +| :---------- | :------------- | +| `dark_grey` | `black` | +| `red` | `dark_red` | +| `green` | `dark_green` | +| `yellow` | `dark_yellow` | +| `blue` | `dark_blue` | +| `magenta` | `dark_magenta` | +| `cyan` | `dark_cyan` | +| `white` | `grey` | + ### Database Location McFly stores its SQLite database in the standard location for the OS. On OS X, this is in `~/Library/Application Support/McFly` and on Linux it is in `$XDG_DATA_DIR/mcfly/history.db` (default would be `~/.local/share/mcfly/history.db`). For legacy support, if `~/.mcfly/` exists, it is used instead. diff --git a/src/interface.rs b/src/interface.rs index dfdf3463..e16e46c5 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -85,9 +85,9 @@ impl MenuMode { menu_text } - fn bg(&self) -> Color { + fn bg(&self, menu_background: Color) -> Color { match *self { - MenuMode::Normal => Color::Blue, + MenuMode::Normal => menu_background, MenuMode::ConfirmDelete => Color::Red, } } @@ -160,8 +160,8 @@ impl<'a> Interface<'a> { cursor::Hide, cursor::MoveTo(0, self.info_line_index()), Clear(ClearType::CurrentLine), - SetBackgroundColor(self.menu_mode.bg()), - SetForegroundColor(Color::White), + SetBackgroundColor(self.menu_mode.bg(self.settings.menu_background)), + SetForegroundColor(self.settings.menu_foreground), cursor::MoveTo(1, self.info_line_index()), Print(format!( "{text:width$}", @@ -175,11 +175,15 @@ impl<'a> Interface<'a> { fn prompt(&self, screen: &mut W) { let prompt_line_index = self.prompt_line_index(); - let fg = if self.settings.lightmode { - Color::Black - } else { - Color::White - }; + + let fg = self.settings.prompt_foreground.unwrap_or({ + if self.settings.lightmode { + Color::Black + } else { + Color::White + } + }); + queue!( screen, cursor::MoveTo(1, prompt_line_index), @@ -214,31 +218,31 @@ impl<'a> Interface<'a> { let mut index: usize = 0; for command in self.matches.iter() { - let mut fg = if self.settings.lightmode { + let fg = if (index == self.selection) != self.settings.lightmode { Color::Black } else { Color::White }; - let mut highlight = if self.settings.lightmode { - Color::DarkBlue - } else { - Color::DarkGreen - }; - - let mut bg = Color::Reset; - - if index == self.selection { + let highlight = self.settings.highlight_foreground.unwrap_or({ if self.settings.lightmode { - fg = Color::White; - bg = Color::DarkGrey; - highlight = Color::Grey; + Color::DarkBlue } else { - fg = Color::Black; - bg = Color::White; - highlight = Color::DarkGreen; + Color::DarkGreen } - } + }); + + let bg = if index == self.selection { + self.settings.selection_background.unwrap_or({ + if self.settings.lightmode { + Color::DarkGrey + } else { + Color::White + } + }) + } else { + Color::Reset + }; let command_line_index = self.command_line_index(index as i16); queue!( diff --git a/src/settings.rs b/src/settings.rs index 2dd13c2f..f2c5987c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,6 +1,7 @@ use crate::cli::{Cli, SubCommand}; use crate::shell_history; use clap::Parser; +use crossterm::style::Color; use directories_next::{ProjectDirs, UserDirs}; use std::env; use std::path::PathBuf; @@ -88,6 +89,11 @@ pub struct Settings { pub disable_menu: bool, pub prompt: String, pub disable_run_command: bool, + pub menu_background: Color, + pub menu_foreground: Color, + pub prompt_foreground: Option, + pub highlight_foreground: Option, + pub selection_background: Option, } impl Default for Settings { @@ -119,6 +125,11 @@ impl Default for Settings { disable_menu: false, prompt: String::from("$"), disable_run_command: false, + menu_background: Color::Blue, + menu_foreground: Color::White, + prompt_foreground: None, + highlight_foreground: None, + selection_background: None, } } } @@ -155,6 +166,20 @@ impl Settings { _ => ResultSort::Rank, }; + if let Some(color) = get_env_var_color("MCFLY_MENU_BACKGROUND") { + settings.menu_background = color; + } + + if let Some(color) = get_env_var_color("MCFLY_MENU_FOREGROUND") { + settings.menu_foreground = color; + } + + settings.prompt_foreground = get_env_var_color("MCFLY_PROMPT_COLOR"); + + settings.highlight_foreground = get_env_var_color("MCFLY_HIGHLIGHT_COLOR"); + + settings.selection_background = get_env_var_color("MCFLY_SELECTION_COLOR"); + settings.session_id = cli.session_id.unwrap_or_else(|| env::var("MCFLY_SESSION_ID") .unwrap_or_else(|err| { @@ -409,3 +434,9 @@ fn is_env_var_truthy(name: &str) -> bool { Err(_) => false, } } + +fn get_env_var_color(name: &str) -> Option { + env::var(name) + .ok() + .and_then(|val| Color::try_from(val.as_str()).ok()) +}