Merge branch 'master' into conditional-style

This commit is contained in:
Filip Bachul 2022-08-01 22:52:10 +02:00
commit 07fe2cc722
14 changed files with 276 additions and 6 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": [
@ -1732,6 +1752,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": {

9
Cargo.lock generated
View File

@ -282,9 +282,9 @@ dependencies = [
[[package]]
name = "clap"
version = "3.2.15"
version = "3.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44bbe24bbd31a185bc2c4f7c2abe80bea13a20d57ee4e55be70ac512bdc76417"
checksum = "a3dbbb6653e7c55cc8595ad3e1f7be8f32aba4eb7ff7f0fd1163d4f3d137c0a9"
dependencies = [
"atty",
"bitflags",
@ -1439,10 +1439,11 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pest"
version = "2.1.3"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
checksum = "69486e2b8c2d2aeb9762db7b4e00b0331156393555cff467f4163ff06821eef8"
dependencies = [
"thiserror",
"ucd-trie",
]

View File

@ -38,7 +38,7 @@ notify = ["notify-rust"]
[dependencies]
ansi_term = "0.12.1"
chrono = "0.4.19"
clap = { version = "=3.2.15", features = ["derive", "cargo", "unicode", "unstable-v4"] }
clap = { version = "=3.2.16", features = ["derive", "cargo", "unicode", "unstable-v4"] }
clap_complete = "3.2.3"
dirs-next = "2.0.0"
dunce = "1.0.2"
@ -54,7 +54,7 @@ once_cell = "1.13.0"
open = "3.0.2"
os_info = "3.4.0"
path-slash = "0.2.0"
pest = "2.1.3"
pest = "2.2.1"
pest_derive = "2.1.0"
quick-xml = "0.23.0"
rand = "0.8.5"

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(),