diff options
author | Matan Kushner <hello@matchai.me> | 2019-07-27 18:25:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-27 18:25:13 -0400 |
commit | 0bc28c521df789d3aae5ceb541ee336aefa14340 (patch) | |
tree | 2f5aca63ac63e9970ff0587f5e4dc973d56cc0a9 /src | |
parent | 95ce43ee70d51ce1181806b7c0f1ce623a618b4b (diff) |
feat: Add configuration for add_newline (#116)
- Replace TableExt with a Config trait that extends toml::value::Table
Add configuration for add_newline
- add_newline is a root-level configuration value. When set to false, the initial newline before the prompt is removed.
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 40 | ||||
-rw-r--r-- | src/context.rs | 6 | ||||
-rw-r--r-- | src/module.rs | 2 | ||||
-rw-r--r-- | src/print.rs | 6 |
4 files changed, 28 insertions, 26 deletions
diff --git a/src/config.rs b/src/config.rs index f2045a688..d5861b9f0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,25 +2,33 @@ use crate::utils; use std::env; use dirs::home_dir; +use toml::value::Table; -pub struct Config { - data: toml::value::Table, +pub trait Config { + fn initialize() -> Table; + fn config_from_file() -> Option<Table>; + fn get_module_config(&self, module_name: &str) -> Option<&Table>; + + // Config accessor methods + fn get_as_bool(&self, key: &str) -> Option<bool>; + fn get_as_str(&self, key: &str) -> Option<&str>; + + // Internal implementation for accessors + fn get_config(&self, key: &str) -> Option<&toml::value::Value>; } -impl Config { +impl Config for Table { /// Initialize the Config struct - pub fn initialize() -> Config { - if let Some(file_data) = Config::config_from_file() { - return Config { data: file_data }; + fn initialize() -> Table { + if let Some(file_data) = Table::config_from_file() { + return file_data; } - Config { - data: toml::value::Table::new(), - } + Table::new() } /// Create a config from a starship configuration file - fn config_from_file() -> Option<toml::value::Table> { + fn config_from_file() -> Option<Table> { let file_path = match env::var("STARSHIP_CONFIG") { Ok(path) => { // Use $STARSHIP_CONFIG as the config path if available @@ -55,9 +63,8 @@ impl Config { } /// Get the subset of the table for a module by its name - pub fn get_module_config(&self, module_name: &str) -> Option<&toml::value::Table> { + fn get_module_config(&self, module_name: &str) -> Option<&toml::value::Table> { let module_config = self - .data .get(module_name) .map(toml::Value::as_table) .unwrap_or(None); @@ -74,16 +81,7 @@ impl Config { module_config } -} - -/// Extends `toml::value::Table` with useful methods -pub trait TableExt { - fn get_config(&self, key: &str) -> Option<&toml::value::Value>; - fn get_as_bool(&self, key: &str) -> Option<bool>; - fn get_as_str(&self, key: &str) -> Option<&str>; -} -impl TableExt for toml::value::Table { /// Get the config value for a given key fn get_config(&self, key: &str) -> Option<&toml::value::Value> { log::trace!("Looking for config key \"{}\"", key); diff --git a/src/context.rs b/src/context.rs index e8ca107f1..3e3640ace 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,4 @@ -use crate::config::{Config, TableExt}; +use crate::config::Config; use crate::module::Module; use clap::ArgMatches; @@ -13,7 +13,7 @@ use std::path::PathBuf; /// of the prompt. pub struct Context<'a> { /// The deserialized configuration map from the user's `starship.toml` file. - pub config: Config, + pub config: toml::value::Table, /// The current working directory that starship is being called in. pub current_dir: PathBuf, @@ -51,7 +51,7 @@ impl<'a> Context<'a> { where T: Into<PathBuf>, { - let config = Config::initialize(); + let config = toml::value::Table::initialize(); // TODO: Currently gets the physical directory. Get the logical directory. let current_dir = Context::expand_tilde(dir.into()); diff --git a/src/module.rs b/src/module.rs index 3e530c3f6..af985ea3a 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,4 +1,4 @@ -use crate::config::TableExt; +use crate::config::Config; use crate::segment::Segment; use ansi_term::Style; use ansi_term::{ANSIString, ANSIStrings}; diff --git a/src/print.rs b/src/print.rs index ae712e123..3cd185e41 100644 --- a/src/print.rs +++ b/src/print.rs @@ -2,6 +2,7 @@ use clap::ArgMatches; use rayon::prelude::*; use std::io::{self, Write}; +use crate::config::Config; use crate::context::Context; use crate::module::Module; use crate::modules; @@ -23,12 +24,15 @@ const PROMPT_ORDER: &[&str] = &[ pub fn prompt(args: ArgMatches) { let context = Context::new(args); + let config = &context.config; let stdout = io::stdout(); let mut handle = stdout.lock(); // Write a new line before the prompt - writeln!(handle).unwrap(); + if config.get_as_bool("add_newline") != Some(false) { + writeln!(handle).unwrap(); + } let modules = PROMPT_ORDER .par_iter() |