1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-25 03:16:01 +00:00

feat(PureScript): Add Purescript module (#1227)

* Added configs/purescript

* Added modules/purescript

* Added necessary codes

* Added tests

* Updated README

* Fixed color because black is hard to see

* Fixed of push mistake

* Fixed pointed out in PR
This commit is contained in:
nobv 2020-05-23 01:26:58 +09:00 committed by GitHub
parent a670e01c22
commit 63799b97d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 131 additions and 0 deletions

View File

@ -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 = "<=> "
```

View File

@ -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;

23
src/configs/purescript.rs Normal file
View File

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

View File

@ -42,6 +42,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
"nodejs",
"ocaml",
"php",
"purescript",
"python",
"ruby",
"rust",

View File

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

View File

@ -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<Module<'a>> {
"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",

72
src/modules/purescript.rs Normal file
View File

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

View File

@ -73,6 +73,10 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
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(),