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

@ -1865,23 +1865,27 @@ By default the module will be shown if any of the following conditions are met:
### Options
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format string for the module. |
| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. |
| `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_folders` | `["_opam", "esy.lock"]` | Which folders should trigger this module. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `ocaml` module. |
| Option | Default | Description |
| ------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------- |
| `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. |
| `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_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. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `ocaml` module. |
### Variables
| Variable | Example | Description |
| -------- | --------- | ------------------------------------ |
| version | `v4.10.0` | The version of `ocaml` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
| Variable | Example | Description |
| ---------------- | ------------ | ----------------------------------------------------------------- |
| 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` |
| style\* | | Mirrors the value of option `style` |
\*: This variable can only be used as a part of a style string

View File

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

View File

@ -1,8 +1,18 @@
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::formatter::StringFormatter;
#[derive(Debug, PartialEq)]
enum SwitchType {
Global,
Local,
}
type OpamSwitch = (SwitchType, String);
/// Creates a module with the current OCaml version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("ocaml");
@ -18,10 +28,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None;
}
let opam_switch: Lazy<Option<OpamSwitch>, _> = Lazy::new(|| get_opam_switch(context));
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"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,
})
.map_style(|variable| match variable {
@ -29,6 +48,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"switch_name" => {
let (_, name) = opam_switch.deref().as_ref()?;
Some(Ok(name.to_string()))
}
"version" => {
let is_esy_project = context
.try_begin_scan()?
@ -58,13 +81,49 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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)]
mod tests {
use crate::test::ModuleRenderer;
use super::{parse_opam_switch, SwitchType};
use crate::{test::ModuleRenderer, utils::CommandOutput};
use ansi_term::Color;
use std::fs::{self, File};
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]
fn folder_without_ocaml_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
@ -80,7 +139,10 @@ mod tests {
File::create(dir.path().join("any.opam"))?.sync_all()?;
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);
dir.close()
}
@ -91,7 +153,10 @@ mod tests {
fs::create_dir_all(dir.path().join("_opam"))?;
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);
dir.close()
}
@ -106,7 +171,10 @@ mod tests {
"{\"dependencies\": {\"ocaml\": \"4.8.1000\"}}",
)?;
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);
dir.close()
}
@ -117,7 +185,10 @@ mod tests {
File::create(dir.path().join("dune"))?.sync_all()?;
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);
dir.close()
}
@ -128,7 +199,10 @@ mod tests {
File::create(dir.path().join("dune-project"))?.sync_all()?;
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);
dir.close()
}
@ -139,7 +213,10 @@ mod tests {
File::create(dir.path().join("jbuild"))?.sync_all()?;
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);
dir.close()
}
@ -150,7 +227,10 @@ mod tests {
File::create(dir.path().join("jbuild-ignore"))?.sync_all()?;
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);
dir.close()
}
@ -161,7 +241,10 @@ mod tests {
File::create(dir.path().join(".merlin"))?.sync_all()?;
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);
dir.close()
}
@ -172,7 +255,10 @@ mod tests {
File::create(dir.path().join("any.ml"))?.sync_all()?;
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);
dir.close()
}
@ -183,7 +269,10 @@ mod tests {
File::create(dir.path().join("any.mli"))?.sync_all()?;
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);
dir.close()
}
@ -194,7 +283,10 @@ mod tests {
File::create(dir.path().join("any.re"))?.sync_all()?;
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);
dir.close()
}
@ -205,8 +297,79 @@ mod tests {
File::create(dir.path().join("any.rei"))?.sync_all()?;
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 ")));
assert_eq!(expected, actual);
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"),
stderr: String::default(),
}),
"opam switch show --safe" => Some(CommandOutput {
stdout: String::from("default\n"),
stderr: String::default(),
}),
"esy ocaml -vnum" => Some(CommandOutput {
stdout: String::from("4.08.1\n"),
stderr: String::default(),