Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic customisation for menubar colors #343

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,34 @@ set -gx MCFLY_PROMPT "❯"
```
Note that only single-character-prompts are allowed. setting `MCFLY_PROMPT` to `"<str>"` 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.
Expand Down
56 changes: 30 additions & 26 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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$}",
Expand All @@ -175,11 +175,15 @@ impl<'a> Interface<'a> {

fn prompt<W: Write>(&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),
Expand Down Expand Up @@ -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!(
Expand Down
31 changes: 31 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Color>,
pub highlight_foreground: Option<Color>,
pub selection_background: Option<Color>,
}

impl Default for Settings {
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -409,3 +434,9 @@ fn is_env_var_truthy(name: &str) -> bool {
Err(_) => false,
}
}

fn get_env_var_color(name: &str) -> Option<Color> {
env::var(name)
.ok()
.and_then(|val| Color::try_from(val.as_str()).ok())
}