1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-26 11:37:32 +00:00

feat(ocaml): add switch_indicator and switch_name variable (#2503)

* feat(ocaml): add switch variable

* feat(ocaml): configurable opam switch display

* docs(ocaml): document opam switch display options

Add `global_switch_indicator` and `local_switch_indicator` config
options.

* style(opam): add `OpamSwitch` type alias

* fix(opam): gracefully handle empty opam switch

Co-authored-by: cdaringe <cdaringe@gmail.com>
This commit is contained in:
Dario Vladović 2021-04-02 19:21:48 +02:00 committed by GitHub
parent 7779ed6075
commit dbb6885724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 202 additions and 27 deletions

View File

@ -1866,9 +1866,11 @@ By default the module will be shown if any of the following conditions are met:
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------- | | ------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format string for the module. | | `format` | `"via [$symbol($version )(\($switch_indicator$switch_name\) )]($style)"` | The format string for the module. |
| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. | | `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. |
| `global_switch_indicator` | `""` | The format string used to represent global OPAM switch. |
| `local_switch_indicator` | `"*"` | The format string used to represent local OPAM switch. |
| `detect_extensions` | `["opam", "ml", "mli", "re", "rei"]` | Which extensions should trigger this module. | | `detect_extensions` | `["opam", "ml", "mli", "re", "rei"]` | Which extensions should trigger this module. |
| `detect_files` | `["dune", "dune-project", "jbuild", "jbuild-ignore", ".merlin"]` | Which filenames should trigger this module. | | `detect_files` | `["dune", "dune-project", "jbuild", "jbuild-ignore", ".merlin"]` | Which filenames should trigger this module. |
| `detect_folders` | `["_opam", "esy.lock"]` | Which folders should trigger this module. | | `detect_folders` | `["_opam", "esy.lock"]` | Which folders should trigger this module. |
@ -1878,8 +1880,10 @@ By default the module will be shown if any of the following conditions are met:
### Variables ### Variables
| Variable | Example | Description | | Variable | Example | Description |
| -------- | --------- | ------------------------------------ | | ---------------- | ------------ | ----------------------------------------------------------------- |
| version | `v4.10.0` | The version of `ocaml` | | version | `v4.10.0` | The version of `ocaml` |
| switch_name | `my-project` | The active OPAM switch |
| switch_indicator | | Mirrors the value of `indicator` for currently active OPAM switch |
| symbol | | Mirrors the value of option `symbol` | | symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` | | style\* | | Mirrors the value of option `style` |

View File

@ -5,6 +5,8 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)] #[derive(Clone, ModuleConfig, Serialize)]
pub struct OCamlConfig<'a> { pub struct OCamlConfig<'a> {
pub global_switch_indicator: &'a str,
pub local_switch_indicator: &'a str,
pub format: &'a str, pub format: &'a str,
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
@ -17,7 +19,9 @@ pub struct OCamlConfig<'a> {
impl<'a> Default for OCamlConfig<'a> { impl<'a> Default for OCamlConfig<'a> {
fn default() -> Self { fn default() -> Self {
OCamlConfig { OCamlConfig {
format: "via [$symbol($version )]($style)", global_switch_indicator: "",
local_switch_indicator: "*",
format: "via [$symbol($version )(\\($switch_indicator$switch_name\\) )]($style)",
symbol: "🐫 ", symbol: "🐫 ",
style: "bold yellow", style: "bold yellow",
disabled: false, disabled: false,

View File

@ -1,8 +1,18 @@
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use once_cell::sync::Lazy;
use std::ops::Deref;
use std::path::Path;
use crate::configs::ocaml::OCamlConfig; use crate::configs::ocaml::OCamlConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
#[derive(Debug, PartialEq)]
enum SwitchType {
Global,
Local,
}
type OpamSwitch = (SwitchType, String);
/// Creates a module with the current OCaml version /// Creates a module with the current OCaml version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("ocaml"); let mut module = context.new_module("ocaml");
@ -18,10 +28,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
let opam_switch: Lazy<Option<OpamSwitch>, _> = Lazy::new(|| get_opam_switch(context));
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|variable, _| match variable { .map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol), "symbol" => Some(config.symbol),
"switch_indicator" => {
let (switch_type, _) = &opam_switch.deref().as_ref()?;
match switch_type {
SwitchType::Global => Some(config.global_switch_indicator),
SwitchType::Local => Some(config.local_switch_indicator),
}
}
_ => None, _ => None,
}) })
.map_style(|variable| match variable { .map_style(|variable| match variable {
@ -29,6 +48,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"switch_name" => {
let (_, name) = opam_switch.deref().as_ref()?;
Some(Ok(name.to_string()))
}
"version" => { "version" => {
let is_esy_project = context let is_esy_project = context
.try_begin_scan()? .try_begin_scan()?
@ -58,13 +81,49 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn get_opam_switch(context: &Context) -> Option<OpamSwitch> {
let opam_switch = context
.exec_cmd("opam", &["switch", "show", "--safe"])?
.stdout;
parse_opam_switch(&opam_switch.trim())
}
fn parse_opam_switch(opam_switch: &str) -> Option<OpamSwitch> {
if opam_switch.is_empty() {
return None;
}
let path = Path::new(opam_switch);
if !path.has_root() {
Some((SwitchType::Global, opam_switch.to_string()))
} else {
Some((SwitchType::Local, path.file_name()?.to_str()?.to_string()))
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::test::ModuleRenderer; use super::{parse_opam_switch, SwitchType};
use crate::{test::ModuleRenderer, utils::CommandOutput};
use ansi_term::Color; use ansi_term::Color;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io; use std::io;
#[test]
fn test_parse_opam_switch() {
let global_switch = "ocaml-base-compiler.4.10.0";
let local_switch = "/path/to/my-project";
assert_eq!(
parse_opam_switch(global_switch),
Some((SwitchType::Global, "ocaml-base-compiler.4.10.0".to_string()))
);
assert_eq!(
parse_opam_switch(local_switch),
Some((SwitchType::Local, "my-project".to_string()))
);
}
#[test] #[test]
fn folder_without_ocaml_file() -> io::Result<()> { fn folder_without_ocaml_file() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
@ -80,7 +139,10 @@ mod tests {
File::create(dir.path().join("any.opam"))?.sync_all()?; File::create(dir.path().join("any.opam"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -91,7 +153,10 @@ mod tests {
fs::create_dir_all(dir.path().join("_opam"))?; fs::create_dir_all(dir.path().join("_opam"))?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -106,7 +171,10 @@ mod tests {
"{\"dependencies\": {\"ocaml\": \"4.8.1000\"}}", "{\"dependencies\": {\"ocaml\": \"4.8.1000\"}}",
)?; )?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.08.1 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.08.1 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -117,7 +185,10 @@ mod tests {
File::create(dir.path().join("dune"))?.sync_all()?; File::create(dir.path().join("dune"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -128,7 +199,10 @@ mod tests {
File::create(dir.path().join("dune-project"))?.sync_all()?; File::create(dir.path().join("dune-project"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -139,7 +213,10 @@ mod tests {
File::create(dir.path().join("jbuild"))?.sync_all()?; File::create(dir.path().join("jbuild"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -150,7 +227,10 @@ mod tests {
File::create(dir.path().join("jbuild-ignore"))?.sync_all()?; File::create(dir.path().join("jbuild-ignore"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -161,7 +241,10 @@ mod tests {
File::create(dir.path().join(".merlin"))?.sync_all()?; File::create(dir.path().join(".merlin"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -172,7 +255,10 @@ mod tests {
File::create(dir.path().join("any.ml"))?.sync_all()?; File::create(dir.path().join("any.ml"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -183,7 +269,10 @@ mod tests {
File::create(dir.path().join("any.mli"))?.sync_all()?; File::create(dir.path().join("any.mli"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -194,7 +283,10 @@ mod tests {
File::create(dir.path().join("any.re"))?.sync_all()?; File::create(dir.path().join("any.re"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
@ -205,8 +297,79 @@ mod tests {
File::create(dir.path().join("any.rei"))?.sync_all()?; File::create(dir.path().join("any.rei"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn without_opam_switch() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("any.ml"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml")
.cmd(
"opam switch show --safe",
Some(CommandOutput {
stdout: String::default(),
stderr: String::default(),
}),
)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 ")));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
} }
#[test]
fn with_global_opam_switch() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("any.ml"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml")
.cmd(
"opam switch show --safe",
Some(CommandOutput {
stdout: String::from("ocaml-base-compiler.4.10.0\n"),
stderr: String::default(),
}),
)
.path(dir.path())
.collect();
let expected = Some(format!(
"via {}",
Color::Yellow
.bold()
.paint("🐫 v4.10.0 (ocaml-base-compiler.4.10.0) ")
));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn with_local_opam_switch() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("any.ml"))?.sync_all()?;
let actual = ModuleRenderer::new("ocaml")
.cmd(
"opam switch show --safe",
Some(CommandOutput {
stdout: String::from("/path/to/my-project\n"),
stderr: String::default(),
}),
)
.path(dir.path())
.collect();
let expected = Some(format!(
"via {}",
Color::Yellow.bold().paint("🐫 v4.10.0 (*my-project) ")
));
assert_eq!(expected, actual);
dir.close()
}
} }

View File

@ -129,6 +129,10 @@ active boot switches: -d:release\n",
stdout: String::from("4.10.0\n"), stdout: String::from("4.10.0\n"),
stderr: String::default(), stderr: String::default(),
}), }),
"opam switch show --safe" => Some(CommandOutput {
stdout: String::from("default\n"),
stderr: String::default(),
}),
"esy ocaml -vnum" => Some(CommandOutput { "esy ocaml -vnum" => Some(CommandOutput {
stdout: String::from("4.08.1\n"), stdout: String::from("4.08.1\n"),
stderr: String::default(), stderr: String::default(),