1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2025-01-30 02:28:28 +00:00
starship/src/modules/elixir.rs

157 lines
4.6 KiB
Rust
Raw Normal View History

use super::{Context, Module, ModuleConfig};
2020-03-02 04:29:27 +01:00
use crate::configs::elixir::ElixirConfig;
use crate::formatter::StringFormatter;
2020-03-02 04:29:27 +01:00
feat: Add version formating for modules (#2611) * format crystal version with VersionFormatter * update crystal dosc * format crystal module * fix typos * format dart version with VersionFormatter * fix dart malformed test * update dart docs * format cmake version with VersionFormatter * update cmake docs * format deno version with VersionFormatter * update deno docs * remove Version type * format dotnet version with VersionFormatter * update dotnet docs * format erlang version with VersionFormatter * update erlang docs * format golang version with VersionFormatter * refactor formatting in my modules * format helm version with VersionFormatter * format julia version with VersionFormatter * format kotlin version with VersionFormatter * format lua version with VersionFormatter * format nim version with VersionFormatter * format perl version with VersionFormatter * format php version with VersionFormatter * format purescript version with VersionFormatter * format scala version with VersionFormatter * format swift version with VersionFormatter * format terraform version with VersionFormatter * format vagrant version with VersionFormatter * format zig version with VersionFormatter * format elixir version with VersionFormatter * format ocaml version with VersionFormatter * update elixir docs * update golang docs * update helm docs * update julia docs * update kotlin docs * update lua docs * update nim docs * update ocaml docs * update perl docs * update php docs * update purescript docs * update scala docs * update swift docs * update terraform docs * update vagrant docs * update zig docs * format elm version with VersionFormatter * update elm docs * pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00
use crate::formatter::VersionFormatter;
use std::ops::Deref;
use std::sync::LazyLock;
2020-03-02 04:29:27 +01:00
/// Create a module with the current Elixir version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("elixir");
let config = ElixirConfig::try_load(module.config);
let is_elixir_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
2020-03-02 04:29:27 +01:00
if !is_elixir_project {
return None;
}
let versions = LazyLock::new(|| get_elixir_version(context));
2020-03-02 04:29:27 +01:00
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"version" => versions
.deref()
.as_ref()
.map(|(_, elixir_version)| elixir_version)
feat: Add version formating for modules (#2611) * format crystal version with VersionFormatter * update crystal dosc * format crystal module * fix typos * format dart version with VersionFormatter * fix dart malformed test * update dart docs * format cmake version with VersionFormatter * update cmake docs * format deno version with VersionFormatter * update deno docs * remove Version type * format dotnet version with VersionFormatter * update dotnet docs * format erlang version with VersionFormatter * update erlang docs * format golang version with VersionFormatter * refactor formatting in my modules * format helm version with VersionFormatter * format julia version with VersionFormatter * format kotlin version with VersionFormatter * format lua version with VersionFormatter * format nim version with VersionFormatter * format perl version with VersionFormatter * format php version with VersionFormatter * format purescript version with VersionFormatter * format scala version with VersionFormatter * format swift version with VersionFormatter * format terraform version with VersionFormatter * format vagrant version with VersionFormatter * format zig version with VersionFormatter * format elixir version with VersionFormatter * format ocaml version with VersionFormatter * update elixir docs * update golang docs * update helm docs * update julia docs * update kotlin docs * update lua docs * update nim docs * update ocaml docs * update perl docs * update php docs * update purescript docs * update scala docs * update swift docs * update terraform docs * update vagrant docs * update zig docs * format elm version with VersionFormatter * update elm docs * pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00
.map(|elixir_version| {
VersionFormatter::format_module_version(
module.get_name(),
elixir_version,
feat: Add version formating for modules (#2611) * format crystal version with VersionFormatter * update crystal dosc * format crystal module * fix typos * format dart version with VersionFormatter * fix dart malformed test * update dart docs * format cmake version with VersionFormatter * update cmake docs * format deno version with VersionFormatter * update deno docs * remove Version type * format dotnet version with VersionFormatter * update dotnet docs * format erlang version with VersionFormatter * update erlang docs * format golang version with VersionFormatter * refactor formatting in my modules * format helm version with VersionFormatter * format julia version with VersionFormatter * format kotlin version with VersionFormatter * format lua version with VersionFormatter * format nim version with VersionFormatter * format perl version with VersionFormatter * format php version with VersionFormatter * format purescript version with VersionFormatter * format scala version with VersionFormatter * format swift version with VersionFormatter * format terraform version with VersionFormatter * format vagrant version with VersionFormatter * format zig version with VersionFormatter * format elixir version with VersionFormatter * format ocaml version with VersionFormatter * update elixir docs * update golang docs * update helm docs * update julia docs * update kotlin docs * update lua docs * update nim docs * update ocaml docs * update perl docs * update php docs * update purescript docs * update scala docs * update swift docs * update terraform docs * update vagrant docs * update zig docs * format elm version with VersionFormatter * update elm docs * pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00
config.version_format,
)
})?
.map(Ok),
"otp_version" => versions
.deref()
.as_ref()
feat: Add version formating for modules (#2611) * format crystal version with VersionFormatter * update crystal dosc * format crystal module * fix typos * format dart version with VersionFormatter * fix dart malformed test * update dart docs * format cmake version with VersionFormatter * update cmake docs * format deno version with VersionFormatter * update deno docs * remove Version type * format dotnet version with VersionFormatter * update dotnet docs * format erlang version with VersionFormatter * update erlang docs * format golang version with VersionFormatter * refactor formatting in my modules * format helm version with VersionFormatter * format julia version with VersionFormatter * format kotlin version with VersionFormatter * format lua version with VersionFormatter * format nim version with VersionFormatter * format perl version with VersionFormatter * format php version with VersionFormatter * format purescript version with VersionFormatter * format scala version with VersionFormatter * format swift version with VersionFormatter * format terraform version with VersionFormatter * format vagrant version with VersionFormatter * format zig version with VersionFormatter * format elixir version with VersionFormatter * format ocaml version with VersionFormatter * update elixir docs * update golang docs * update helm docs * update julia docs * update kotlin docs * update lua docs * update nim docs * update ocaml docs * update perl docs * update php docs * update purescript docs * update scala docs * update swift docs * update terraform docs * update vagrant docs * update zig docs * format elm version with VersionFormatter * update elm docs * pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00
.map(|(otp_version, _)| otp_version.to_string())
.map(Ok),
_ => None,
})
.parse(None, Some(context))
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `elixir`:\n{}", error);
return None;
}
});
2020-03-02 04:29:27 +01:00
Some(module)
}
fn get_elixir_version(context: &Context) -> Option<(String, String)> {
let output = context.exec_cmd("elixir", &["--version"])?.stdout;
2020-03-02 04:29:27 +01:00
parse_elixir_version(&output)
}
fn parse_elixir_version(version: &str) -> Option<(String, String)> {
let mut lines = version.lines();
// split line into ["Erlang/OTP", "22", "[erts-10.5]", ...], take "22"
let otp_version = lines.next()?.split_whitespace().nth(1)?;
// skip empty line
let _ = lines.next()?;
// split line into ["Elixir", "1.10", "(compiled", ...], take "1.10"
let elixir_version = lines.next()?.split_whitespace().nth(1)?;
Some((otp_version.to_string(), elixir_version.to_string()))
2020-03-02 04:29:27 +01:00
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use nu_ansi_term::Color;
2020-03-02 04:29:27 +01:00
use std::fs::File;
use std::io;
#[test]
fn test_parse_elixir_version() {
let stable_input = "\
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Elixir 1.11.3 (compiled with Erlang/OTP 21)
";
let rc_input = "\
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
2020-03-02 04:29:27 +01:00
Elixir 1.12.0-rc.0 (31d2b99) (compiled with Erlang/OTP 21)
2020-03-02 04:29:27 +01:00
";
let dev_input = "\
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
2020-03-02 04:29:27 +01:00
Elixir 1.13.0-dev (compiled with Erlang/OTP 23)
";
assert_eq!(
parse_elixir_version(stable_input),
Some(("23".to_string(), "1.11.3".to_string()))
);
assert_eq!(
parse_elixir_version(rc_input),
Some(("23".to_string(), "1.12.0-rc.0".to_string()))
);
2020-03-02 04:29:27 +01:00
assert_eq!(
parse_elixir_version(dev_input),
Some(("23".to_string(), "1.13.0-dev".to_string()))
2020-03-02 04:29:27 +01:00
);
}
#[test]
fn test_without_mix_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let expected = None;
let output = ModuleRenderer::new("elixir").path(dir.path()).collect();
2020-03-02 04:29:27 +01:00
assert_eq!(output, expected);
dir.close()
2020-03-02 04:29:27 +01:00
}
#[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 {}",
feat: Add version formating for modules (#2611) * format crystal version with VersionFormatter * update crystal dosc * format crystal module * fix typos * format dart version with VersionFormatter * fix dart malformed test * update dart docs * format cmake version with VersionFormatter * update cmake docs * format deno version with VersionFormatter * update deno docs * remove Version type * format dotnet version with VersionFormatter * update dotnet docs * format erlang version with VersionFormatter * update erlang docs * format golang version with VersionFormatter * refactor formatting in my modules * format helm version with VersionFormatter * format julia version with VersionFormatter * format kotlin version with VersionFormatter * format lua version with VersionFormatter * format nim version with VersionFormatter * format perl version with VersionFormatter * format php version with VersionFormatter * format purescript version with VersionFormatter * format scala version with VersionFormatter * format swift version with VersionFormatter * format terraform version with VersionFormatter * format vagrant version with VersionFormatter * format zig version with VersionFormatter * format elixir version with VersionFormatter * format ocaml version with VersionFormatter * update elixir docs * update golang docs * update helm docs * update julia docs * update kotlin docs * update lua docs * update nim docs * update ocaml docs * update perl docs * update php docs * update purescript docs * update scala docs * update swift docs * update terraform docs * update vagrant docs * update zig docs * format elm version with VersionFormatter * update elm docs * pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00
Color::Purple.bold().paint("💧 v1.10 (OTP 22) ")
2020-03-02 04:29:27 +01:00
));
let output = ModuleRenderer::new("elixir").path(dir.path()).collect();
2020-03-02 04:29:27 +01:00
assert_eq!(output, expected);
dir.close()
2020-03-02 04:29:27 +01:00
}
}