From 05210b9510b797f7738d5b2d51e8a6877f2d5283 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Fri, 4 Oct 2019 22:30:46 +0900 Subject: refactor: Go from Rust workspaces to a package with nested packages (#480) --- src/config.rs | 589 ++++++++++++++++++++++++++++++++++++++++++++ src/configs/aws.rs | 29 +++ src/configs/battery.rs | 52 ++++ src/configs/character.rs | 38 +++ src/configs/dotnet.rs | 31 +++ src/configs/mod.rs | 57 +++++ src/configs/rust.rs | 29 +++ src/context.rs | 294 ++++++++++++++++++++++ src/init/mod.rs | 170 +++++++++++++ src/init/starship.bash | 69 ++++++ src/init/starship.fish | 15 ++ src/init/starship.zsh | 58 +++++ src/lib.rs | 9 + src/main.rs | 137 +++++++++++ src/module.rs | 356 ++++++++++++++++++++++++++ src/modules/aws.rs | 27 ++ src/modules/battery.rs | 102 ++++++++ src/modules/character.rs | 59 +++++ src/modules/cmd_duration.rs | 101 ++++++++ src/modules/directory.rs | 312 +++++++++++++++++++++++ src/modules/dotnet.rs | 314 +++++++++++++++++++++++ src/modules/env_var.rs | 43 ++++ src/modules/git_branch.rs | 65 +++++ src/modules/git_state.rs | 162 ++++++++++++ src/modules/git_status.rs | 196 +++++++++++++++ src/modules/golang.rs | 82 ++++++ src/modules/hostname.rs | 41 +++ src/modules/java.rs | 105 ++++++++ src/modules/jobs.rs | 34 +++ src/modules/kubernetes.rs | 182 ++++++++++++++ src/modules/line_break.rs | 15 ++ src/modules/memory_usage.rs | 91 +++++++ src/modules/mod.rs | 68 +++++ src/modules/nix_shell.rs | 56 +++++ src/modules/nodejs.rs | 49 ++++ src/modules/package.rs | 166 +++++++++++++ src/modules/python.rs | 122 +++++++++ src/modules/ruby.rs | 61 +++++ src/modules/rust.rs | 289 ++++++++++++++++++++++ src/modules/time.rs | 105 ++++++++ src/modules/username.rs | 53 ++++ src/print.rs | 65 +++++ src/segment.rs | 66 +++++ src/utils.rs | 12 + 44 files changed, 4976 insertions(+) create mode 100644 src/config.rs create mode 100644 src/configs/aws.rs create mode 100644 src/configs/battery.rs create mode 100644 src/configs/character.rs create mode 100644 src/configs/dotnet.rs create mode 100644 src/configs/mod.rs create mode 100644 src/configs/rust.rs create mode 100644 src/context.rs create mode 100644 src/init/mod.rs create mode 100644 src/init/starship.bash create mode 100644 src/init/starship.fish create mode 100644 src/init/starship.zsh create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/module.rs create mode 100644 src/modules/aws.rs create mode 100644 src/modules/battery.rs create mode 100644 src/modules/character.rs create mode 100644 src/modules/cmd_duration.rs create mode 100644 src/modules/directory.rs create mode 100644 src/modules/dotnet.rs create mode 100644 src/modules/env_var.rs create mode 100644 src/modules/git_branch.rs create mode 100644 src/modules/git_state.rs create mode 100644 src/modules/git_status.rs create mode 100644 src/modules/golang.rs create mode 100644 src/modules/hostname.rs create mode 100644 src/modules/java.rs create mode 100644 src/modules/jobs.rs create mode 100644 src/modules/kubernetes.rs create mode 100644 src/modules/line_break.rs create mode 100644 src/modules/memory_usage.rs create mode 100644 src/modules/mod.rs create mode 100644 src/modules/nix_shell.rs create mode 100644 src/modules/nodejs.rs create mode 100644 src/modules/package.rs create mode 100644 src/modules/python.rs create mode 100644 src/modules/ruby.rs create mode 100644 src/modules/rust.rs create mode 100644 src/modules/time.rs create mode 100644 src/modules/username.rs create mode 100644 src/print.rs create mode 100644 src/segment.rs create mode 100644 src/utils.rs (limited to 'src') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 000000000..6f5025179 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,589 @@ +use crate::configs::StarshipRootConfig; +use crate::utils; +use ansi_term::{Color, Style}; + +use std::clone::Clone; +use std::marker::Sized; + +use dirs::home_dir; +use std::env; +use toml::Value; + +/// Root config of a module. +pub trait RootModuleConfig<'a> +where + Self: ModuleConfig<'a>, +{ + /// Create a new root module config with default values. + fn new() -> Self; + + /// Load root module config from given Value and fill unset variables with default + /// values. + fn load(config: &'a Value) -> Self { + Self::new().load_config(config) + } + + /// Helper function that will call RootModuleConfig::load(config) if config is Some, + /// or RootModuleConfig::new() if config is None. + fn try_load(config: Option<&'a Value>) -> Self { + if let Some(config) = config { + Self::load(config) + } else { + Self::new() + } + } +} + +/// Parsable config. +pub trait ModuleConfig<'a> +where + Self: Sized + Clone, +{ + /// Construct a `ModuleConfig` from a toml value. + fn from_config(_config: &'a Value) -> Option { + None + } + + /// Merge `self` with config from a toml table. + fn load_config(&self, config: &'a Value) -> Self { + Self::from_config(config).unwrap_or_else(|| self.clone()) + } +} + +// TODO: Add logging to default implementations +impl<'a> ModuleConfig<'a> for &'a str { + fn from_config(config: &'a Value) -> Option { + config.as_str() + } +} + +impl<'a> ModuleConfig<'a> for Style { + fn from_config(config: &Value) -> Option { + parse_style_string(config.as_str()?) + } +} + +impl<'a> ModuleConfig<'a> for bool { + fn from_config(config: &Value) -> Option { + config.as_bool() + } +} + +impl<'a> ModuleConfig<'a> for i64 { + fn from_config(config: &Value) -> Option { + config.as_integer() + } +} + +impl<'a> ModuleConfig<'a> for f64 { + fn from_config(config: &Value) -> Option { + config.as_float() + } +} + +impl<'a, T> ModuleConfig<'a> for Vec +where + T: ModuleConfig<'a>, +{ + fn from_config(config: &'a Value) -> Option { + config + .as_array()? + .iter() + .map(|value| T::from_config(value)) + .collect() + } +} + +impl<'a, T> ModuleConfig<'a> for Option +where + T: ModuleConfig<'a> + Sized, +{ + fn from_config(config: &'a Value) -> Option { + Some(T::from_config(config)) + } +} + +/// Root config of starship. +pub struct StarshipConfig { + pub config: Option, +} + +impl StarshipConfig { + /// Initialize the Config struct + pub fn initialize() -> Self { + if let Some(file_data) = Self::config_from_file() { + StarshipConfig { + config: Some(file_data), + } + } else { + StarshipConfig { + config: Some(Value::Table(toml::value::Table::new())), + } + } + } + + /// Create a config from a starship configuration file + fn config_from_file() -> Option { + let file_path = if let Ok(path) = env::var("STARSHIP_CONFIG") { + // Use $STARSHIP_CONFIG as the config path if available + log::debug!("STARSHIP_CONFIG is set: \n{}", &path); + path + } else { + // Default to using ~/.config/starship.toml + log::debug!("STARSHIP_CONFIG is not set"); + let config_path = home_dir()?.join(".config/starship.toml"); + let config_path_str = config_path.to_str()?.to_owned(); + log::debug!("Using default config path: {}", config_path_str); + config_path_str + }; + + let toml_content = match utils::read_file(&file_path) { + Ok(content) => { + log::trace!("Config file content: \n{}", &content); + Some(content) + } + Err(e) => { + log::debug!("Unable to read config file content: \n{}", &e); + None + } + }?; + + let config = toml::from_str(&toml_content).ok()?; + log::debug!("Config parsed: \n{:?}", &config); + Some(config) + } + + /// Get the subset of the table for a module by its name + pub fn get_module_config(&self, module_name: &str) -> Option<&Value> { + let module_config = self.config.as_ref()?.as_table()?.get(module_name); + if module_config.is_some() { + log::debug!( + "Config found for \"{}\": \n{:?}", + &module_name, + &module_config + ); + } else { + log::trace!("No config found for \"{}\"", &module_name); + } + module_config + } + + pub fn get_root_config(&self) -> StarshipRootConfig { + if let Some(root_config) = &self.config { + StarshipRootConfig::load(root_config) + } else { + StarshipRootConfig::new() + } + } +} + +#[derive(Clone)] +pub struct SegmentConfig<'a> { + pub value: &'a str, + pub style: Option