From 938ea3c40145af68d8e28b2ef0275531f1123202 Mon Sep 17 00:00:00 2001 From: marcybell Date: Thu, 21 Mar 2024 00:57:26 +0300 Subject: [PATCH] feat: $gemset variable for Ruby module (#5429) * $gemset variable for Ruby module * typo * Added test for no GEM_HOME env set * Formatting * Uses `rvm current` for gemset, no more version num --- docs/config/README.md | 11 +++---- src/modules/ruby.rs | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 21343951..0719540f 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3772,11 +3772,12 @@ Starship gets the current Ruby version by running `ruby -v`. ### Variables -| Variable | Example | Description | -| -------- | -------- | ------------------------------------ | -| version | `v2.5.1` | The version of `ruby` | -| symbol | | Mirrors the value of option `symbol` | -| style\* | | Mirrors the value of option `style` | +| Variable | Example | Description | +| -------- | -------- | ------------------------------------------- | +| version | `v2.5.1` | The version of `ruby` | +| symbol | | Mirrors the value of option `symbol` | +| style\* | | Mirrors the value of option `style` | +| gemset | `test` | Optional, gets the current RVM gemset name. | *: This variable can only be used as a part of a style string diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs index 498fefc3..da056246 100644 --- a/src/modules/ruby.rs +++ b/src/modules/ruby.rs @@ -1,3 +1,5 @@ +use regex::Regex; + use super::{Context, Module, ModuleConfig}; use crate::configs::ruby::RubyConfig; @@ -45,6 +47,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { config.version_format, ) .map(Ok), + "gemset" => { + format_rvm_gemset(&context.exec_cmd("rvm", &["current"])?.stdout).map(Ok) + } _ => None, }) .parse(None, Some(context)) @@ -81,10 +86,21 @@ fn format_ruby_version(ruby_version: &str, version_format: &str) -> Option Option { + let gemset_re = Regex::new(r"@(\S+)").unwrap(); + if let Some(gemset) = gemset_re.captures(current) { + let gemset_name = gemset.get(1)?.as_str(); + return Some(gemset_name.to_string()); + } + + None +} + #[cfg(test)] mod tests { use super::*; use crate::test::ModuleRenderer; + use crate::utils::CommandOutput; use nu_ansi_term::Color; use std::fs::File; use std::io; @@ -163,6 +179,58 @@ mod tests { dir.close() } + #[test] + fn rvm_gemset_active() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.rb"))?.sync_all()?; + + let actual = ModuleRenderer::new("ruby") + .path(dir.path()) + .cmd( + "rvm current", + Some(CommandOutput { + stdout: String::from("ruby-2.5.1@test\n"), + stderr: String::default(), + }), + ) + .config(toml::toml! { + [ruby] + format = "via [$symbol($version)@($gemset )]($style)" + version_format = "${raw}" + }) + .collect(); + let expected = Some(format!("via {}", Color::Red.bold().paint("💎 2.5.1@test "))); + + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn rvm_gemset_not_active() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.rb"))?.sync_all()?; + + let actual = ModuleRenderer::new("ruby") + .path(dir.path()) + .cmd( + "rvm current", + Some(CommandOutput { + // with no gemset, `rvm current` outputs an empty string + stdout: String::default(), + stderr: String::default(), + }), + ) + .config(toml::toml! { + [ruby] + format = "via [$symbol($version)(@$gemset) ]($style)" + }) + .collect(); + let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 "))); + + assert_eq!(expected, actual); + dir.close() + } + #[test] fn test_format_ruby_version() { let config = RubyConfig::default();