mirror of
https://github.com/starship/starship.git
synced 2024-11-23 00:33:16 +01:00
feat(dart): add Dart module (#1392)
This commit is contained in:
parent
feb4124cac
commit
84f049f836
@ -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
|
||||
|
@ -28,6 +28,9 @@ discharging_symbol = ""
|
||||
[conda]
|
||||
symbol = " "
|
||||
|
||||
[dart]
|
||||
symbol = " "
|
||||
|
||||
[docker]
|
||||
symbol = " "
|
||||
|
||||
|
22
src/configs/dart.rs
Normal file
22
src/configs/dart.rs
Normal file
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -27,6 +27,7 @@ pub const PROMPT_ORDER: &[&str] = &[
|
||||
// ↓ Toolchain version modules ↓
|
||||
// (Let's keep these sorted alphabetically)
|
||||
"cmake",
|
||||
"dart",
|
||||
"dotnet",
|
||||
"elixir",
|
||||
"elm",
|
||||
|
@ -15,6 +15,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"cmake",
|
||||
"cmd_duration",
|
||||
"conda",
|
||||
"dart",
|
||||
"directory",
|
||||
"docker_context",
|
||||
"dotnet",
|
||||
|
145
src/modules/dart.rs
Normal file
145
src/modules/dart.rs
Normal file
@ -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<Module<'a>> {
|
||||
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<String> {
|
||||
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()
|
||||
}
|
||||
}
|
@ -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<Module<'a>> {
|
||||
"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",
|
||||
|
@ -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"),
|
||||
|
Loading…
Reference in New Issue
Block a user