1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

feat(fennel): add fennel module (#4717)

This commit is contained in:
fold-squirrel 2022-12-21 18:53:53 +02:00 committed by GitHub
parent c79e1d2be2
commit e93dbf8630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 273 additions and 0 deletions

View File

@ -473,6 +473,25 @@
} }
] ]
}, },
"fennel": {
"default": {
"detect_extentions": [
"fnl"
],
"detect_files": [],
"detect_folders": [],
"disabled": true,
"format": "via [$symbol($version )]($style)",
"style": "bold green",
"symbol": "🧅 ",
"version_format": "v${raw}"
},
"allOf": [
{
"$ref": "#/definitions/FennelConfig"
}
]
},
"fill": { "fill": {
"default": { "default": {
"disabled": false, "disabled": false,
@ -2867,6 +2886,55 @@
}, },
"additionalProperties": false "additionalProperties": false
}, },
"FennelConfig": {
"type": "object",
"properties": {
"format": {
"default": "via [$symbol($version )]($style)",
"type": "string"
},
"version_format": {
"default": "v${raw}",
"type": "string"
},
"symbol": {
"default": "🧅 ",
"type": "string"
},
"style": {
"default": "bold green",
"type": "string"
},
"disabled": {
"default": true,
"type": "boolean"
},
"detect_extentions": {
"default": [
"fnl"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_files": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"detect_folders": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"FillConfig": { "FillConfig": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -46,6 +46,9 @@ format = '\[[$symbol($version)]($style)\]'
[erlang] [erlang]
format = '\[[$symbol($version)]($style)\]' format = '\[[$symbol($version)]($style)\]'
[fennel]
format = '\[[$symbol($version)]($style)\]'
[gcloud] [gcloud]
format = '\[[$symbol$account(@$domain)(\($region\))]($style)\]' format = '\[[$symbol$account(@$domain)(\($region\))]($style)\]'

View File

@ -37,6 +37,9 @@ format = '(via [$symbol($version )]($style))'
[erlang] [erlang]
format = '(via [$symbol($version )]($style))' format = '(via [$symbol($version )]($style))'
[fennel]
format = '(via [$symbol($version )]($style))'
[golang] [golang]
format = '(via [$symbol($version )]($style))' format = '(via [$symbol($version )]($style))'

View File

@ -34,6 +34,9 @@ format = 'via [$symbol]($style)'
[erlang] [erlang]
format = 'via [$symbol]($style)' format = 'via [$symbol]($style)'
[fennel]
format = 'via [$symbol]($style)'
[golang] [golang]
format = 'via [$symbol]($style)' format = 'via [$symbol]($style)'

View File

@ -58,6 +58,9 @@ symbol = "exs "
[elm] [elm]
symbol = "elm " symbol = "elm "
[fennel]
symbol = "fnl "
[git_branch] [git_branch]
symbol = "git " symbol = "git "

View File

@ -283,6 +283,7 @@ $dotnet\
$elixir\ $elixir\
$elm\ $elm\
$erlang\ $erlang\
$fennel\
$golang\ $golang\
$guix_shell\ $guix_shell\
$haskell\ $haskell\
@ -1475,6 +1476,45 @@ By default the module will be shown if any of the following conditions are met:
format = 'via [e $version](bold red) ' format = 'via [e $version](bold red) '
``` ```
## Fennel
The `fennel` module shows the currently installed version of [Fennel](https://fennel-lang.org).
By default the module will be shown if any of the following conditions are met:
- The current directory contains a file with the `.fnl` extension
### Options
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------------------------- |
| `format` | `'via [$symbol($version )]($style)'` | The format for the module. |
| `version_format` | `'v${raw}'` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `'🧅 '` | The symbol used before displaying the version of fennel. |
| `style` | `'bold green'` | The style for the module. |
| `detect_extensions` | `[fnl]` | Which extensions should trigger this module. |
| `detect_files` | `[]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this modules. |
| `disabled` | `false` | Disables the `fennel` module. |
### Variables
| Variable | Example | Description |
| -------- | -------- | ------------------------------------ |
| version | `v1.2.1` | The version of `fennel` |
| 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
[fennel]
symbol = '⫰ '
```
## Fill ## Fill
The `fill` module fills any extra space on the line with a symbol. If multiple `fill` modules are The `fill` module fills any extra space on the line with a symbol. If multiple `fill` modules are

34
src/configs/fennel.rs Normal file
View File

@ -0,0 +1,34 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct FennelConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extentions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}
impl<'a> Default for FennelConfig<'a> {
fn default() -> Self {
FennelConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "🧅 ",
style: "bold green",
disabled: true,
detect_extentions: vec!["fnl"],
detect_files: vec![],
detect_folders: vec![],
}
}
}

View File

@ -25,6 +25,7 @@ pub mod elixir;
pub mod elm; pub mod elm;
pub mod env_var; pub mod env_var;
pub mod erlang; pub mod erlang;
pub mod fennel;
pub mod fill; pub mod fill;
pub mod gcloud; pub mod gcloud;
pub mod git_branch; pub mod git_branch;
@ -149,6 +150,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)] #[serde(borrow)]
erlang: erlang::ErlangConfig<'a>, erlang: erlang::ErlangConfig<'a>,
#[serde(borrow)] #[serde(borrow)]
fennel: fennel::FennelConfig<'a>,
#[serde(borrow)]
fill: fill::FillConfig<'a>, fill: fill::FillConfig<'a>,
#[serde(borrow)] #[serde(borrow)]
gcloud: gcloud::GcloudConfig<'a>, gcloud: gcloud::GcloudConfig<'a>,

View File

@ -57,6 +57,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"elixir", "elixir",
"elm", "elm",
"erlang", "erlang",
"fennel",
"golang", "golang",
"gradle", "gradle",
"haskell", "haskell",

View File

@ -33,6 +33,7 @@ pub const ALL_MODULES: &[&str] = &[
"elm", "elm",
"env_var", "env_var",
"erlang", "erlang",
"fennel",
"fill", "fill",
"gcloud", "gcloud",
"git_branch", "git_branch",

107
src/modules/fennel.rs Normal file
View File

@ -0,0 +1,107 @@
use super::{Context, Module, ModuleConfig};
use crate::configs::fennel::FennelConfig;
use crate::formatter::{StringFormatter, VersionFormatter};
use crate::utils::get_command_string_output;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("fennel");
let config = FennelConfig::try_load(module.config);
let is_fnl_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extentions)
.set_folders(&config.detect_folders)
.is_match();
if !is_fnl_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 fennel_version_string =
get_command_string_output(context.exec_cmd("fennel", &["--version"])?);
let fennel_version = parse_fennel_version(&fennel_version_string)?;
VersionFormatter::format_module_version(
module.get_name(),
&fennel_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 `fennel`:\n{}", error);
return None;
}
});
Some(module)
}
fn parse_fennel_version(fennel_version: &str) -> Option<String> {
// fennel -v output looks like this:
// Fennel 1.2.1 on PUC Lua 5.4
let version = fennel_version
// split into ["Fennel", "1.2.1", "on", ...]
.split_whitespace()
// take "1.2.1"
.nth(1)?;
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 folder_without_fennel_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("fennel").path(dir.path()).collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_fennel_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("man.fnl"))?.sync_all()?;
let actual = ModuleRenderer::new("fennel").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Green.bold().paint("🧅 v1.2.1 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn test_parse_fennel_version() {
let fennel_input = "Fennel 1.2.1 on PUC Lua 5.4";
assert_eq!(
parse_fennel_version(fennel_input),
Some("1.2.1".to_string())
);
}
}

View File

@ -22,6 +22,7 @@ mod elixir;
mod elm; mod elm;
mod env_var; mod env_var;
mod erlang; mod erlang;
mod fennel;
mod fill; mod fill;
mod gcloud; mod gcloud;
mod git_branch; mod git_branch;
@ -121,6 +122,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"elixir" => elixir::module(context), "elixir" => elixir::module(context),
"elm" => elm::module(context), "elm" => elm::module(context),
"erlang" => erlang::module(context), "erlang" => erlang::module(context),
"fennel" => fennel::module(context),
"env_var" => env_var::module(context), "env_var" => env_var::module(context),
"fill" => fill::module(context), "fill" => fill::module(context),
"gcloud" => gcloud::module(context), "gcloud" => gcloud::module(context),
@ -233,6 +235,7 @@ pub fn description(module: &str) -> &'static str {
"elm" => "The currently installed version of Elm", "elm" => "The currently installed version of Elm",
"env_var" => "Displays the current value of a selected environment variable", "env_var" => "Displays the current value of a selected environment variable",
"erlang" => "Current OTP version", "erlang" => "Current OTP version",
"fennel" => "The currently installed version of Fennel",
"fill" => "Fills the remaining space on the line with a pad string", "fill" => "Fills the remaining space on the line with a pad string",
"gcloud" => "The current GCP client configuration", "gcloud" => "The current GCP client configuration",
"git_branch" => "The active branch of the repo in your current directory", "git_branch" => "The active branch of the repo in your current directory",

View File

@ -245,6 +245,10 @@ Elixir 1.10 (compiled with Erlang/OTP 22)\n",
stdout: String::from("0.19.1\n"), stdout: String::from("0.19.1\n"),
stderr: String::default(), stderr: String::default(),
}), }),
"fennel --version" => Some(CommandOutput {
stdout: String::from("Fennel 1.2.1 on PUC Lua 5.4\n"),
stderr: String::default(),
}),
"go version" => Some(CommandOutput { "go version" => Some(CommandOutput {
stdout: String::from("go version go1.12.1 linux/amd64\n"), stdout: String::from("go version go1.12.1 linux/amd64\n"),
stderr: String::default(), stderr: String::default(),