diff --git a/docs/config/README.md b/docs/config/README.md index 00b5b25f5..5109b4d9e 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -188,6 +188,7 @@ $hg_branch\ $docker_context\ $package\ $cmake\ +$dart\ $dotnet\ $elixir\ $elm\ @@ -575,6 +576,43 @@ The module will be shown if any of the following conditions are met: format = "via [✨ $version](bold blue) " ``` +## Dart + +The `dart` module shows the currently installed version of Dart. +The module will be shown if any of the following conditions are met: + +- The current directory contains a file with `.dart` extension +- The current directory contains a `.dart_tool` directory +- The current directory contains a `pubspec.yaml` or `pubspec.lock` file + +### Options + +| Variable | Default | Description | +| ---------- | ---------------------------------- | ----------------------------------------------- | +| `format` | `"via [$symbol$version]($style) "` | The format for the module. | +| `symbol` | `"🎯 "` | A format string representing the symbol of Dart | +| `style` | `"bold blue"` | The style for the module. | +| `disabled` | `false` | Disables the `dart` module. | + +### Variables + +| Variable | Example | Description | +| -------- | -------- | ------------------------------------ | +| version | `v2.8.4` | The version of `dart` | +| symbol | | Mirrors the value of option `symbol` | +| style\* | | Mirrors the value of option `style` | + +\*: This variable can only be used as a part of a style string + +### Example + +```toml +# ~/.config/starship.toml + +[dart] +format = "via [🔰 $version](bold red) " +``` + ## Directory The `directory` module shows the path to your current directory, truncated to diff --git a/docs/presets/README.md b/docs/presets/README.md index c7989cf7b..aeb3f8236 100644 --- a/docs/presets/README.md +++ b/docs/presets/README.md @@ -28,6 +28,9 @@ discharging_symbol = "" [conda] symbol = " " +[dart] +symbol = " " + [docker] symbol = " " diff --git a/src/configs/dart.rs b/src/configs/dart.rs new file mode 100644 index 000000000..f0353218f --- /dev/null +++ b/src/configs/dart.rs @@ -0,0 +1,22 @@ +use crate::config::{ModuleConfig, RootModuleConfig}; + +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct DartConfig<'a> { + pub format: &'a str, + pub symbol: &'a str, + pub style: &'a str, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for DartConfig<'a> { + fn new() -> Self { + DartConfig { + format: "via [$symbol$version]($style) ", + symbol: "🎯 ", + style: "bold blue", + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 7cf208777..3ba51bd85 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -6,6 +6,7 @@ pub mod cmd_duration; pub mod conda; pub mod crystal; pub mod custom; +pub mod dart; pub mod directory; pub mod docker_context; pub mod dotnet; diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index b740c0996..e832f3098 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -27,6 +27,7 @@ pub const PROMPT_ORDER: &[&str] = &[ // ↓ Toolchain version modules ↓ // (Let's keep these sorted alphabetically) "cmake", + "dart", "dotnet", "elixir", "elm", diff --git a/src/module.rs b/src/module.rs index 3f33c9d2a..5e833830f 100644 --- a/src/module.rs +++ b/src/module.rs @@ -15,6 +15,7 @@ pub const ALL_MODULES: &[&str] = &[ "cmake", "cmd_duration", "conda", + "dart", "directory", "docker_context", "dotnet", diff --git a/src/modules/dart.rs b/src/modules/dart.rs new file mode 100644 index 000000000..ecf7b47c8 --- /dev/null +++ b/src/modules/dart.rs @@ -0,0 +1,145 @@ +use super::{Context, Module, RootModuleConfig}; + +use crate::configs::dart::DartConfig; +use crate::formatter::StringFormatter; +use crate::utils; + +/// Creates a module with the current Dart version +/// +/// Will display the Dart version if any of the following criteria are met: +/// - Current directory contains a file with `.dart` extension +/// - Current directory contains a `.dart_tool` directory +/// - Current directory contains a `pubspec.yaml`/`pubspec.yml` or `pubspec.lock` file +pub fn module<'a>(context: &'a Context) -> Option> { + let is_dart_project = context + .try_begin_scan()? + .set_extensions(&["dart"]) + .set_folders(&[".dart_tool"]) + .set_files(&["pubspec.yaml", "pubspec.yml", "pubspec.lock"]) + .is_match(); + + if !is_dart_project { + return None; + } + + let dart_version = utils::exec_cmd("dart", &["--version"])?.stderr; + + let mut module = context.new_module("dart"); + let config: DartConfig = DartConfig::try_load(module.config); + + 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" => parse_dart_version(&dart_version).map(Ok), + _ => None, + }) + .parse(None) + }); + + module.set_segments(match parsed { + Ok(segments) => segments, + Err(error) => { + log::warn!("Error in module `dart`:\n{}", error); + return None; + } + }); + + Some(module) +} + +fn parse_dart_version(dart_version: &str) -> Option { + let version = dart_version + // split into ["Dart", "VM", "version:", "2.8.4", "(stable)", ...] + .split_whitespace() + // return "2.8.4" + .nth(3)?; + + Some(format!("v{}", version)) +} + +#[cfg(test)] +mod tests { + use super::parse_dart_version; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::{self, File}; + use std::io; + + #[test] + fn test_parse_dart_version() { + let input = "Dart VM version: 2.8.4 (stable)"; + assert_eq!(parse_dart_version(input), Some("v2.8.4".to_string())); + } + + #[test] + fn folder_without_dart_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let actual = render_module("dart", dir.path(), None); + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_dart_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.dart"))?.sync_all()?; + + let actual = render_module("dart", dir.path(), None); + let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_dart_tool_directory() -> io::Result<()> { + let dir = tempfile::tempdir()?; + fs::create_dir_all(dir.path().join(".dart_tool"))?; + + let actual = render_module("dart", dir.path(), None); + let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_pubspec_yaml_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("pubspec.yaml"))?.sync_all()?; + + let actual = render_module("dart", dir.path(), None); + let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_pubspec_yml_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("pubspec.yml"))?.sync_all()?; + + let actual = render_module("dart", dir.path(), None); + let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_pubspec_lock_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("pubspec.lock"))?.sync_all()?; + + let actual = render_module("dart", dir.path(), None); + let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + assert_eq!(expected, actual); + dir.close() + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 06ae96734..f2984f9e1 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -6,6 +6,7 @@ mod cmd_duration; mod conda; mod crystal; pub(crate) mod custom; +mod dart; mod directory; mod docker_context; mod dotnet; @@ -63,6 +64,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "cmake" => cmake::module(context), "cmd_duration" => cmd_duration::module(context), "conda" => conda::module(context), + "dart" => dart::module(context), "directory" => directory::module(context), "docker_context" => docker_context::module(context), "dotnet" => dotnet::module(context), @@ -119,6 +121,7 @@ pub fn description(module: &str) -> &'static str { "cmd_duration" => "How long the last command took to execute", "conda" => "The current conda environment, if $CONDA_DEFAULT_ENV is set", "crystal" => "The currently installed version of Crystal", + "dart" => "The currently installed version of Dart", "directory" => "The current working directory", "docker_context" => "The current docker context", "dotnet" => "The relevant version of the .NET Core SDK for the current directory", diff --git a/src/utils.rs b/src/utils.rs index 18cd34a13..34fafce60 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -49,6 +49,12 @@ Default target: x86_64-apple-macosx\n", ), stderr: String::default(), }), + "dart --version" => Some(CommandOutput { + stdout: String::default(), + stderr: String::from( + "Dart VM version: 2.8.4 (stable) (Wed Jun 3 12:26:04 2020 +0200) on \"macos_x64\"", + ), + }), "dummy_command" => Some(CommandOutput { stdout: String::from("stdout ok!\n"), stderr: String::from("stderr ok!\n"),