Allow user to set theme with flag (#481)

closes #480
This commit is contained in:
David Karrick 2021-01-27 21:24:27 +00:00 committed by GitHub
parent ffdee44105
commit 530e077fe7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 23 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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,11 +265,19 @@ 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")
@ -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<()> {

View file

@ -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()
}
}