feat(perl): Added perl version module (#1382)

Adds a module for perl support.
This commit is contained in:
Stéphane Benoit 2020-08-04 18:22:44 +02:00 committed by GitHub
parent c5f2eedf07
commit ef55042f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 304 additions and 0 deletions

View File

@ -200,6 +200,7 @@ $julia\
$nim\ $nim\
$nodejs\ $nodejs\
$ocaml\ $ocaml\
$perl\
$php\ $php\
$purescript\ $purescript\
$python\ $python\
@ -1765,6 +1766,45 @@ The module will be shown if any of the following conditions are met:
format = "via [🐪 $version]($style) " 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 ## PHP
The `php` module shows the currently installed version of PHP. The `php` module shows the currently installed version of PHP.

View File

@ -33,6 +33,7 @@ pub mod nix_shell;
pub mod nodejs; pub mod nodejs;
pub mod ocaml; pub mod ocaml;
pub mod package; pub mod package;
pub mod perl;
pub mod php; pub mod php;
pub mod purescript; pub mod purescript;
pub mod python; pub mod python;

22
src/configs/perl.rs Normal file
View File

@ -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,
}
}
}

View File

@ -39,6 +39,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"nim", "nim",
"nodejs", "nodejs",
"ocaml", "ocaml",
"perl",
"php", "php",
"purescript", "purescript",
"python", "python",

View File

@ -43,6 +43,7 @@ pub const ALL_MODULES: &[&str] = &[
"nodejs", "nodejs",
"ocaml", "ocaml",
"package", "package",
"perl",
"purescript", "purescript",
"python", "python",
"ruby", "ruby",

View File

@ -34,6 +34,7 @@ mod nix_shell;
mod nodejs; mod nodejs;
mod ocaml; mod ocaml;
mod package; mod package;
mod perl;
mod php; mod php;
mod purescript; mod purescript;
mod python; mod python;
@ -93,6 +94,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"nodejs" => nodejs::module(context), "nodejs" => nodejs::module(context),
"ocaml" => ocaml::module(context), "ocaml" => ocaml::module(context),
"package" => package::module(context), "package" => package::module(context),
"perl" => perl::module(context),
"php" => php::module(context), "php" => php::module(context),
"purescript" => purescript::module(context), "purescript" => purescript::module(context),
"python" => python::module(context), "python" => python::module(context),
@ -149,6 +151,7 @@ pub fn description(module: &str) -> &'static str {
"nodejs" => "The currently installed version of NodeJS", "nodejs" => "The currently installed version of NodeJS",
"ocaml" => "The currently installed version of OCaml", "ocaml" => "The currently installed version of OCaml",
"package" => "The package version of the current directory's project", "package" => "The package version of the current directory's project",
"perl" => "The currently installed version of Perl",
"php" => "The currently installed version of PHP", "php" => "The currently installed version of PHP",
"purescript" => "The currently installed version of PureScript", "purescript" => "The currently installed version of PureScript",
"python" => "The currently installed version of Python", "python" => "The currently installed version of Python",

232
src/modules/perl.rs Normal file
View File

@ -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<Module<'a>> {
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()
}
}

View File

@ -111,6 +111,10 @@ active boot switches: -d:release\n",
stdout: String::from("4.08.1\n"), stdout: String::from("4.08.1\n"),
stderr: String::default(), 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;'" => { "php -nr 'echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;'" => {
Some(CommandOutput { Some(CommandOutput {
stdout: String::from("7.3.8"), stdout: String::from("7.3.8"),