feat(nim): Add Nim module (#1189)

* Nim module and tests

* Add nim to docs

* parse_nim_version refactor, add nim desc

* Add nim symbol to Nerd Font preset

* Yellow with v prefix as default version

* Nim version fmt fix

* Update docs/config/README.md

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Update src/modules/nim.rs

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Nim module and tests

* Add nim to docs

* Yellow with v prefix as default version

* Update docs/config/README.md

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Update src/modules/nim.rs

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Add nim to docs, proper version formatting.

* Remove v from symbol, add to fmt string

* cargo fmt

Co-authored-by: Dario Vladovic <d.vladimyr@gmail.com>
This commit is contained in:
Rekihyt 2020-06-09 10:14:47 -07:00 committed by GitHub
parent fb121cd62a
commit fcd003785b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 205 additions and 9 deletions

View file

@ -115,6 +115,7 @@ prompt_order = [
"haskell",
"java",
"julia",
"nim",
"nodejs",
"ocaml",
"php",
@ -1016,6 +1017,33 @@ truncation_length = 4
truncation_symbol = ""
```
## Nim
The `nim` module shows the currently installed version of Nim.
The module will be shown if any of the following conditions are met:
- The current directory contains a `nim.cfg` file
- The current directory contains a file with the `.nim` extension
- The current directory contains a file with the `.nims` extension
- The current directory contains a file with the `.nimble` extension
### Options
| Variable | Default | Description |
| ---------- | --------------- | ------------------------------------------------------ |
| `symbol` | `"👑 "` | The symbol used before displaying the version of Nim. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `nim` module. |
### Example
```toml
# ~/.config/starship.toml
[nim]
style = "yellow"
symbol = "🎣 "
```
## Nix-shell
The `nix_shell` module shows the nix-shell environment.

View file

@ -58,6 +58,9 @@ symbol = " "
[memory_usage]
symbol = " "
[nim]
symbol = " "
[nix_shell]
symbol = " "

View file

@ -25,6 +25,7 @@ pub mod jobs;
pub mod julia;
pub mod kubernetes;
pub mod memory_usage;
pub mod nim;
pub mod nix_shell;
pub mod nodejs;
pub mod ocaml;

23
src/configs/nim.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 NimConfig<'a> {
pub symbol: SegmentConfig<'a>,
pub version: SegmentConfig<'a>,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for NimConfig<'a> {
fn new() -> Self {
NimConfig {
symbol: SegmentConfig::new("👑 "),
version: SegmentConfig::default(),
style: Color::Yellow.bold(),
disabled: false,
}
}
}

View file

@ -39,6 +39,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
"haskell",
"java",
"julia",
"nim",
"nodejs",
"ocaml",
"php",

View file

@ -37,6 +37,7 @@ pub const ALL_MODULES: &[&str] = &[
"kubernetes",
"line_break",
"memory_usage",
"nim",
"nix_shell",
"nodejs",
"ocaml",

View file

@ -26,6 +26,7 @@ mod julia;
mod kubernetes;
mod line_break;
mod memory_usage;
mod nim;
mod nix_shell;
mod nodejs;
mod ocaml;
@ -80,6 +81,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"kubernetes" => kubernetes::module(context),
"line_break" => line_break::module(context),
"memory_usage" => memory_usage::module(context),
"nim" => nim::module(context),
"nix_shell" => nix_shell::module(context),
"nodejs" => nodejs::module(context),
"ocaml" => ocaml::module(context),
@ -131,6 +133,7 @@ pub fn description(module: &str) -> &'static str {
"kubernetes" => "The current Kubernetes context name and, if set, the namespace",
"line_break" => "Separates the prompt into two lines",
"memory_usage" => "Current system memory and swap usage",
"nim" => "The currently installed version of Nim",
"nix_shell" => "The nix-shell environment",
"nodejs" => "The currently installed version of NodeJS",
"ocaml" => "The currently installed version of OCaml",

125
src/modules/nim.rs Normal file
View file

@ -0,0 +1,125 @@
use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::nim::NimConfig;
use crate::utils;
/// Creates a module with the current Nim version
///
/// Will display the Nim version if any of the following criteria are met:
/// - The current directory contains a file with extension `.nim`, `.nims`, or `.nimble`
/// - The current directory contains a `nim.cfg` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let is_nim_project = context
.try_begin_scan()?
.set_files(&["nim.cfg"])
.set_extensions(&["nim", "nims", "nimble"])
.is_match();
if !is_nim_project {
return None;
}
let nim_version_output = utils::exec_cmd("nim", &["--version"])?.stdout;
let formatted_nim_version = format!("v{}", parse_nim_version(&nim_version_output)?);
let mut module = context.new_module("nim");
let config = NimConfig::try_load(module.config);
module.set_style(config.style);
module.create_segment("symbol", &config.symbol);
module.create_segment("version", &SegmentConfig::new(&formatted_nim_version));
Some(module)
}
fn parse_nim_version(version_cmd_output: &str) -> Option<&str> {
version_cmd_output
.lines()
// First line has the version
.next()?
.split(' ')
.find(|&s| s.chars().all(|c| c >= '0' && c <= '9' || c == '.'))
}
#[cfg(test)]
mod tests {
use super::parse_nim_version;
use crate::modules::utils::test::render_module;
use ansi_term::Color;
use std::fs::File;
use std::io;
#[test]
fn nim_version() {
let ok_versions = ["1.1.1", "2", "."];
let not_ok_versions = ["abc", " \n.", ". ", "abc."];
let all_some = ok_versions.iter().all(|&v| parse_nim_version(v).is_some());
let all_none = not_ok_versions
.iter()
.any(|&v| parse_nim_version(v).is_some());
assert_eq!(true, all_some);
assert_eq!(true, all_none);
let sample_nimc_output = "Nim Compiler Version 1.2.0 [Linux: amd64]
Compiled at 2020-04-03
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: 7e83adff84be5d0c401a213eccb61e321a3fb1ff
active boot switches: -d:release\n";
assert_eq!(Some("1.2.0"), parse_nim_version(sample_nimc_output))
}
#[test]
fn folder_without_nim() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("nim.txt"))?.sync_all()?;
let actual = render_module("nim", dir.path(), None);
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_nimble_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("main.nimble"))?.sync_all()?;
let actual = render_module("nim", dir.path(), None);
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("👑 v1.2.0")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_nim_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("main.nim"))?.sync_all()?;
let actual = render_module("nim", dir.path(), None);
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("👑 v1.2.0")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_nims_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("main.nims"))?.sync_all()?;
let actual = render_module("nim", dir.path(), None);
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("👑 v1.2.0")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_cfg_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("cfg.nim"))?.sync_all()?;
let actual = render_module("nim", dir.path(), None);
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("👑 v1.2.0")));
assert_eq!(expected, actual);
dir.close()
}
}

View file

@ -47,6 +47,15 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
stdout: String::from("stdout ok!"),
stderr: String::from("stderr ok!"),
}),
"elixir --version" => Some(CommandOutput {
stdout: String::from(
"\
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
Elixir 1.10 (compiled with Erlang/OTP 22)",
),
stderr: String::default(),
}),
"elm --version" => Some(CommandOutput {
stdout: String::from("0.19.1"),
stderr: String::default(),
@ -59,6 +68,17 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
stdout: String::from("julia version 1.4.0"),
stderr: String::default(),
}),
"nim --version" => Some(CommandOutput {
stdout: String::from(
"\
Nim Compiler Version 1.2.0 [Linux: amd64]
Compiled at 2020-04-03
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: 7e83adff84be5d0c401a213eccb61e321a3fb1ff
active boot switches: -d:release\n",
),
stderr: String::default(),
}),
"node --version" => Some(CommandOutput {
stdout: String::from("v12.0.0"),
stderr: String::default(),
@ -87,15 +107,6 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
stderr: String::default(),
})
}
"elixir --version" => Some(CommandOutput {
stdout: String::from(
"\
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
Elixir 1.10 (compiled with Erlang/OTP 22)",
),
stderr: String::default(),
}),
"zig version" => Some(CommandOutput {
stdout: String::from("0.6.0"),
stderr: String::default(),