summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorZhenhui Xie <xiezh0831@yahoo.co.jp>2020-07-08 06:45:32 +0800
committerGitHub <noreply@github.com>2020-07-07 18:45:32 -0400
commitec76fafff08933f6f31fb99ea974bdb5ae97a0af (patch)
treebb2c822cdf291635f03d27677c419488ecf77f53 /src
parent0f52b7b12e8c1a2060aa873a68032937dfa2c044 (diff)
feat: refactor modules to use format strings (#1374)
Diffstat (limited to 'src')
-rw-r--r--src/config.rs130
-rw-r--r--src/configs/aws.rs40
-rw-r--r--src/configs/battery.rs31
-rw-r--r--src/configs/character.rs23
-rw-r--r--src/configs/cmd_duration.rs9
-rw-r--r--src/configs/conda.rs21
-rw-r--r--src/configs/crystal.rs13
-rw-r--r--src/configs/custom.rs17
-rw-r--r--src/configs/directory.rs9
-rw-r--r--src/configs/docker_context.rs15
-rw-r--r--src/configs/dotnet.rs49
-rw-r--r--src/configs/elixir.rs17
-rw-r--r--src/configs/elm.rs15
-rw-r--r--src/configs/env_var.rs17
-rw-r--r--src/configs/erlang.rs15
-rw-r--r--src/configs/git_branch.rs15
-rw-r--r--src/configs/git_commit.rs15
-rw-r--r--src/configs/git_state.rs39
-rw-r--r--src/configs/git_status.rs75
-rw-r--r--src/configs/go.rs15
-rw-r--r--src/configs/hg_branch.rs15
-rw-r--r--src/configs/hostname.rs11
-rw-r--r--src/configs/java.rs13
-rw-r--r--src/configs/jobs.rs13
-rw-r--r--src/configs/julia.rs15
-rw-r--r--src/configs/kubernetes.rs17
-rw-r--r--src/configs/memory_usage.rs23
-rw-r--r--src/configs/nim.rs15
-rw-r--r--src/configs/nix_shell.rs23
-rw-r--r--src/configs/nodejs.rs13
-rw-r--r--src/configs/ocaml.rs15
-rw-r--r--src/configs/package.rs13
-rw-r--r--src/configs/php.rs13
-rw-r--r--src/configs/purescript.rs15
-rw-r--r--src/configs/python.rs17
-rw-r--r--src/configs/ruby.rs13
-rw-r--r--src/configs/rust.rs15
-rw-r--r--src/configs/singularity.rs19
-rw-r--r--src/configs/starship_root.rs108
-rw-r--r--src/configs/terraform.rs19
-rw-r--r--src/configs/time.rs11
-rw-r--r--src/configs/username.rs15
-rw-r--r--src/configs/zig.rs15
-rw-r--r--src/formatter/mod.rs1
-rw-r--r--src/formatter/model.rs86
-rw-r--r--src/formatter/parser.rs71
-rw-r--r--src/formatter/spec.pest51
-rw-r--r--src/formatter/string_formatter.rs616
-rw-r--r--src/module.rs130
-rw-r--r--src/modules/aws.rs98
-rw-r--r--src/modules/battery.rs92
-rw-r--r--src/modules/character.rs55
-rw-r--r--src/modules/cmd_duration.rs37
-rw-r--r--src/modules/conda.rs34
-rw-r--r--src/modules/crystal.rs31
-rw-r--r--src/modules/custom.rs56
-rw-r--r--src/modules/directory.rs55
-rw-r--r--src/modules/docker_context.rs47
-rw-r--r--src/modules/dotnet.rs91
-rw-r--r--src/modules/elixir.rs44
-rw-r--r--src/modules/elm.rs32
-rw-r--r--src/modules/env_var.rs38
-rw-r--r--src/modules/erlang.rs30
-rw-r--r--src/modules/git_branch.rs64
-rw-r--r--src/modules/git_commit.rs44
-rw-r--r--src/modules/git_state.rs171
-rw-r--r--src/modules/git_status.rs475
-rw-r--r--src/modules/golang.rs36
-rw-r--r--src/modules/hg_branch.rs36
-rw-r--r--src/modules/hostname.rs27
-rw-r--r--src/modules/java.rs30
-rw-r--r--src/modules/jobs.rs44
-rw-r--r--src/modules/julia.rs37
-rw-r--r--src/modules/kubernetes.rs56
-rw-r--r--src/modules/line_break.rs11
-rw-r--r--src/modules/memory_usage.rs87
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/nim.rs39
-rw-r--r--src/modules/nix_shell.rs58
-rw-r--r--src/modules/nodejs.rs37
-rw-r--r--src/modules/ocaml.rs36
-rw-r--r--src/modules/package.rs46
-rw-r--r--src/modules/php.rs34
-rw-r--r--src/modules/purescript.rs31
-rw-r--r--src/modules/python.rs51
-rw-r--r--src/modules/ruby.rs38
-rw-r--r--src/modules/rust.rs43
-rw-r--r--src/modules/singularity.rs40
-rw-r--r--src/modules/terraform.rs44
-rw-r--r--src/modules/time.rs40
-rw-r--r--src/modules/username.rs36
-rw-r--r--src/modules/zig.rs27
-rw-r--r--src/print.rs198
93 files changed, 2821 insertions, 1818 deletions
diff --git a/src/config.rs b/src/config.rs
index fe60edad8..be55af0f8 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -235,37 +235,84 @@ impl StarshipConfig {
/// 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);
+ let module_config = self.get_config(&[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
}
+ /// Get the value of the config in a specific path
+ pub fn get_config(&self, path: &[&str]) -> Option<&Value> {
+ let mut prev_table = self.config.as_ref()?.as_table()?;
+
+ assert_ne!(
+ path.len(),
+ 0,
+ "Starship::get_config called with an empty path"
+ );
+
+ let (table_options, _) = path.split_at(path.len() - 1);
+
+ // Assumes all keys except the last in path has a table
+ for option in table_options {
+ match prev_table.get(*option) {
+ Some(value) => match value.as_table() {
+ Some(value) => {
+ prev_table = value;
+ }
+ None => {
+ log::trace!(
+ "No config found for \"{}\": \"{}\" is not a table",
+ path.join("."),
+ &option
+ );
+ return None;
+ }
+ },
+ None => {
+ log::trace!(
+ "No config found for \"{}\": Option \"{}\" not found",
+ path.join("."),
+ &option
+ );
+ return None;
+ }
+ }
+ }
+
+ let last_option = path.last().unwrap();
+ let value = prev_table.get(*last_option);
+ if value.is_none() {
+ log::trace!(
+ "No config found for \"{}\": Option \"{}\" not found",
+ path.join("."),
+ &last_option
+ );
+ };
+ value
+ }
+
/// Get the subset of the table for a custom module by its name
pub fn get_custom_module_config(&self, module_name: &str) -> Option<&Value> {
- let module_config = self.get_custom_modules()?.get(module_name);
+ let module_config = self.get_config(&["custom", module_name]);
if module_config.is_some() {
log::debug!(
"Custom config found for \"{}\": \n{:?}",
&module_name,
&module_config
);
- } else {
- log::trace!("No custom config found for \"{}\"", &module_name);
}
module_config
}
/// Get the table of all the registered custom modules, if any
pub fn get_custom_modules(&self) -> Option<&toml::value::Table> {
- self.config.as_ref()?.as_table()?.get("custom")?.as_table()
+ self.get_config(&["custom"])?.as_table()
}
pub fn get_root_config(&self) -> StarshipRootConfig {
@@ -277,75 +324,6 @@ impl StarshipConfig {
}
}
-#[derive(Clone)]
-pub struct SegmentConfig<'a> {
- pub value: &'a str,
- pub style: Option<Style>,
-}
-
-impl<'a> ModuleConfig<'a> for SegmentConfig<'a> {
- fn from_config(config: &'a Value) -> Option<Self> {
- match config {
- Value::String(ref config_str) => Some(Self {
- value: config_str,
- style: None,
- }),
- Value::Table(ref config_table) => Some(Self {
- value: config_table.get("value")?.as_str()?,
- style: config_table.get("style").and_then(<Style>::from_config),
- }),
- _ => None,
- }
- }
-
- fn load_config(&self, config: &'a Value) -> Self {
- let mut new_config = self.clone();
- match config {
- Value::String(ref config_str) => {
- new_config.value = config_str;
- }
- Value::Table(ref config_table) => {
- if let Some(Value::String(value)) = config_table.get("value") {
- new_config.value = value;
- };
- if let Some(style) = config_table.get("style") {
- new_config.style = <Style>::from_config(style);
- };
- }
- _ => {}
- };
- new_config
- }
-}
-
-impl<'a> SegmentConfig<'a> {
- pub fn new(value: &'a str) -> Self {
- Self { value, style: None }
- }
-
- /// Immutably set value
- pub fn with_value(&self, value: &'a str) -> Self {
- Self {
- value,
- style: self.style,
- }
- }
-
- /// Immutably set style
- pub fn with_style(&self, style: Option<Style>) -> Self {
- Self {
- value: self.value,
- style,
- }
- }
-}
-
-impl Default for SegmentConfig<'static> {
- fn default() -> Self {
- Self::new("")
- }
-}
-
/** Parse a style string which represents an ansi style. Valid tokens in the style
string include the following:
- 'fg:<color>' (specifies that the color read should be a foreground color)
diff --git a/src/configs/aws.rs b/src/configs/aws.rs
index 228558a24..89a9d871d 100644
--- a/src/configs/aws.rs
+++ b/src/configs/aws.rs
@@ -1,48 +1,24 @@
-use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
-use std::collections::HashMap;
-
-use ansi_term::{Color, Style};
+use crate::config::{ModuleConfig, RootModuleConfig};
use starship_module_config_derive::ModuleConfig;
-
-#[derive(Clone, PartialEq)]
-pub enum AwsItems {
- All,
- Region,
- Profile,
-}
+use std::collections::HashMap;
#[derive(Clone, ModuleConfig)]
pub struct AwsConfig<'a> {
- pub symbol: SegmentConfig<'a>,
- pub profile: SegmentConfig<'a>,
- pub region: SegmentConfig<'a>,
- pub style: Style,
+ pub format: &'a str,
+ pub symbol: &'a str,
+ pub style: &'a str,
pub disabled: bool,
- pub displayed_items: AwsItems,
pub region_aliases: HashMap<String, &'a str>,
}
impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
fn new() -> Self {
AwsConfig {
- symbol: SegmentConfig::new("☁️ "),
- profile: SegmentConfig::default(),
- region: SegmentConfig::default(),
- style: Color::Yellow.bold(),
+ format: "on [$symbol$profile(\\($region\\))]($style) ",
+ symbol: "☁️ ",
+ style: "bold yellow",
disabled: false,
- displayed_items: AwsItems::All,
region_aliases: HashMap::new(),
}
}
}
-
-impl<'a> ModuleConfig<'a> for AwsItems {
- fn from_config(config: &toml::Value) -> Option<Self> {
- match config.as_str()? {
- "all" => Some(AwsItems::All),
- "region" => Some(AwsItems::Region),
- "profile" => Some(AwsItems::Profile),
- _ => None,
- }
- }
-}
diff --git a/src/configs/battery.rs b/src/configs/battery.rs
index d86227d79..ec7287705 100644
--- a/src/configs/battery.rs
+++ b/src/configs/battery.rs
@@ -1,40 +1,39 @@
-use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
+use crate::config::{ModuleConfig, RootModuleConfig};
-use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct BatteryConfig<'a> {
- pub full_symbol: SegmentConfig<'a>,
- pub charging_symbol: SegmentConfig<'a>,
- pub discharging_symbol: SegmentConfig<'a>,
- pub unknown_symbol: Option<SegmentConfig<'a>>,
- pub empty_symbol: Option<SegmentConfig<'a>>,
- pub display: Vec<BatteryDisplayConfig>,
+ pub full_symbol: &'a str,
+ pub charging_symbol: &'a str,
+ pub discharging_symbol: &'a str,
+ pub unknown_symbol: Option<&'a str>,
+ pub empty_symbol: Option<&'a str>,
+ pub display: Vec<BatteryDisplayConfig<'a>>,
pub disabled: bool,
- pub percentage: SegmentConfig<'a>,
+ pub format: &'a str,
}
impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
fn new() -> Self {
BatteryConfig {
- full_symbol: SegmentConfig::new("•"),
- charging_symbol: SegmentConfig::new("↑"),
- discharging_symbol: SegmentConfig::new("↓"),
+ full_symbol: "•",
+ charging_symbol: "↑",
+ discharging_symbol: "↓",
unknown_symbol: None,
empty_symbol: None,
+ format: "[$symbol$percentage]($style) ",
display: vec![BatteryDisplayConfig {
threshold: 10,
- style: Color::Red.bold(),
+ style: "red bold",
}],
disabled: false,
- percentage: SegmentConfig::default(),
}
}
}
#[derive(Clone, ModuleConfig)]
-pub struct BatteryDisplayConfig {
+pub struct BatteryDisplayConfig<'a> {
pub threshold: i64,
- pub style: Style,
+ pub style: &'a str,
}
diff --git a/src/configs/character.rs b/src/configs/character.rs
index 92f977ebb..7486890e7 100644
--- a/src/configs/character.rs
+++ b/src/configs/character.rs
@@ -1,28 +1,23 @@
-use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
+use crate::config::{ModuleConfig, RootModuleConfig};
-use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct CharacterConfig<'a> {
- pub symbol: SegmentConfig<'a>,
- pub error_symbol: SegmentConfig<'a>,
- pub vicmd_symbol: SegmentConfig<'a>,
- pub use_symbol_for_status: bool,
- pub style_success: Style,
- pub style_failure: Style,
+ pub format: &'a str,
+ pub success_symbol: &'a str,
+ pub error_symbol: &'a str,
+ pub vicmd_symbol: &'a str,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> {
fn new() -> Self {
CharacterConfig {
- symbol: SegmentConfig::new("❯"),
- error_symbol: SegmentConfig::new("✖