From 2c2d140796649c95a83a9916d44b46d7a25c16db Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Mon, 25 Feb 2019 22:00:37 +0000 Subject: [PATCH] Customize colors --- src/colors.rs | 30 ++++++++++++++++++++++++++++++ src/main.rs | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/colors.rs diff --git a/src/colors.rs b/src/colors.rs new file mode 100644 index 0000000..0713d1d --- /dev/null +++ b/src/colors.rs @@ -0,0 +1,30 @@ +use rustbox::Color; +use std::collections::HashMap; + +const COLORS: [(&'static str, Color); 9] = [ + ("black", Color::Black), + ("red", Color::Red), + ("green", Color::Green), + ("yellow", Color::Yellow), + ("blue", Color::Blue), + ("magenta", Color::Magenta), + ("cyan", Color::Cyan), + ("white", Color::White), + ("default", Color::Default), +]; + +pub fn get_color(color_name: &str) -> Color { + let available_colors: HashMap<&str, Color> = COLORS.iter().cloned().collect(); + + available_colors[&color_name] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn match_color () { + assert_eq!(get_color("green"), Color::Green); + } +} diff --git a/src/main.rs b/src/main.rs index 819af31..28b0f99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate clap; mod state; mod alphabets; +mod colors; use self::clap::{Arg, App}; use std::char; @@ -28,12 +29,31 @@ fn app_args<'a> () -> clap::ArgMatches<'a> { .arg(Arg::with_name("alphabet") .help("Sets the alphabet") .long("alphabet") - .short("a") + .takes_value(true)) + .arg(Arg::with_name("foreground_color") + .help("Sets the foregroud color for matches") + .long("fg-color") + .takes_value(true)) + .arg(Arg::with_name("background_color") + .help("Sets the background color for matches") + .long("bg-color") + .takes_value(true)) + .arg(Arg::with_name("hint_foreground_color") + .help("Sets the foregroud color for hints") + .long("hint-fg-color") + .takes_value(true)) + .arg(Arg::with_name("hint_background_color") + .help("Sets the background color for hints") + .long("hint-bg-color") .takes_value(true)) .arg(Arg::with_name("reverse") .help("Reverse the order for assigned hints") .long("reverse") .short("r")) + .arg(Arg::with_name("select_foreground_color") + .help("Sets the foregroud color for selection") + .long("select-fg-color") + .takes_value(true)) .arg(Arg::with_name("unique") .help("Don't show duplicated hints for the same match") .long("unique") @@ -47,6 +67,12 @@ fn main() { let reverse = args.is_present("reverse"); let unique = args.is_present("unique"); + let foreground_color = colors::get_color(args.value_of("foreground_color").unwrap_or("green")); + let background_color = colors::get_color(args.value_of("background_color").unwrap_or("black")); + let hint_foreground_color = colors::get_color(args.value_of("hint_foreground_color").unwrap_or("yellow")); + let hint_background_color = colors::get_color(args.value_of("hint_background_color").unwrap_or("black")); + let select_foreground_color = colors::get_color(args.value_of("select_foreground_color").unwrap_or("blue")); + let execution = exec_command(format!("tmux capture-pane -e -J -p")); let output = String::from_utf8_lossy(&execution.stdout); let lines = output.split("\n").collect::>(); @@ -85,9 +111,9 @@ fn main() { for mat in matches.iter() { let selected_color = if selected == Some(mat) { - Color::Blue + select_foreground_color } else { - Color::Green + foreground_color }; // TODO: Find long utf sequences and extract it from mat.x @@ -101,10 +127,10 @@ fn main() { let offset = (mat.x as usize) - extra; - rustbox.print(offset, mat.y as usize, rustbox::RB_NORMAL, selected_color, Color::Black, mat.text); + rustbox.print(offset, mat.y as usize, rustbox::RB_NORMAL, selected_color, background_color, mat.text); if let Some(ref hint) = mat.hint { - rustbox.print(offset, mat.y as usize, rustbox::RB_BOLD, Color::Yellow, Color::Black, hint.as_str()); + rustbox.print(offset, mat.y as usize, rustbox::RB_BOLD, hint_foreground_color, hint_background_color, hint.as_str()); } }