diff options
author | David Knaack <davidkna@users.noreply.github.com> | 2021-03-31 17:31:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-31 17:31:55 +0200 |
commit | d06ba072a88da96448260ff7c97174a08aad669a (patch) | |
tree | 30ff17a324703e767a8bb19b0114d7d6ca2aea0c | |
parent | 2b15ca1a157fa21dd7d8432b2c51e1e2ecf87496 (diff) |
feat(config): allow printing default and computed config (#2521)
* feat: allow printing default and computed config
* fix custom modules
* actually fix custom modules
66 files changed, 252 insertions, 157 deletions
diff --git a/Cargo.lock b/Cargo.lock index cf30a0a1f..4372916fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -694,6 +694,7 @@ checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg", "hashbrown", + "serde", ] [[package]] @@ -1627,7 +1628,7 @@ dependencies = [ [[package]] name = "starship_module_config_derive" -version = "0.1.2" +version = "0.1.3" dependencies = [ "proc-macro2", "quote 1.0.9", diff --git a/Cargo.toml b/Cargo.toml index adb3360d2..9c7939b7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ term_size = "0.3.2" quick-xml = "0.22.0" rand = "0.8.3" serde = { version = "1.0.125", features = ["derive"] } -indexmap = "1.6.2" +indexmap = { version ="1.6.2", features = ["serde"] } notify-rust = { version = "4.3.0", optional = true } semver = "0.11.0" which = "4.1.0" diff --git a/docs/config/README.md b/docs/config/README.md index a752a5f10..f7b8bef42 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3,7 +3,7 @@ To get started configuring starship, create the following file: `~/.config/starship.toml`. ```sh -mkdir -p ~/.config && touch ~/.config/starship.toml +mkdir -p ~/.config && starship print-config --default > ~/.config/starship.toml ``` All configuration for starship is done in this [TOML](https://github.com/toml-lang/toml) file: diff --git a/src/config.rs b/src/config.rs index f8a0a3a13..0e6a35df5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use crate::configs::StarshipRootConfig; use crate::utils; use ansi_term::{Color, Style}; use indexmap::IndexMap; +use serde::Serialize; use std::clone::Clone; use std::collections::HashMap; @@ -174,7 +175,7 @@ where /// A wrapper around `Vec<T>` that implements `ModuleConfig`, and either /// accepts a value of type `T` or a list of values of type `T`. -#[derive(Clone, Default)] +#[derive(Clone, Default, Serialize)] pub struct VecOr<T>(pub Vec<T>); impl<'a, T> ModuleConfig<'a> for VecOr<T> @@ -479,7 +480,7 @@ mod tests { #[test] fn test_load_config() { - #[derive(Clone, ModuleConfig)] + #[derive(Clone, Default, ModuleConfig)] struct TestConfig<'a> { pub symbol: &'a str, pub disabled: bool, @@ -505,13 +506,13 @@ mod tests { #[test] fn test_load_nested_config() { - #[derive(Clone, ModuleConfig)] + #[derive(Clone, Default, ModuleConfig)] struct TestConfig<'a> { pub untracked: SegmentDisplayConfig<'a>, pub modified: SegmentDisplayConfig<'a>, } - #[derive(PartialEq, Debug, Clone, ModuleConfig)] + #[derive(PartialEq, Debug, Clone, Default, ModuleConfig)] struct SegmentDisplayConfig<'a> { pub value: &'a str, pub style: Style, @@ -552,7 +553,7 @@ mod tests { #[test] fn test_load_optional_config() { - #[derive(Clone, ModuleConfig)] + #[derive(Clone, Default, ModuleConfig)] struct TestConfig<'a> { pub optional: Option<&'a str>, pub hidden: Option<&'a str>, @@ -573,7 +574,7 @@ mod tests { #[test] fn test_load_enum_config() { - #[derive(Clone, ModuleConfig)] + #[derive(Clone, Default, ModuleConfig)] struct TestConfig { pub switch_a: Switch, pub switch_b: Switch, @@ -586,6 +587,12 @@ mod tests { Off, } + impl Default for Switch { + fn default() -> Self { + Self::Off + } + } + impl<'a> ModuleConfig<'a> for Switch { fn from_config(config: &'a Value) -> Option<Self> { match config.as_str()? { diff --git a/src/configs/aws.rs b/src/configs/aws.rs index adc3868e8..8cef66362 100644 --- a/src/configs/aws.rs +++ b/src/configs/aws.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; use std::collections::HashMap; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct AwsConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/battery.rs b/src/configs/battery.rs index 578fc040b..db313daa7 100644 --- a/src/configs/battery.rs +++ b/src/configs/battery.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct BatteryConfig<'a> { pub full_symbol: &'a str, pub charging_symbol: &'a str, @@ -32,7 +33,7 @@ impl<'a> Default for BatteryConfig<'a> { } } -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Default, Serialize)] pub struct BatteryDisplayConfig<'a> { pub threshold: i64, pub style: &'a str, diff --git a/src/configs/character.rs b/src/configs/character.rs index 3e7153370..19204fe3c 100644 --- a/src/configs/character.rs +++ b/src/configs/character.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CharacterConfig<'a> { pub format: &'a str, pub success_symbol: &'a str, diff --git a/src/configs/cmake.rs b/src/configs/cmake.rs index 8b9f51c85..d225b0d90 100644 --- a/src/configs/cmake.rs +++ b/src/configs/cmake.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CMakeConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/cmd_duration.rs b/src/configs/cmd_duration.rs index 096fbf8e1..be5d2f732 100644 --- a/src/configs/cmd_duration.rs +++ b/src/configs/cmd_duration.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CmdDurationConfig<'a> { pub min_time: i64, pub format: &'a str, diff --git a/src/configs/conda.rs b/src/configs/conda.rs index 54e9434ef..623b4fc67 100644 --- a/src/configs/conda.rs +++ b/src/configs/conda.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CondaConfig<'a> { pub truncation_length: usize, pub format: &'a str, diff --git a/src/configs/crystal.rs b/src/configs/crystal.rs index ae3c4da75..e5b61476e 100644 --- a/src/configs/crystal.rs +++ b/src/configs/crystal.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CrystalConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/custom.rs b/src/configs/custom.rs index df6d03de5..bced89319 100644 --- a/src/configs/custom.rs +++ b/src/configs/custom.rs @@ -1,29 +1,22 @@ use crate::config::{ModuleConfig, VecOr}; +use serde::{self, Serialize}; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, Default, PartialEq)] -pub struct Files<'a>(pub Vec<&'a str>); - -#[derive(Clone, Default, PartialEq)] -pub struct Extensions<'a>(pub Vec<&'a str>); - -#[derive(Clone, Default, PartialEq)] -pub struct Directories<'a>(pub Vec<&'a str>); - -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct CustomConfig<'a> { pub format: &'a str, pub symbol: &'a str, pub command: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] pub when: Option<&'a str>, pub shell: VecOr<&'a str>, pub description: &'a str, pub style: &'a str, pub disabled: bool, - pub files: Files<'a>, - pub extensions: Extensions<'a>, - pub directories: Directories<'a>, + pub files: Vec<&'a str>, + pub extensions: Vec<&'a str>, + pub directories: Vec<&'a str>, } impl<'a> Default for CustomConfig<'a> { @@ -37,57 +30,9 @@ impl<'a> Default for CustomConfig<'a> { description: "<custom config>", style: "green bold", disabled: false, - files: Files::default(), - extensions: Extensions::default(), - directories: Directories::default(), + files: Vec::default(), + extensions: Vec::default(), + directories: Vec::default(), } } } - -impl<'a> ModuleConfig<'a> for Files<'a> { - fn from_config(config: &'a toml::Value) -> Option<Self> { - let mut files = Vec::new(); - - for item in config.as_array()? { - if let Some(file) = item.as_str() { - files.push(file); - } else { - log::warn!("Unexpected file {:?}", item); - } - } - - Some(Files(files)) - } -} - -impl<'a> ModuleConfig<'a> for Extensions<'a> { - fn from_config(config: &'a toml::Value) -> Option<Self> { - let mut extensions = Vec::new(); - - for item in config.as_array()? { - if let Some(file) = item.as_str() { - extensions.push(file); - } else { - log::warn!("Unexpected extension {:?}", item); - } - } - - Some(Extensions(extensions)) - } -} - -impl<'a> ModuleConfig<'a> for Directories<'a> { - fn from_config(config: &'a toml::Value) -> Option<Self> { - let mut directories = Vec::new(); - - for item in config.as_array()? { - if let Some(file) = item.as_str() { - directories.push(file); - } else { - log::warn!("Unexpected directory {:?}", item); - } - } - - Some(Directories(directories)) - } -} diff --git a/src/configs/dart.rs b/src/configs/dart.rs index ef8e16bba..94f5112e4 100644 --- a/src/configs/dart.rs +++ b/src/configs/dart.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct DartConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/directory.rs b/src/configs/directory.rs index 47fa8bdcf..9f1b27ad4 100644 --- a/src/configs/directory.rs +++ b/src/configs/directory.rs @@ -1,9 +1,10 @@ use crate::config::ModuleConfig; use indexmap::IndexMap; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct DirectoryConfig<'a> { pub truncation_length: i64, pub truncate_to_repo: bool, diff --git a/src/configs/docker_context.rs b/src/configs/docker_context.rs index 7d8757d4d..b026acda9 100644 --- a/src/configs/docker_context.rs +++ b/src/configs/docker_context.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct DockerContextConfig<'a> { pub symbol: &'a str, pub style: &'a str, diff --git a/src/configs/dotnet.rs b/src/configs/dotnet.rs index 33118e7c9..05438f363 100644 --- a/src/configs/dotnet.rs +++ b/src/configs/dotnet.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct DotnetConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/elixir.rs b/src/configs/elixir.rs index 3fddbb979..9eb5b347c 100644 --- a/src/configs/elixir.rs +++ b/src/configs/elixir.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct ElixirConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/elm.rs b/src/configs/elm.rs index 4b582867b..d97cddd9c 100644 --- a/src/configs/elm.rs +++ b/src/configs/elm.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct ElmConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/env_var.rs b/src/configs/env_var.rs index 07aef6090..30ab72e95 100644 --- a/src/configs/env_var.rs +++ b/src/configs/env_var.rs @@ -1,12 +1,15 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct EnvVarConfig<'a> { pub symbol: &'a str, pub style: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] pub variable: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] pub default: Option<&'a str>, pub format: &'a str, pub disabled: bool, diff --git a/src/configs/erlang.rs b/src/configs/erlang.rs index 9e04105df..b0a7515ef 100644 --- a/src/configs/erlang.rs +++ b/src/configs/erlang.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct ErlangConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/gcloud.rs b/src/configs/gcloud.rs index d3f3c2028..fd291badf 100644 --- a/src/configs/gcloud.rs +++ b/src/configs/gcloud.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; use std::collections::HashMap; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct GcloudConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/git_branch.rs b/src/configs/git_branch.rs index 87ab33f1a..d9c40c9ad 100644 --- a/src/configs/git_branch.rs +++ b/src/configs/git_branch.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serialize; use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig)] +#[derive(Clone, ModuleConfig, Serialize)] pub struct GitBranchConfig<'a> { pub format: &'a str, pub symbol: &'a str, diff --git a/src/configs/git_commit.rs b/src/configs/git_commit.rs index d07a9d765..01c4c3ee3 100644 --- a/src/configs/git_commit.rs +++ b/src/configs/git_commit.rs @@ -1,8 +1,9 @@ use crate::config::ModuleConfig; +use serde::Serial |