mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-09-29 21:49:07 +00:00
097f1b05f1
- Create `Config` struct that is added to `Context` when initialized - Read `~/.confg/starship.toml` during initialization (can be updated later to also look at `$XDG_CONFIG_HOME`) - `Context` now has a method for creating modules. This allows us to provide modules with a reference to the configuration specific to that module
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use ansi_term::Color;
|
|
use std::process::Command;
|
|
|
|
use super::{Context, Module};
|
|
|
|
/// Creates a segment with the current Node.js version
|
|
///
|
|
/// Will display the Node.js version if any of the following criteria are met:
|
|
/// - Current directory contains a `.js` file
|
|
/// - Current directory contains a `package.json` file
|
|
/// - Current directory contains a `node_modules` directory
|
|
pub fn segment<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
let is_js_project = context
|
|
.new_scan_dir()
|
|
.set_files(&["package.json"])
|
|
.set_extensions(&["js"])
|
|
.set_folders(&["node_modules"])
|
|
.scan();
|
|
|
|
if !is_js_project {
|
|
return None;
|
|
}
|
|
|
|
match get_node_version() {
|
|
Some(node_version) => {
|
|
const NODE_CHAR: &str = "⬢ ";
|
|
let module_color = Color::Green.bold();
|
|
|
|
let mut module = context.new_module("node");
|
|
module.set_style(module_color);
|
|
|
|
let formatted_version = node_version.trim();
|
|
module.new_segment("symbol", NODE_CHAR);
|
|
module.new_segment("version", formatted_version);
|
|
|
|
Some(module)
|
|
}
|
|
None => None,
|
|
}
|
|
}
|
|
|
|
fn get_node_version() -> Option<String> {
|
|
match Command::new("node").arg("--version").output() {
|
|
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
|
|
Err(_) => None,
|
|
}
|
|
}
|