2021-03-31 18:13:23 +00:00
|
|
|
use crate::{config::ModuleConfig, module::ALL_MODULES};
|
2019-10-25 01:08:09 +00:00
|
|
|
|
2021-03-31 15:31:55 +00:00
|
|
|
use serde::Serialize;
|
2021-04-06 20:12:37 +00:00
|
|
|
use std::cmp::Ordering;
|
2019-10-25 01:08:09 +00:00
|
|
|
|
2021-04-05 14:47:49 +00:00
|
|
|
// On changes please also update the `FullConfig` struct in `mod.rs`
|
2021-03-31 18:13:23 +00:00
|
|
|
#[derive(Clone, Serialize)]
|
2021-10-27 13:13:17 +00:00
|
|
|
pub struct StarshipRootConfig {
|
|
|
|
pub format: String,
|
|
|
|
pub right_format: String,
|
2019-10-28 13:41:16 +00:00
|
|
|
pub scan_timeout: u64,
|
2021-02-11 20:34:47 +00:00
|
|
|
pub command_timeout: u64,
|
2020-08-18 16:58:29 +00:00
|
|
|
pub add_newline: bool,
|
2019-10-25 01:08:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
// List of default prompt order
|
|
|
|
// NOTE: If this const value is changed then Default prompt order subheading inside
|
|
|
|
// prompt heading of config docs needs to be updated according to changes made here.
|
2020-07-24 21:42:36 +00:00
|
|
|
pub const PROMPT_ORDER: &[&str] = &[
|
2020-07-07 22:45:32 +00:00
|
|
|
"username",
|
|
|
|
"hostname",
|
2020-08-05 16:30:01 +00:00
|
|
|
"shlvl",
|
2020-07-07 22:45:32 +00:00
|
|
|
"singularity",
|
|
|
|
"kubernetes",
|
|
|
|
"directory",
|
2021-03-28 15:48:15 +00:00
|
|
|
"vcsh",
|
2020-07-07 22:45:32 +00:00
|
|
|
"git_branch",
|
|
|
|
"git_commit",
|
|
|
|
"git_state",
|
2021-07-10 20:54:34 +00:00
|
|
|
"git_metrics",
|
2020-07-07 22:45:32 +00:00
|
|
|
"git_status",
|
|
|
|
"hg_branch",
|
|
|
|
"docker_context",
|
|
|
|
"package",
|
|
|
|
// ↓ Toolchain version modules ↓
|
|
|
|
// (Let's keep these sorted alphabetically)
|
2020-07-09 19:40:33 +00:00
|
|
|
"cmake",
|
2021-09-07 14:59:14 +00:00
|
|
|
"cobol",
|
2020-07-29 15:38:23 +00:00
|
|
|
"dart",
|
2021-04-15 13:22:12 +00:00
|
|
|
"deno",
|
2020-07-07 22:45:32 +00:00
|
|
|
"dotnet",
|
|
|
|
"elixir",
|
|
|
|
"elm",
|
|
|
|
"erlang",
|
|
|
|
"golang",
|
2020-07-17 07:51:25 +00:00
|
|
|
"helm",
|
2020-07-07 22:45:32 +00:00
|
|
|
"java",
|
|
|
|
"julia",
|
2020-12-26 14:26:50 +00:00
|
|
|
"kotlin",
|
2020-10-27 18:05:20 +00:00
|
|
|
"lua",
|
2020-07-07 22:45:32 +00:00
|
|
|
"nim",
|
|
|
|
"nodejs",
|
|
|
|
"ocaml",
|
2020-08-04 16:22:44 +00:00
|
|
|
"perl",
|
2020-07-07 22:45:32 +00:00
|
|
|
"php",
|
2021-10-05 23:27:25 +00:00
|
|
|
"pulumi",
|
2020-07-07 22:45:32 +00:00
|
|
|
"purescript",
|
|
|
|
"python",
|
2021-05-25 18:13:30 +00:00
|
|
|
"rlang",
|
2021-04-20 16:31:47 +00:00
|
|
|
"red",
|
2020-07-07 22:45:32 +00:00
|
|
|
"ruby",
|
|
|
|
"rust",
|
2021-03-14 19:37:00 +00:00
|
|
|
"scala",
|
2020-07-29 15:36:49 +00:00
|
|
|
"swift",
|
2020-07-07 22:45:32 +00:00
|
|
|
"terraform",
|
2021-05-03 19:50:29 +00:00
|
|
|
"vlang",
|
2021-01-30 11:05:16 +00:00
|
|
|
"vagrant",
|
2020-07-07 22:45:32 +00:00
|
|
|
"zig",
|
|
|
|
// ↑ Toolchain version modules ↑
|
|
|
|
"nix_shell",
|
|
|
|
"conda",
|
|
|
|
"memory_usage",
|
|
|
|
"aws",
|
2020-08-03 21:30:20 +00:00
|
|
|
"gcloud",
|
2020-10-24 09:46:43 +00:00
|
|
|
"openstack",
|
2020-07-07 22:45:32 +00:00
|
|
|
"env_var",
|
|
|
|
"crystal",
|
|
|
|
"custom",
|
2020-12-05 18:57:22 +00:00
|
|
|
"cmd_duration",
|
2020-07-07 22:45:32 +00:00
|
|
|
"line_break",
|
|
|
|
"jobs",
|
|
|
|
#[cfg(feature = "battery")]
|
|
|
|
"battery",
|
|
|
|
"time",
|
2020-09-25 22:04:51 +00:00
|
|
|
"status",
|
2021-02-20 14:40:49 +00:00
|
|
|
"shell",
|
2020-07-07 22:45:32 +00:00
|
|
|
"character",
|
|
|
|
];
|
|
|
|
|
2021-04-26 17:55:22 +00:00
|
|
|
// On changes please also update `Default` for the `FullConfig` struct in `mod.rs`
|
2021-10-27 13:13:17 +00:00
|
|
|
impl<'a> Default for StarshipRootConfig {
|
2021-03-15 10:40:52 +00:00
|
|
|
fn default() -> Self {
|
2019-10-25 01:08:09 +00:00
|
|
|
StarshipRootConfig {
|
2021-10-27 13:13:17 +00:00
|
|
|
format: "$all".to_string(),
|
|
|
|
right_format: "".to_string(),
|
2019-10-28 13:41:16 +00:00
|
|
|
scan_timeout: 30,
|
2021-02-11 20:34:47 +00:00
|
|
|
command_timeout: 500,
|
2020-08-18 16:58:29 +00:00
|
|
|
add_newline: true,
|
2019-10-25 01:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 18:13:23 +00:00
|
|
|
|
2021-10-27 13:13:17 +00:00
|
|
|
impl<'a> ModuleConfig<'a> for StarshipRootConfig {
|
2021-03-31 18:13:23 +00:00
|
|
|
fn load_config(&mut self, config: &'a toml::Value) {
|
|
|
|
if let toml::Value::Table(config) = config {
|
|
|
|
config.iter().for_each(|(k, v)| match k.as_str() {
|
|
|
|
"format" => self.format.load_config(v),
|
2021-09-08 19:45:27 +00:00
|
|
|
"right_format" => self.right_format.load_config(v),
|
2021-03-31 18:13:23 +00:00
|
|
|
"scan_timeout" => self.scan_timeout.load_config(v),
|
|
|
|
"command_timeout" => self.command_timeout.load_config(v),
|
|
|
|
"add_newline" => self.add_newline.load_config(v),
|
|
|
|
unknown => {
|
2021-07-13 21:06:08 +00:00
|
|
|
if !ALL_MODULES.contains(&unknown)
|
|
|
|
&& unknown != "custom"
|
|
|
|
&& unknown != "env_var"
|
|
|
|
{
|
2021-03-31 18:13:23 +00:00
|
|
|
log::warn!("Unknown config key '{}'", unknown);
|
2021-04-06 20:12:37 +00:00
|
|
|
|
|
|
|
let did_you_mean = &[
|
|
|
|
// Root options
|
|
|
|
"format",
|
2021-09-08 19:45:27 +00:00
|
|
|
"right_format",
|
2021-04-06 20:12:37 +00:00
|
|
|
"scan_timeout",
|
|
|
|
"command_timeout",
|
|
|
|
"add_newline",
|
|
|
|
// Modules
|
|
|
|
"custom",
|
2021-07-13 21:06:08 +00:00
|
|
|
"env_var",
|
2021-04-06 20:12:37 +00:00
|
|
|
]
|
|
|
|
.iter()
|
2021-09-02 15:48:29 +00:00
|
|
|
.chain(ALL_MODULES)
|
2021-04-06 20:12:37 +00:00
|
|
|
.filter_map(|field| {
|
|
|
|
let score = strsim::jaro_winkler(unknown, field);
|
|
|
|
(score > 0.8).then(|| (score, field))
|
|
|
|
})
|
|
|
|
.max_by(
|
|
|
|
|(score_a, _field_a), (score_b, _field_b)| {
|
|
|
|
score_a.partial_cmp(score_b).unwrap_or(Ordering::Equal)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some((_score, field)) = did_you_mean {
|
|
|
|
log::warn!("Did you mean '{}'?", field);
|
|
|
|
}
|
2021-03-31 18:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_config(config: &'a toml::Value) -> Option<Self> {
|
|
|
|
let mut out = Self::default();
|
|
|
|
out.load_config(config);
|
|
|
|
Some(out)
|
|
|
|
}
|
|
|
|
}
|