diff --git a/docs/config/README.md b/docs/config/README.md index 3466c644a..67d27130c 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -102,6 +102,7 @@ prompt_order = [ "hg_branch", "package", "dotnet", + "elixir", "elm", "golang", "haskell", @@ -470,6 +471,29 @@ variable = "SHELL" default = "unknown shell" ``` +## Elixir + +The `elixir` module shows the currently installed version of Elixir and Erlang/OTP. +The module will be shown if any of the following conditions are met: + +- The current directory contains a `mix.exs` file. + +### Options + +| Variable | Default | Description | +| ---------- | ------------ | ------------------------------------------------------ | +| `symbol` | `"💧 "` | The symbol used before displaying the version of Rust. | +| `disabled` | `false` | Disables the `elixir` module. | + +### Example + +```toml +# ~/.config/starship.toml + +[elixir] +symbol = "🔮 " +``` + ## Git Branch The `git_branch` module shows the active branch of the repo in your current directory. diff --git a/src/configs/elixir.rs b/src/configs/elixir.rs new file mode 100644 index 000000000..cb9d701e4 --- /dev/null +++ b/src/configs/elixir.rs @@ -0,0 +1,25 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct ElixirConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub version: SegmentConfig<'a>, + pub otp_version: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for ElixirConfig<'a> { + fn new() -> Self { + ElixirConfig { + symbol: SegmentConfig::new("💧 "), + version: SegmentConfig::default(), + otp_version: SegmentConfig::default(), + style: Color::Purple.bold(), + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index af62804c1..d1ab700b4 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -6,6 +6,7 @@ pub mod conda; pub mod crystal; pub mod directory; pub mod dotnet; +pub mod elixir; pub mod elm; pub mod env_var; pub mod git_branch; diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index f645c2c93..642891aba 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -31,6 +31,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> { // ↓ Toolchain version modules ↓ // (Let's keep these sorted alphabetically) "dotnet", + "elixir", "elm", "golang", "haskell", diff --git a/src/module.rs b/src/module.rs index cfce216c4..b52c77567 100644 --- a/src/module.rs +++ b/src/module.rs @@ -18,6 +18,7 @@ pub const ALL_MODULES: &[&str] = &[ "conda", "directory", "dotnet", + "elixir", "elm", "env_var", "git_branch", diff --git a/src/modules/elixir.rs b/src/modules/elixir.rs new file mode 100644 index 000000000..d68d11d6c --- /dev/null +++ b/src/modules/elixir.rs @@ -0,0 +1,109 @@ +use regex::Regex; + +use super::{Context, Module, RootModuleConfig}; + +use crate::configs::elixir::ElixirConfig; + +const ELIXIR_VERSION_PATTERN: &str = "\ +Erlang/OTP (?P\\d+)[^\\n]+ + +Elixir (?P\\d[.\\d]+).*"; + +/// Create a module with the current Elixir version +/// +/// Will display the Rust version if any of the following criteria are met: +/// - Current directory contains a `mix.exs` file +pub fn module<'a>(context: &'a Context) -> Option> { + let is_elixir_project = context.try_begin_scan()?.set_files(&["mix.exs"]).is_match(); + + if !is_elixir_project { + return None; + } + + let (otp_version, elixir_version) = get_elixir_version()?; + + let mut module = context.new_module("elixir"); + let config = ElixirConfig::try_load(module.config); + module.set_style(config.style); + + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &config.version.with_value(&elixir_version)); + module.create_segment( + "otp_version", + &config + .otp_version + .with_value(&format!(" (OTP {})", otp_version)), + ); + + Some(module) +} + +fn get_elixir_version() -> Option<(String, String)> { + use crate::utils; + + let output = utils::exec_cmd("elixir", &["--version"])?.stdout; + + parse_elixir_version(&output) +} + +fn parse_elixir_version(version: &str) -> Option<(String, String)> { + let version_regex = Regex::new(ELIXIR_VERSION_PATTERN).ok()?; + let captures = version_regex.captures(version)?; + + let otp_version = captures["otp"].to_owned(); + let elixir_version = captures["elixir"].to_owned(); + + Some((otp_version, elixir_version)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::File; + use std::io; + use tempfile; + + #[test] + fn test_parse_elixir_version() { + const OUTPUT: &str = "\ +Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] + +Elixir 1.10 (compiled with Erlang/OTP 22) +"; + + assert_eq!( + parse_elixir_version(OUTPUT), + Some(("22".to_owned(), "1.10".to_owned())) + ); + } + + #[test] + fn test_without_mix_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let expected = None; + let output = render_module("elixir", dir.path()); + + assert_eq!(output, expected); + + Ok(()) + } + + #[test] + fn test_with_mix_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("mix.exs"))?.sync_all()?; + + let expected = Some(format!( + "via {} ", + Color::Purple.bold().paint("💧 1.10 (OTP 22)") + )); + let output = render_module("elixir", dir.path()); + + assert_eq!(output, expected); + + Ok(()) + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 982dabba8..5fd21797c 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -6,6 +6,7 @@ mod conda; mod crystal; mod directory; mod dotnet; +mod elixir; mod elm; mod env_var; mod git_branch; @@ -53,6 +54,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "conda" => conda::module(context), "directory" => directory::module(context), "dotnet" => dotnet::module(context), + "elixir" => elixir::module(context), "elm" => elm::module(context), "env_var" => env_var::module(context), "git_branch" => git_branch::module(context), diff --git a/src/utils.rs b/src/utils.rs index 5a9fbef4f..5e29400aa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -73,6 +73,15 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option { stdout: String::from("8.6.5"), stderr: String::default(), }), + "elixir --version" => Some(CommandOutput { + stdout: String::from( + "\ +Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] + +Elixir 1.10 (compiled with Erlang/OTP 22)", + ), + stderr: String::default(), + }), // If we don't have a mocked command fall back to executing the command _ => internal_exec_cmd(&cmd, &args), }