1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 21:57:41 +00:00

fix: only print root level config logs a single time (#3132)

This commit is contained in:
David Knaack 2021-10-27 15:13:17 +02:00 committed by GitHub
parent b2c48358c3
commit c4439531d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 33 deletions

View File

@ -1,4 +1,3 @@
use crate::configs::StarshipRootConfig;
use crate::utils; use crate::utils;
use ansi_term::{Color, Style}; use ansi_term::{Color, Style};
use indexmap::IndexMap; use indexmap::IndexMap;
@ -63,6 +62,12 @@ impl<'a> ModuleConfig<'a> for &'a str {
} }
} }
impl<'a> ModuleConfig<'a> for String {
fn from_config(config: &'a Value) -> Option<Self> {
config.as_str().map(std::borrow::ToOwned::to_owned)
}
}
impl<'a> ModuleConfig<'a> for Style { impl<'a> ModuleConfig<'a> for Style {
fn from_config(config: &Value) -> Option<Self> { fn from_config(config: &Value) -> Option<Self> {
parse_style_string(config.as_str()?) parse_style_string(config.as_str()?)
@ -346,14 +351,6 @@ impl StarshipConfig {
pub fn get_env_var_modules(&self) -> Option<&toml::value::Table> { pub fn get_env_var_modules(&self) -> Option<&toml::value::Table> {
self.get_config(&["env_var"])?.as_table() self.get_config(&["env_var"])?.as_table()
} }
pub fn get_root_config(&self) -> StarshipRootConfig {
if let Some(root_config) = &self.config {
StarshipRootConfig::load(root_config)
} else {
StarshipRootConfig::default()
}
}
} }
/** Parse a style string which represents an ansi style. Valid tokens in the style /** Parse a style string which represents an ansi style. Valid tokens in the style

View File

@ -76,8 +76,8 @@ pub use starship_root::*;
#[serde(default)] #[serde(default)]
pub struct FullConfig<'a> { pub struct FullConfig<'a> {
// Root config // Root config
pub format: &'a str, pub format: String,
pub right_format: &'a str, pub right_format: String,
pub scan_timeout: u64, pub scan_timeout: u64,
pub command_timeout: u64, pub command_timeout: u64,
pub add_newline: bool, pub add_newline: bool,
@ -152,8 +152,8 @@ pub struct FullConfig<'a> {
impl<'a> Default for FullConfig<'a> { impl<'a> Default for FullConfig<'a> {
fn default() -> Self { fn default() -> Self {
Self { Self {
format: "$all", format: "$all".to_string(),
right_format: "", right_format: "".to_string(),
scan_timeout: 30, scan_timeout: 30,
command_timeout: 500, command_timeout: 500,
add_newline: true, add_newline: true,

View File

@ -5,9 +5,9 @@ use std::cmp::Ordering;
// On changes please also update the `FullConfig` struct in `mod.rs` // On changes please also update the `FullConfig` struct in `mod.rs`
#[derive(Clone, Serialize)] #[derive(Clone, Serialize)]
pub struct StarshipRootConfig<'a> { pub struct StarshipRootConfig {
pub format: &'a str, pub format: String,
pub right_format: &'a str, pub right_format: String,
pub scan_timeout: u64, pub scan_timeout: u64,
pub command_timeout: u64, pub command_timeout: u64,
pub add_newline: bool, pub add_newline: bool,
@ -88,11 +88,11 @@ pub const PROMPT_ORDER: &[&str] = &[
]; ];
// On changes please also update `Default` for the `FullConfig` struct in `mod.rs` // On changes please also update `Default` for the `FullConfig` struct in `mod.rs`
impl<'a> Default for StarshipRootConfig<'a> { impl<'a> Default for StarshipRootConfig {
fn default() -> Self { fn default() -> Self {
StarshipRootConfig { StarshipRootConfig {
format: "$all", format: "$all".to_string(),
right_format: "", right_format: "".to_string(),
scan_timeout: 30, scan_timeout: 30,
command_timeout: 500, command_timeout: 500,
add_newline: true, add_newline: true,
@ -100,7 +100,7 @@ impl<'a> Default for StarshipRootConfig<'a> {
} }
} }
impl<'a> ModuleConfig<'a> for StarshipRootConfig<'a> { impl<'a> ModuleConfig<'a> for StarshipRootConfig {
fn load_config(&mut self, config: &'a toml::Value) { fn load_config(&mut self, config: &'a toml::Value) {
if let toml::Value::Table(config) = config { if let toml::Value::Table(config) = config {
config.iter().for_each(|(k, v)| match k.as_str() { config.iter().for_each(|(k, v)| match k.as_str() {

View File

@ -1,4 +1,5 @@
use crate::config::StarshipConfig; use crate::config::{RootModuleConfig, StarshipConfig};
use crate::configs::StarshipRootConfig;
use crate::module::Module; use crate::module::Module;
use crate::utils::{exec_cmd, CommandOutput}; use crate::utils::{exec_cmd, CommandOutput};
@ -64,8 +65,8 @@ pub struct Context<'a> {
#[cfg(feature = "battery")] #[cfg(feature = "battery")]
pub battery_info_provider: &'a (dyn crate::modules::BatteryInfoProvider + Send + Sync), pub battery_info_provider: &'a (dyn crate::modules::BatteryInfoProvider + Send + Sync),
/// Timeout for the execution of commands /// Starship root config
cmd_timeout: Duration, pub root_config: StarshipRootConfig,
} }
impl<'a> Context<'a> { impl<'a> Context<'a> {
@ -127,7 +128,10 @@ impl<'a> Context<'a> {
let current_dir = current_dir.canonicalize().unwrap_or(current_dir); let current_dir = current_dir.canonicalize().unwrap_or(current_dir);
let logical_dir = logical_path; let logical_dir = logical_path;
let cmd_timeout = Duration::from_millis(config.get_root_config().command_timeout); let root_config = config
.config
.as_ref()
.map_or_else(StarshipRootConfig::default, StarshipRootConfig::load);
let right = arguments.is_present("right"); let right = arguments.is_present("right");
@ -154,7 +158,7 @@ impl<'a> Context<'a> {
cmd: HashMap::new(), cmd: HashMap::new(),
#[cfg(feature = "battery")] #[cfg(feature = "battery")]
battery_info_provider: &crate::modules::BatteryInfoProviderImpl, battery_info_provider: &crate::modules::BatteryInfoProviderImpl,
cmd_timeout, root_config,
} }
} }
@ -279,8 +283,8 @@ impl<'a> Context<'a> {
pub fn dir_contents(&self) -> Result<&DirContents, std::io::Error> { pub fn dir_contents(&self) -> Result<&DirContents, std::io::Error> {
self.dir_contents.get_or_try_init(|| { self.dir_contents.get_or_try_init(|| {
let timeout = Duration::from_millis(self.config.get_root_config().scan_timeout); let timeout = self.root_config.scan_timeout;
DirContents::from_path_with_timeout(&self.current_dir, timeout) DirContents::from_path_with_timeout(&self.current_dir, Duration::from_millis(timeout))
}) })
} }
@ -318,7 +322,11 @@ impl<'a> Context<'a> {
return output.clone(); return output.clone();
} }
} }
exec_cmd(&cmd, args, self.cmd_timeout) exec_cmd(
&cmd,
args,
Duration::from_millis(self.root_config.command_timeout),
)
} }
} }

View File

@ -62,7 +62,7 @@ pub fn prompt(args: ArgMatches) {
} }
pub fn get_prompt(context: Context) -> String { pub fn get_prompt(context: Context) -> String {
let config = context.config.get_root_config(); let config = &context.root_config;
let mut buf = String::new(); let mut buf = String::new();
match std::env::var_os("TERM") { match std::env::var_os("TERM") {
@ -412,10 +412,10 @@ fn all_modules_uniq(module_list: &BTreeSet<String>) -> Vec<String> {
/// Load the correct formatter for the context (ie left prompt or right prompt) /// Load the correct formatter for the context (ie left prompt or right prompt)
/// and the list of all modules used in a format string /// and the list of all modules used in a format string
fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) { fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) {
let config = context.config.get_root_config(); let config = &context.root_config;
let lformatter = StringFormatter::new(config.format); let lformatter = StringFormatter::new(&config.format);
let rformatter = StringFormatter::new(config.right_format); let rformatter = StringFormatter::new(&config.right_format);
if lformatter.is_err() { if lformatter.is_err() {
log::error!("Error parsing `format`") log::error!("Error parsing `format`")
} }
@ -454,6 +454,7 @@ mod test {
format=">\n>" format=">\n>"
}), }),
}; };
context.root_config.right_format = "$character".to_string();
context.right = true; context.right = true;
let expected = String::from(">>"); // should strip new lines let expected = String::from(">>"); // should strip new lines

View File

@ -1,7 +1,8 @@
use crate::context::{Context, Shell}; use crate::context::{Context, Shell};
use crate::logger::StarshipLogger; use crate::logger::StarshipLogger;
use crate::{ use crate::{
config::StarshipConfig, config::{RootModuleConfig, StarshipConfig},
configs::StarshipRootConfig,
utils::{create_command, CommandOutput}, utils::{create_command, CommandOutput},
}; };
use log::{Level, LevelFilter}; use log::{Level, LevelFilter};
@ -78,6 +79,7 @@ impl<'a> ModuleRenderer<'a> {
/// Sets the config of the underlying context /// Sets the config of the underlying context
pub fn config(mut self, config: toml::Value) -> Self { pub fn config(mut self, config: toml::Value) -> Self {
self.context.root_config = StarshipRootConfig::load(&config);
self.context.config = StarshipConfig { self.context.config = StarshipConfig {
config: Some(config), config: Some(config),
}; };