diff --git a/docs/config/README.md b/docs/config/README.md index 64626ea3e..a5fb3421a 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -118,6 +118,7 @@ prompt_order = [ "nodejs", "ocaml", "php", + "purescript", "python", "ruby", "rust", @@ -1441,3 +1442,28 @@ files = ["foo"] # can specify filters when = """ test "$HOME" == "$PWD" """ prefix = " transcending " ``` + +## PureScript + +The `purescript` module shows the currently installed version of PureScript version. +The module will be shown if any of the following conditions are met: + +- The current directory contains a `spago.dhall` file +- The current directory contains a \*.purs files + +### Options + +| Variable | Default | Description | +| ---------- | -------------- | ------------------------------------------------------------ | +| `symbol` | `"<=> "` | The symbol used before displaying the version of PureScript. | +| `style` | `"bold white"` | The style for the module. | +| `disabled` | `false` | Disables the `purescript` module. | + +### Example + +```toml +# ~/.config/starship.toml + +[purescript] +symbol = "<=> " +``` diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 57521e522..1f695a66d 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -30,6 +30,7 @@ pub mod nodejs; pub mod ocaml; pub mod package; pub mod php; +pub mod purescript; pub mod python; pub mod ruby; pub mod rust; diff --git a/src/configs/purescript.rs b/src/configs/purescript.rs new file mode 100644 index 000000000..7ae0c73cf --- /dev/null +++ b/src/configs/purescript.rs @@ -0,0 +1,23 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct PureScriptConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub version: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for PureScriptConfig<'a> { + fn new() -> Self { + PureScriptConfig { + symbol: SegmentConfig::new("<=> "), + version: SegmentConfig::default(), + style: Color::White.bold(), + disabled: false, + } + } +} diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index 6d93cc46a..18eaeee73 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -42,6 +42,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> { "nodejs", "ocaml", "php", + "purescript", "python", "ruby", "rust", diff --git a/src/module.rs b/src/module.rs index 89d7e508b..a3784212c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -41,6 +41,7 @@ pub const ALL_MODULES: &[&str] = &[ "nodejs", "ocaml", "package", + "purescript", "python", "ruby", "crystal", diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 39993de13..26b69e4f6 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -31,6 +31,7 @@ mod nodejs; mod ocaml; mod package; mod php; +mod purescript; mod python; mod ruby; mod rust; @@ -84,6 +85,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "ocaml" => ocaml::module(context), "package" => package::module(context), "php" => php::module(context), + "purescript" => purescript::module(context), "python" => python::module(context), "ruby" => ruby::module(context), "rust" => rust::module(context), @@ -134,6 +136,7 @@ pub fn description(module: &str) -> &'static str { "ocaml" => "The currently installed version of OCaml", "package" => "The package version of the current directory's project", "php" => "The currently installed version of PHP", + "purescript" => "The currently installed version of PureScript", "python" => "The currently installed version of Python", "ruby" => "The currently installed version of Ruby", "rust" => "The currently installed version of Rust", diff --git a/src/modules/purescript.rs b/src/modules/purescript.rs new file mode 100644 index 000000000..2ca3d20f9 --- /dev/null +++ b/src/modules/purescript.rs @@ -0,0 +1,72 @@ +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::purescript::PureScriptConfig; +use crate::utils; + +/// Creates a module with the current PureScript version +/// +/// Will display the PureScript version if any of the following criteria are met: +/// - Current directory contains a `spago.dhall` file +/// - Current directory contains a `*.purs` files +pub fn module<'a>(context: &'a Context) -> Option> { + let is_purs_project = context + .try_begin_scan()? + .set_files(&["spago.dhall"]) + .set_extensions(&["purs"]) + .is_match(); + + if !is_purs_project { + return None; + } + + let purs_version = utils::exec_cmd("purs", &["--version"])?.stdout; + let formatted_version = Some(format!("v{}", purs_version.trim()))?; + + let mut module = context.new_module("purescript"); + let config: PureScriptConfig = PureScriptConfig::try_load(module.config); + module.set_style(config.style); + + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(&formatted_version)); + + Some(module) +} + +#[cfg(test)] +mod tests { + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::File; + use std::io; + + #[test] + fn folder_without_purescript_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let actual = render_module("purescript", dir.path(), None); + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_purescript_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Main.purs"))?.sync_all()?; + + let actual = render_module("purescript", dir.path(), None); + let expected = Some(format!("via {} ", Color::White.bold().paint("<=> v0.13.5"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_spago_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("spago.dhall"))?.sync_all()?; + + let actual = render_module("purescript", dir.path(), None); + let expected = Some(format!("via {} ", Color::White.bold().paint("<=> v0.13.5"))); + assert_eq!(expected, actual); + dir.close() + } +} diff --git a/src/utils.rs b/src/utils.rs index 606624f28..687544d68 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -73,6 +73,10 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option { stderr: String::default(), }) } + "purs --version" => Some(CommandOutput { + stdout: String::from("0.13.5"), + stderr: String::default(), + }), "ruby -v" => Some(CommandOutput { stdout: String::from("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"), stderr: String::default(),