From ef55042f2e0d4766fe68449987657c84058c4169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Benoit?= Date: Tue, 4 Aug 2020 18:22:44 +0200 Subject: [PATCH] feat(perl): Added perl version module (#1382) Adds a module for perl support. --- docs/config/README.md | 40 ++++++ src/configs/mod.rs | 1 + src/configs/perl.rs | 22 ++++ src/configs/starship_root.rs | 1 + src/module.rs | 1 + src/modules/mod.rs | 3 + src/modules/perl.rs | 232 +++++++++++++++++++++++++++++++++++ src/utils.rs | 4 + 8 files changed, 304 insertions(+) create mode 100644 src/configs/perl.rs create mode 100644 src/modules/perl.rs diff --git a/docs/config/README.md b/docs/config/README.md index 21f59a6e..253e6214 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -200,6 +200,7 @@ $julia\ $nim\ $nodejs\ $ocaml\ +$perl\ $php\ $purescript\ $python\ @@ -1765,6 +1766,45 @@ The module will be shown if any of the following conditions are met: format = "via [🐪 $version]($style) " ``` + +## Perl + +The `perl` module shows the currently installed version of Perl. +The module will be shown if any of the following conditions are met: + +- The current directory contains a `Makefile.PL` or `Build.PL` file +- The current directory contains a `cpanfile` or `cpanfile.snapshot` file +- The current directory contains a `META.json` file or `META.yml` file +- The current directory contains a `.perl-version` file +- The current directory contains a `.pl`, `.pm` or `.pod` + +### Options + +| Variable | Default | Description | +| ---------- |----------------------------------- | ----------------------------------------------------- | +| `format` | `"via [$symbol$version]($style) "` | The format string for the module.| +| `symbol` | `"🐪 "` | The symbol used before displaying the version of Perl | +| `style` | `"bold 149"` | The style for the module. | +| `disabled` | `false` | Disables the `perl` module. | + +### Variables + +| Variable | Example | Description | +| -------- | --------- | ------------------------------------ | +| version | `v5.26.1` | The version of `perl` | +| symbol | | Mirrors the value of option `symbol` | +| style\* | | Mirrors the value of option `style` | + +### Example + +```toml +# ~/.config/starship.toml + +[perl] +format = "via [🦪 $version]($style) " +``` + + ## PHP The `php` module shows the currently installed version of PHP. diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 6946f79b..0f6d5f85 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -33,6 +33,7 @@ pub mod nix_shell; pub mod nodejs; pub mod ocaml; pub mod package; +pub mod perl; pub mod php; pub mod purescript; pub mod python; diff --git a/src/configs/perl.rs b/src/configs/perl.rs new file mode 100644 index 00000000..9f526fbf --- /dev/null +++ b/src/configs/perl.rs @@ -0,0 +1,22 @@ +use crate::config::{ModuleConfig, RootModuleConfig}; + +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct PerlConfig<'a> { + pub symbol: &'a str, + pub style: &'a str, + pub format: &'a str, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for PerlConfig<'a> { + fn new() -> Self { + PerlConfig { + symbol: "🐪 ", + style: "149 bold", + format: "via [$symbol$version]($style) ", + disabled: false, + } + } +} diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index 20c7034e..b5e913d8 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -39,6 +39,7 @@ pub const PROMPT_ORDER: &[&str] = &[ "nim", "nodejs", "ocaml", + "perl", "php", "purescript", "python", diff --git a/src/module.rs b/src/module.rs index 7e592214..8292cb99 100644 --- a/src/module.rs +++ b/src/module.rs @@ -43,6 +43,7 @@ pub const ALL_MODULES: &[&str] = &[ "nodejs", "ocaml", "package", + "perl", "purescript", "python", "ruby", diff --git a/src/modules/mod.rs b/src/modules/mod.rs index b2bda793..55d54854 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -34,6 +34,7 @@ mod nix_shell; mod nodejs; mod ocaml; mod package; +mod perl; mod php; mod purescript; mod python; @@ -93,6 +94,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "nodejs" => nodejs::module(context), "ocaml" => ocaml::module(context), "package" => package::module(context), + "perl" => perl::module(context), "php" => php::module(context), "purescript" => purescript::module(context), "python" => python::module(context), @@ -149,6 +151,7 @@ pub fn description(module: &str) -> &'static str { "nodejs" => "The currently installed version of NodeJS", "ocaml" => "The currently installed version of OCaml", "package" => "The package version of the current directory's project", + "perl" => "The currently installed version of Perl", "php" => "The currently installed version of PHP", "purescript" => "The currently installed version of PureScript", "python" => "The currently installed version of Python", diff --git a/src/modules/perl.rs b/src/modules/perl.rs new file mode 100644 index 00000000..5130930d --- /dev/null +++ b/src/modules/perl.rs @@ -0,0 +1,232 @@ +use super::{Context, Module, RootModuleConfig}; + +use crate::configs::perl::PerlConfig; +use crate::formatter::StringFormatter; +use crate::utils; + +/// Creates a module with the current perl version +/// +/// Will display the perl version if any of the following criteria are met: +/// - Current directory contains a `.pl`, `.pm` or a `.pod` file +/// - Current directory contains a "Makefile.PL", "Build.PL", "cpanfile", "cpanfile.snapshot", +/// "META.json", "META.yml", or ".perl-version" file +pub fn module<'a>(context: &'a Context) -> Option> { + let is_perl_project = context + .try_begin_scan()? + .set_files(&[ + "Makefile.PL", + "Build.PL", + "cpanfile", + "cpanfile.snapshot", + "META.json", + "META.yml", + ".perl-version", + ]) + .set_extensions(&["pl", "pm", "pod"]) + .is_match(); + + if !is_perl_project { + return None; + } + + let perl_version = utils::exec_cmd("perl", &["-e", "printf q#%vd#,$^V;"])?.stdout; + + let mut module = context.new_module("perl"); + let config: PerlConfig = PerlConfig::try_load(module.config); + + let parsed = StringFormatter::new(config.format).and_then(|formatter| { + formatter + .map_meta(|var, _| match var { + "symbol" => Some(config.symbol), + _ => None, + }) + .map_style(|variable| match variable { + "style" => Some(Ok(config.style)), + _ => None, + }) + .map(|variable| match variable { + "version" => Some(Ok(format!("v{}", &perl_version))), + _ => None, + }) + .parse(None) + }); + + module.set_segments(match parsed { + Ok(segments) => segments, + Err(error) => { + log::warn!("Error in module `perl`:\n{}", error); + return None; + } + }); + + 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_perl_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_makefile_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Makefile.PL"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_buildfile_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Build.PL"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_cpanfile_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("cpanfile"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_cpanfile_snapshot_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("cpanfile.snapshot"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_meta_json_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("META.json"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_meta_yml_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("META.yml"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_perl_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".perl-version"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_perl_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.pl"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_perl_module_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.pm"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_perldoc_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.pod"))?.sync_all()?; + + let actual = render_module("perl", dir.path(), None); + + let expected = Some(format!( + "via {} ", + Color::Fixed(149).bold().paint("🐪 v5.26.1") + )); + assert_eq!(expected, actual); + dir.close() + } +} diff --git a/src/utils.rs b/src/utils.rs index 75eb478e..ecdd4dc7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -111,6 +111,10 @@ active boot switches: -d:release\n", stdout: String::from("4.08.1\n"), stderr: String::default(), }), + "perl -e printf q#%vd#,$^V;" => Some(CommandOutput { + stdout: String::from("5.26.1"), + stderr: String::default(), + }), "php -nr 'echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;'" => { Some(CommandOutput { stdout: String::from("7.3.8"),