mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-01 09:13:54 +00:00
edc62f4518
Have refactored the golang, java, nodejs, python, ruby and username modules to use the new `exec_cmd` util.
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
|
|
|
use crate::configs::ruby::RubyConfig;
|
|
use crate::utils;
|
|
|
|
/// 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
|
|
.try_begin_scan()?
|
|
.set_files(&["Gemfile"])
|
|
.set_extensions(&["rb"])
|
|
.is_match();
|
|
|
|
if !is_rb_project {
|
|
return None;
|
|
}
|
|
|
|
let ruby_version = utils::exec_cmd("ruby", &["-v"])?.stdout;
|
|
let formatted_version = format_ruby_version(&ruby_version)?;
|
|
|
|
let mut module = context.new_module("ruby");
|
|
let config: RubyConfig = RubyConfig::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)
|
|
}
|
|
|
|
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)
|
|
}
|