1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 13:47:38 +00:00

feat: add bun module (#4187)

This commit is contained in:
David Knaack 2022-08-01 12:59:36 +02:00 committed by GitHub
parent 60c81f3d03
commit 85692d1bf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 269 additions and 0 deletions

View File

@ -82,6 +82,26 @@
}
]
},
"bun": {
"default": {
"detect_extensions": [],
"detect_files": [
"bun.lockb",
"bunfig.toml"
],
"detect_folders": [],
"disabled": false,
"format": "via [$symbol($version )]($style)",
"style": "bold red",
"symbol": "🍞 ",
"version_format": "v${raw}"
},
"allOf": [
{
"$ref": "#/definitions/BunConfig"
}
]
},
"c": {
"default": {
"commands": [
@ -1725,6 +1745,55 @@
}
}
},
"BunConfig": {
"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 red",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
},
"detect_extensions": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"detect_files": {
"default": [
"bun.lockb",
"bunfig.toml"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_folders": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
}
},
"CConfig": {
"type": "object",
"properties": {

View File

@ -1,6 +1,9 @@
[aws]
format = '\[[$symbol($profile)(\($region\))(\[$duration\])]($style)\]'
[bun]
format = '\[[$symbol($version)]($style)\]'
[c]
format = '\[[$symbol($version(-$name))]($style)\]'

View File

@ -1,3 +1,6 @@
[bun]
format = "via [$symbol]($style)"
[buf]
format = "via [$symbol]($style)"

View File

@ -16,6 +16,9 @@ deleted = "x"
[aws]
symbol = "aws "
[bun]
symbol = "bun "
[c]
symbol = "C "

View File

@ -516,6 +516,46 @@ The `buf` module shows the currently installed version of [Buf](https://buf.buil
symbol = "🦬 "
```
## Bun
The `bun` module shows the currently installed version of the [bun](https://bun.sh) JavaScript runtime.
By default the module will be shown if any of the following conditions are met:
- The current directory contains a `bun.lockb` file
- The current directory contains a `bunfig.toml` file
### 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` | `"🍞 "` | A format string representing the symbol of Node.js. |
| `detect_extensions` | `[]` | Which extensions should trigger this module. |
| `detect_files` | `["bun.lockb", "bunfig.toml"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `bun` module. |
### Variables
| Variable | Example | Description |
| -------- | -------- | ------------------------------------ |
| version | `v0.1.4` | The version of `bun` |
| 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
[bun]
format = "via [🍔 $version](bold green) "
```
## C
The `c` module shows some information about your C compiler. By default

30
src/configs/bun.rs Normal file
View File

@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
#[serde(default)]
pub struct BunConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}
impl<'a> Default for BunConfig<'a> {
fn default() -> Self {
BunConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "🍞 ",
style: "bold red",
disabled: false,
detect_extensions: vec![],
detect_files: vec!["bun.lockb", "bunfig.toml"],
detect_folders: vec![],
}
}
}

View File

@ -5,6 +5,7 @@ pub mod aws;
pub mod azure;
pub mod battery;
pub mod buf;
pub mod bun;
pub mod c;
pub mod character;
pub mod cmake;
@ -100,6 +101,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
buf: buf::BufConfig<'a>,
#[serde(borrow)]
bun: bun::BunConfig<'a>,
#[serde(borrow)]
c: c::CConfig<'a>,
#[serde(borrow)]
character: character::CharacterConfig<'a>,

View File

@ -36,6 +36,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"package",
// ↓ Toolchain version modules ↓
// (Let's keep these sorted alphabetically)
"bun",
"c",
"cmake",
"cobol",

View File

@ -14,6 +14,7 @@ pub const ALL_MODULES: &[&str] = &[
#[cfg(feature = "battery")]
"battery",
"buf",
"bun",
"c",
"character",
"cmake",

109
src/modules/bun.rs Normal file
View File

@ -0,0 +1,109 @@
use super::{Context, Module, ModuleConfig};
use crate::configs::bun::BunConfig;
use crate::formatter::StringFormatter;
use crate::formatter::VersionFormatter;
use crate::utils::get_command_string_output;
/// Creates a module with the current Bun version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("bun");
let config = BunConfig::try_load(module.config);
let is_bun_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_bun_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 bun_version = get_bun_version(context)?;
VersionFormatter::format_module_version(
module.get_name(),
&bun_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 `bun`:\n{}", error);
return None;
}
});
Some(module)
}
fn get_bun_version(context: &Context) -> Option<String> {
context
.exec_cmd("bun", &["--version"])
.map(get_command_string_output)
.map(parse_bun_version)
}
fn parse_bun_version(bun_version: String) -> String {
bun_version.trim_end().to_string()
}
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use ansi_term::Color;
use std::fs::File;
use std::io;
#[test]
fn folder_without_bun_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("bun").path(dir.path()).collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_bun_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("bun.lockb"))?.sync_all()?;
let actual = ModuleRenderer::new("bun").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🍞 v0.1.4 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn no_bun_installed() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("bun.lockb"))?.sync_all()?;
let actual = ModuleRenderer::new("bun")
.path(dir.path())
.cmd("bun --version", None)
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🍞 ")));
assert_eq!(expected, actual);
dir.close()
}
}

View File

@ -2,6 +2,7 @@
mod aws;
mod azure;
mod buf;
mod bun;
mod c;
mod character;
mod cmake;
@ -97,6 +98,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
#[cfg(feature = "battery")]
"battery" => battery::module(context),
"buf" => buf::module(context),
"bun" => bun::module(context),
"c" => c::module(context),
"character" => character::module(context),
"cmake" => cmake::module(context),
@ -198,6 +200,7 @@ pub fn description(module: &str) -> &'static str {
"azure" => "The current Azure subscription",
"battery" => "The current charge of the device's battery and its current charging status",
"buf" => "The currently installed version of the Buf CLI",
"bun" => "The currently installed version of the Bun",
"c" => "Your C compiler type",
"character" => {
"A character (usually an arrow) beside where the text is entered in your terminal"

View File

@ -129,6 +129,10 @@ pub fn mock_cmd<T: AsRef<OsStr> + Debug, U: AsRef<OsStr> + Debug>(
) -> Option<Option<CommandOutput>> {
let command = display_command(&cmd, args);
let out = match command.as_str() {
"bun --version"=> Some(CommandOutput {
stdout: String::from("0.1.4\n"),
stderr: String::default(),
}),
"buf --version" => Some(CommandOutput {
stdout: String::from("1.0.0"),
stderr: String::default(),