1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-22 18:05:14 +00:00
starship/src/configs/starship_root.rs
Harald Hoyer 4f46411403
feat: add a container indicator (#3304)
* test: add mock method for absolute files

Signed-off-by: Harald Hoyer <harald@hoyer.xyz>

* feat(module): add a container indicator module

Adds a container type indicator, if inside a container,
detected via the presence of some marker files.

E.g. inside a podman container entered with `toolbox enter`
the prompt changes to the container name and version.

```
starship on  container_rebased [$!] is 📦 v1.0.0 via 🦀 v1.56.1
❯ toolbox enter

starship on  container_rebased [$!] is 📦 v1.0.0 via 🦀 v1.56.1
⬢ [fedora-toolbox:35] ❯
```

Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
2022-01-21 09:44:46 -06:00

164 lines
4.5 KiB
Rust

use crate::{config::ModuleConfig, module::ALL_MODULES};
use serde::Serialize;
use std::cmp::Ordering;
// On changes please also update the `FullConfig` struct in `mod.rs`
#[derive(Clone, Serialize)]
pub struct StarshipRootConfig {
pub format: String,
pub right_format: String,
pub continuation_prompt: String,
pub scan_timeout: u64,
pub command_timeout: u64,
pub add_newline: bool,
}
// 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.
pub const PROMPT_ORDER: &[&str] = &[
"username",
"hostname",
"shlvl",
"singularity",
"kubernetes",
"directory",
"vcsh",
"git_branch",
"git_commit",
"git_state",
"git_metrics",
"git_status",
"hg_branch",
"docker_context",
"package",
// ↓ Toolchain version modules ↓
// (Let's keep these sorted alphabetically)
"cmake",
"cobol",
"dart",
"deno",
"dotnet",
"elixir",
"elm",
"erlang",
"golang",
"helm",
"java",
"julia",
"kotlin",
"lua",
"nim",
"nodejs",
"ocaml",
"perl",
"php",
"pulumi",
"purescript",
"python",
"rlang",
"red",
"ruby",
"rust",
"scala",
"swift",
"terraform",
"vlang",
"vagrant",
"zig",
// ↑ Toolchain version modules ↑
"nix_shell",
"conda",
"memory_usage",
"aws",
"gcloud",
"openstack",
"azure",
"env_var",
"crystal",
"custom",
"sudo",
"cmd_duration",
"line_break",
"jobs",
#[cfg(feature = "battery")]
"battery",
"time",
"status",
"container",
"shell",
"character",
];
// On changes please also update `Default` for the `FullConfig` struct in `mod.rs`
impl<'a> Default for StarshipRootConfig {
fn default() -> Self {
StarshipRootConfig {
format: "$all".to_string(),
right_format: "".to_string(),
continuation_prompt: "[∙](bright-black) ".to_string(),
scan_timeout: 30,
command_timeout: 500,
add_newline: true,
}
}
}
impl<'a> ModuleConfig<'a> for StarshipRootConfig {
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),
"right_format" => self.right_format.load_config(v),
"continuation_prompt" => self.continuation_prompt.load_config(v),
"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 => {
if !ALL_MODULES.contains(&unknown)
&& unknown != "custom"
&& unknown != "env_var"
{
log::warn!("Unknown config key '{}'", unknown);
let did_you_mean = &[
// Root options
"format",
"right_format",
"continuation_prompt",
"scan_timeout",
"command_timeout",
"add_newline",
// Modules
"custom",
"env_var",
]
.iter()
.chain(ALL_MODULES)
.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);
}
}
}
});
}
}
fn from_config(config: &'a toml::Value) -> Option<Self> {
let mut out = Self::default();
out.load_config(config);
Some(out)
}
}