summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormarcybell <marcelinebelardo@gmail.com>2024-03-21 00:57:26 +0300
committerGitHub <noreply@github.com>2024-03-20 22:57:26 +0100
commit938ea3c40145af68d8e28b2ef0275531f1123202 (patch)
treed797c9a1984f4238d8116dae2cbb1b4e92e78333
parent0e49f04a6b249090cf3703c5dac041a51e6bf530 (diff)
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
-rw-r--r--docs/config/README.md11
-rw-r--r--src/modules/ruby.rs68
2 files changed, 74 insertions, 5 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 213439514..0719540fd 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 498fefc3f..da056246d 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<Module<'a>> {
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<Strin
}
}
+fn format_rvm_gemset(current: &str) -> Option<String> {
+ 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;
@@ -164,6 +180,58 @@ mod tests {
}
#[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();
assert_eq!(