diff options
author | David Karrick <48275422+Th3Whit3Wolf@users.noreply.github.com> | 2021-01-27 21:24:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-27 22:24:27 +0100 |
commit | 530e077fe772daee96d3f7a88259b82957960c9d (patch) | |
tree | 9dd97ec6b29182586cf73cbafed5880f843610d3 | |
parent | ffdee4410537f9dcfcfa2589765f9a9081a98bd6 (diff) |
Allow user to set theme with flag (#481)
closes #480
-rw-r--r-- | THEMES.md | 4 | ||||
-rw-r--r-- | src/app.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 31 | ||||
-rw-r--r-- | src/ui/style.rs | 24 |
4 files changed, 41 insertions, 23 deletions
@@ -3,11 +3,13 @@ default on light terminal: ![](assets/light-theme.png) -to change the colors of the program you have to modify `theme.ron` file +to change the colors of the default theme you have to modify `theme.ron` file [Ron format](https://github.com/ron-rs/ron) located at config path. The path differs depending on the operating system: * `$HOME/Library/Application Support/gitui/theme.ron` (mac) * `$XDG_CONFIG_HOME/gitui/theme.ron` (linux using XDG) * `$HOME/.config/gitui/theme.ron` (linux) +Alternatively you may make a theme in the same directory mentioned above with and select with the `-t` flag followed by the name of the file in the directory. E.g. If you are on linux calling `gitui -t arc.ron` wil use `$XDG_CONFIG_HOME/gitui/arc.ron` or `$HOME/.config/gitui/arc.ron` + Valid colors can be found in tui-rs' [Color](https://docs.rs/tui/0.12.0/tui/style/enum.Color.html) struct. note that rgb colors might not be supported in every terminal. @@ -22,7 +22,7 @@ use crossbeam_channel::Sender; use crossterm::event::{Event, KeyEvent}; use std::{ cell::{Cell, RefCell}, - path::Path, + path::{Path, PathBuf}, rc::Rc, }; use tui::{ @@ -70,10 +70,11 @@ impl App { pub fn new( sender: &Sender<AsyncNotification>, input: Input, + theme_path: PathBuf, ) -> Self { let queue = Queue::default(); - let theme = Rc::new(Theme::init()); + let theme = Rc::new(Theme::init(theme_path)); let key_config = Rc::new(KeyConfig::init()); Self { diff --git a/src/main.rs b/src/main.rs index 560d52ee..e1e45ec0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,8 +73,12 @@ pub enum QueueEvent { InputEvent(InputEvent), } +struct CliArgs { + theme: PathBuf, +} + fn main() -> Result<()> { - process_cmdline()?; + let cliargs = process_cmdline()?; let _profiler = Profiler::new(); @@ -100,7 +104,7 @@ fn main() -> Result<()> { let ticker = tick(TICK_INTERVAL); let spinner_ticker = tick(SPINNER_INTERVAL); - let mut app = App::new(&tx_git, input); + let mut app = App::new(&tx_git, input, cliargs.theme); let mut spinner = Spinner::default(); let mut first_update = true; @@ -261,12 +265,20 @@ fn setup_logging() -> Result<()> { Ok(()) } -fn process_cmdline() -> Result<()> { +fn process_cmdline() -> Result<CliArgs> { let app = ClapApp::new(crate_name!()) .author(crate_authors!()) .version(crate_version!()) .about(crate_description!()) .arg( + Arg::with_name("theme") + .help("Set the color theme (defaults to theme.ron)") + .short("t") + .long("theme") + .value_name("THEME") + .takes_value(true), + ) + .arg( Arg::with_name("logging") .help("Stores logging output into a cache directory") .short("l") @@ -290,8 +302,17 @@ fn process_cmdline() -> Result<()> { arg_matches.value_of("directory").unwrap_or("."); env::set_current_dir(directory)?; } - - Ok(()) + let arg_theme = + arg_matches.value_of("theme").unwrap_or("theme.ron"); + if get_app_config_path()?.join(arg_theme).is_file() { + Ok(CliArgs { + theme: get_app_config_path()?.join(arg_theme), + }) + } else { + Ok(CliArgs { + theme: get_app_config_path()?.join("theme.ron"), + }) + } } fn set_panic_handlers() -> Result<()> { diff --git a/src/ui/style.rs b/src/ui/style.rs index a1e59f8d..50b81b21 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -1,4 +1,3 @@ -use crate::get_app_config_path; use anyhow::Result; use asyncgit::{DiffLineType, StatusItemType}; use ron::{ @@ -230,19 +229,14 @@ impl Theme { .bg(self.push_gauge_bg) } - fn save(&self) -> Result<()> { - let theme_file = Self::get_theme_file()?; + // This will only be called when theme.ron doesn't already exists + fn save(&self, theme_file: PathBuf) -> Result<()> { let mut file = File::create(theme_file)?; let data = to_string_pretty(self, PrettyConfig::default())?; file.write_all(data.as_bytes())?; Ok(()) } - fn get_theme_file() -> Result<PathBuf> { - let app_home = get_app_config_path()?; - Ok(app_home.join("theme.ron")) - } - fn read_file(theme_file: PathBuf) -> Result<Self> { let mut f = File::open(theme_file)?; let mut buffer = Vec::new(); @@ -250,21 +244,21 @@ impl Theme { Ok(from_bytes(&buffer)?) } - fn init_internal() -> Result<Self> { - let file = Self::get_theme_file()?; - if file.exists() { - Ok(Self::read_file(file)?) + fn init_internal(theme: PathBuf) -> Result<Self> { + if theme.exists() { + Ok(Self::read_file(theme)?) } else { + // This will only be called when theme.ron doesn't already exists let def = Self::default(); - if def.save().is_err() { + if def.save(theme).is_err() { log::warn!("failed to store default theme to disk.") } Ok(def) } } - pub fn init() -> Self { - Self::init_internal().unwrap_or_default() + pub fn init(theme_path: PathBuf) -> Self { + Self::init_internal(theme_path).unwrap_or_default() } } |