summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndré Zanellato <30451287+AZanellato@users.noreply.github.com>2019-08-13 19:43:29 -0300
committerMatan Kushner <hello@matchai.me>2019-08-13 18:43:29 -0400
commitb06249d61c98813bf56c5c9fdc7d974a35b61dad (patch)
treebf13e98ea264925b7a99e762e4be36ae67cc5cff /src
parentf10bfe4616794967e9014c978c3819e91634e4d6 (diff)
feat: implement the ruby module (#131)
Diffstat (limited to 'src')
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/ruby.rs59
2 files changed, 61 insertions, 0 deletions
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 35bbe2d31..3ab7d8b3f 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -10,6 +10,7 @@ mod line_break;
mod nodejs;
mod package;
mod python;
+mod ruby;
mod rust;
mod username;
@@ -23,6 +24,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"nodejs" => nodejs::module(context),
"rust" => rust::module(context),
"python" => python::module(context),
+ "ruby" => ruby::module(context),
"golang" => golang::module(context),
"line_break" => line_break::module(context),
"package" => package::module(context),
diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs
new file mode 100644
index 000000000..ff3945360
--- /dev/null
+++ b/src/modules/ruby.rs
@@ -0,0 +1,59 @@
+use ansi_term::Color;
+use std::process::Command;
+
+use super::{Context, Module};
+
+/// Creates a module with the current Ruby version
+///
+/// Will display the Ruby version if any of the following criteria are met:
+/// - Current directory contains a `.rb` file
+/// - Current directory contains a `Gemfile` file
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ let is_rb_project = context
+ .new_scan_dir()
+ .set_files(&["Gemfile"])
+ .set_extensions(&["rb"])
+ .scan();
+
+ if !is_rb_project {
+ return None;
+ }
+
+ match get_ruby_version() {
+ Some(ruby_version) => {
+ const RUBY_CHAR: &str = "💎 ";
+ let module_color = Color::Red.bold();
+
+ let mut module = context.new_module("ruby")?;
+ module.set_style(module_color);
+
+ let formatted_version = format_ruby_version(&ruby_version)?;
+ module.new_segment("symbol", RUBY_CHAR);
+ module.new_segment("version", &formatted_version);
+
+ Some(module)
+ }
+ None => None,
+ }
+}
+
+fn get_ruby_version() -> Option<String> {
+ match Command::new("ruby").arg("-v").output() {
+ Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
+ Err(_) => None,
+ }
+}
+
+fn format_ruby_version(ruby_version: &str) -> Option<String> {
+ let version = ruby_version
+ // split into ["ruby", "2.6.0p0", "linux/amd64"]
+ .split_whitespace()
+ // return "2.6.0p0"
+ .nth(1)?
+ .get(0..5)?;
+
+ let mut formatted_version = String::with_capacity(version.len() + 1);
+ formatted_version.push('v');
+ formatted_version.push_str(version);
+ Some(formatted_version)
+}