1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 15:56:28 +00:00

feat: add Elixir module (#437)

This commit is contained in:
KokaKiwi 2020-03-02 04:29:27 +01:00 committed by GitHub
parent 1eabd52725
commit 243e97cfe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 172 additions and 0 deletions

View File

@ -102,6 +102,7 @@ prompt_order = [
"hg_branch", "hg_branch",
"package", "package",
"dotnet", "dotnet",
"elixir",
"elm", "elm",
"golang", "golang",
"haskell", "haskell",
@ -470,6 +471,29 @@ variable = "SHELL"
default = "unknown 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 ## Git Branch
The `git_branch` module shows the active branch of the repo in your current directory. The `git_branch` module shows the active branch of the repo in your current directory.

25
src/configs/elixir.rs Normal file
View File

@ -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,
}
}
}

View File

@ -6,6 +6,7 @@ pub mod conda;
pub mod crystal; pub mod crystal;
pub mod directory; pub mod directory;
pub mod dotnet; pub mod dotnet;
pub mod elixir;
pub mod elm; pub mod elm;
pub mod env_var; pub mod env_var;
pub mod git_branch; pub mod git_branch;

View File

@ -31,6 +31,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
// ↓ Toolchain version modules ↓ // ↓ Toolchain version modules ↓
// (Let's keep these sorted alphabetically) // (Let's keep these sorted alphabetically)
"dotnet", "dotnet",
"elixir",
"elm", "elm",
"golang", "golang",
"haskell", "haskell",

View File

@ -18,6 +18,7 @@ pub const ALL_MODULES: &[&str] = &[
"conda", "conda",
"directory", "directory",
"dotnet", "dotnet",
"elixir",
"elm", "elm",
"env_var", "env_var",
"git_branch", "git_branch",

109
src/modules/elixir.rs Normal file
View File

@ -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<otp>\\d+)[^\\n]+
Elixir (?P<elixir>\\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<Module<'a>> {
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(())
}
}

View File

@ -6,6 +6,7 @@ mod conda;
mod crystal; mod crystal;
mod directory; mod directory;
mod dotnet; mod dotnet;
mod elixir;
mod elm; mod elm;
mod env_var; mod env_var;
mod git_branch; mod git_branch;
@ -53,6 +54,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"conda" => conda::module(context), "conda" => conda::module(context),
"directory" => directory::module(context), "directory" => directory::module(context),
"dotnet" => dotnet::module(context), "dotnet" => dotnet::module(context),
"elixir" => elixir::module(context),
"elm" => elm::module(context), "elm" => elm::module(context),
"env_var" => env_var::module(context), "env_var" => env_var::module(context),
"git_branch" => git_branch::module(context), "git_branch" => git_branch::module(context),

View File

@ -73,6 +73,15 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
stdout: String::from("8.6.5"), stdout: String::from("8.6.5"),
stderr: String::default(), 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 // If we don't have a mocked command fall back to executing the command
_ => internal_exec_cmd(&cmd, &args), _ => internal_exec_cmd(&cmd, &args),
} }