summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
authorboreinhardt <59684097+boreinhardt@users.noreply.github.com>2024-04-05 23:56:15 +0200
committerGitHub <noreply@github.com>2024-04-05 23:56:15 +0200
commit34a8f7e62845fd66df3f6f9003cb850f2b0d9bc5 (patch)
tree54f23e2ededee81a96477ea0ce62cf724e9b269f /src/modules
parentf9c4bef85d4375070c2a6b7d6c346759e8586ac7 (diff)
feat(gleam): Add Gleam module (#5886)
Co-authored-by: benno.reinhardt <benno.reinhardt@silbury.com>
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/gleam.rs122
-rw-r--r--src/modules/mod.rs3
2 files changed, 125 insertions, 0 deletions
diff --git a/src/modules/gleam.rs b/src/modules/gleam.rs
new file mode 100644
index 000000000..fa9a1e9b7
--- /dev/null
+++ b/src/modules/gleam.rs
@@ -0,0 +1,122 @@
+use super::{Context, Module, ModuleConfig};
+
+use crate::configs::gleam::GleamConfig;
+use crate::formatter::StringFormatter;
+use crate::formatter::VersionFormatter;
+
+/// Creates a module with the current Gleam version
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ let mut module = context.new_module("gleam");
+ let config = GleamConfig::try_load(module.config);
+
+ let is_gleam_project = context
+ .try_begin_scan()?
+ .set_files(&config.detect_files)
+ .set_extensions(&config.detect_extensions)
+ .set_folders(&config.detect_folders)
+ .is_match();
+
+ if !is_gleam_project {
+ return None;
+ }
+
+ 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" => {
+ let gleam_version =
+ parse_gleam_version(&context.exec_cmd("gleam", &["--version"])?.stdout)?;
+ VersionFormatter::format_module_version(
+ module.get_name(),
+ &gleam_version,
+ config.version_format,
+ )
+ .map(Ok)
+ }
+ _ => None,
+ })
+ .parse(None, Some(context))
+ });
+
+ module.set_segments(match parsed {
+ Ok(segments) => segments,
+ Err(error) => {
+ log::warn!("Error in module `gleam`:\n{}", error);
+ return None;
+ }
+ });
+
+ Some(module)
+}
+
+fn parse_gleam_version(version: &str) -> Option<String> {
+ let version = version.split_whitespace().last()?;
+ Some(version.to_string())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::test::ModuleRenderer;
+ use nu_ansi_term::Color;
+ use std::fs::File;
+ use std::io;
+
+ #[test]
+ fn test_folder_without_gleam_file() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+
+ let actual = ModuleRenderer::new("gleam").path(dir.path()).collect();
+
+ let expected = None;
+
+ assert_eq!(actual, expected);
+ dir.close()
+ }
+
+ #[test]
+ fn test_folder_with_gleam_file() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("hello.gleam"))?.sync_all()?;
+
+ let actual = ModuleRenderer::new("gleam").path(dir.path()).collect();
+
+ let expected = Some(format!(
+ "via {}",
+ Color::Rgb(255, 175, 243).bold().paint("⭐ v1.0.0 ")
+ ));
+
+ assert_eq!(actual, expected);
+ dir.close()
+ }
+
+ #[test]
+ fn test_folder_with_gleam_toml() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("gleam.toml"))?.sync_all()?;
+
+ let actual = ModuleRenderer::new("gleam").path(dir.path()).collect();
+
+ let expected = Some(format!(
+ "via {}",
+ Color::Rgb(255, 175, 243).bold().paint("⭐ v1.0.0 ")
+ ));
+
+ assert_eq!(actual, expected);
+ dir.close()
+ }
+
+ #[test]
+ fn test_parse_gleam_version() {
+ let version = "gleam 1.0.0";
+ assert_eq!(parse_gleam_version(version), Some("1.0.0".to_string()));
+ }
+}
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index a219cd0d2..8bbc5f5b9 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -33,6 +33,7 @@ mod git_commit;
mod git_metrics;
mod git_state;
mod git_status;
+mod gleam;
mod golang;
mod gradle;
mod guix_shell;
@@ -141,6 +142,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"git_metrics" => git_metrics::module(context),
"git_state" => git_state::module(context),
"git_status" => git_status::module(context),
+ "gleam" => gleam::module(context),
"golang" => golang::module(context),
"gradle" => gradle::module(context),
"guix_shell" => guix_shell::module(context),
@@ -260,6 +262,7 @@ pub fn description(module: &str) -> &'static str {
"git_metrics" => "The currently added/deleted lines in your repo",
"git_state" => "The current git operation, and it's progress",
"git_status" => "Symbol representing the state of the repo",
+ "gleam" => "The currently installed version of Gleam",
"golang" => "The currently installed version of Golang",
"gradle" => "The currently installed version of Gradle",
"guix_shell" => "The guix-shell environment",