feat(guix_shell): Initial implementation (#4397)

* feat(guix_shell): Initial implementation (#3999)

* fix(guix_shell): Change guix nerd font icon to water buffalo emoji

* fix(guix_shell): Added guix_shell entries in preset files

* fix(guix_shell): Moved guix_shell config docs in to the correct place (alphabetically)
This commit is contained in:
Thierry Delafontaine 2022-10-25 07:44:04 +02:00 committed by GitHub
parent c3cd499a30
commit d4bcc519e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 181 additions and 0 deletions

View File

@ -620,6 +620,19 @@
}
]
},
"guix_shell": {
"default": {
"disabled": false,
"format": "via [$symbol]($style) ",
"style": "yellow bold",
"symbol": "🐃 "
},
"allOf": [
{
"$ref": "#/definitions/GuixShellConfig"
}
]
},
"haskell": {
"default": {
"detect_extensions": [
@ -3091,6 +3104,28 @@
},
"additionalProperties": false
},
"GuixShellConfig": {
"type": "object",
"properties": {
"format": {
"default": "via [$symbol]($style) ",
"type": "string"
},
"symbol": {
"default": "🐃 ",
"type": "string"
},
"style": {
"default": "yellow bold",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
},
"HaskellConfig": {
"type": "object",
"properties": {

View File

@ -58,6 +58,9 @@ format = '([\[$all_status$ahead_behind\]]($style))'
[golang]
format = '\[[$symbol($version)]($style)\]'
[guix_shell]
format = '\[[$symbol]($style)\]'
[haskell]
format = '\[[$symbol($version)]($style)\]'

View File

@ -31,6 +31,9 @@ symbol = " "
[golang]
symbol = " "
[guix_shell]
symbol = " "
[haskell]
symbol = " "

View File

@ -64,6 +64,9 @@ symbol = "git "
[golang]
symbol = "go "
[guix_shell]
symbol = "guix "
[hg_branch]
symbol = "hg "

View File

@ -250,6 +250,7 @@ $elixir\
$elm\
$erlang\
$golang\
$guix_shell\
$haskell\
$helm\
$java\
@ -1842,6 +1843,39 @@ By default the module will be shown if any of the following conditions are met:
format = "via [🏎💨 $version](bold cyan) "
```
## Guix-shell
The `guix_shell` module shows the [guix-shell](https://guix.gnu.org/manual/devel/en/html_node/Invoking-guix-shell.html) environment.
The module will be shown when inside a guix-shell environment.
### Options
| Option | Default | Description |
| ---------- | -------------------------- | ------------------------------------------------------ |
| `format` | `'via [$symbol]($style) '` | The format for the module. |
| `symbol` | `"🐃 "` | A format string representing the symbol of guix-shell. |
| `style` | `"yellow bold"` | The style for the module. |
| `disabled` | `false` | Disables the `guix_shell` module. |
### Variables
| Variable | Example | Description |
| -------- | ------- | ------------------------------------ |
| 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
[guix_shell]
disabled = true
format = 'via [🐂](yellow bold) '
```
## Haskell
The `haskell` module finds the current selected GHC version and/or the selected Stack snapshot.

26
src/configs/guix_shell.rs Normal file
View File

@ -0,0 +1,26 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct GuixShellConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
}
impl<'a> Default for GuixShellConfig<'a> {
fn default() -> Self {
GuixShellConfig {
format: "via [$symbol]($style) ",
symbol: "🐃 ",
style: "yellow bold",
disabled: false,
}
}
}

View File

@ -33,6 +33,7 @@ pub mod git_metrics;
pub mod git_state;
pub mod git_status;
pub mod go;
pub mod guix_shell;
pub mod haskell;
pub mod helm;
pub mod hg_branch;
@ -161,6 +162,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
golang: go::GoConfig<'a>,
#[serde(borrow)]
guix_shell: guix_shell::GuixShellConfig<'a>,
#[serde(borrow)]
haskell: haskell::HaskellConfig<'a>,
#[serde(borrow)]
helm: helm::HelmConfig<'a>,

View File

@ -86,6 +86,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"zig",
// ↑ Toolchain version modules ↑
"buf",
"guix_shell",
"nix_shell",
"conda",
"meson",

View File

@ -41,6 +41,7 @@ pub const ALL_MODULES: &[&str] = &[
"git_state",
"git_status",
"golang",
"guix_shell",
"haskell",
"helm",
"hg_branch",

69
src/modules/guix_shell.rs Normal file
View File

@ -0,0 +1,69 @@
use super::{Context, Module, ModuleConfig};
use crate::configs::guix_shell::GuixShellConfig;
use crate::formatter::StringFormatter;
/// Creates a module showing if inside a guix-shell
///
/// The module will use the `$GUIX_ENVIRONMENT` environment variable to determine if it's
/// inside a guix-shell and the name of it.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("guix_shell");
let config: GuixShellConfig = GuixShellConfig::try_load(module.config);
let is_guix_shell = context.get_env("GUIX_ENVIRONMENT").is_some();
if !is_guix_shell {
return None;
};
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.parse(None, Some(context))
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `guix_shell`:\n{}", error);
return None;
}
});
Some(module)
}
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use nu_ansi_term::Color;
#[test]
fn no_env_variables() {
let actual = ModuleRenderer::new("guix_shell").collect();
let expected = None;
assert_eq!(expected, actual);
}
#[test]
fn env_variables() {
let actual = ModuleRenderer::new("guix_shell")
.env(
"GUIX_ENVIRONMENT",
"/gnu/store/7vmfs4khf4fllsh83kqkxssbw3437qsh-profile",
)
.collect();
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐃 ")));
assert_eq!(expected, actual);
}
}

View File

@ -30,6 +30,7 @@ mod git_metrics;
mod git_state;
mod git_status;
mod golang;
mod guix_shell;
mod haskell;
mod helm;
mod hg_branch;
@ -126,6 +127,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"git_state" => git_state::module(context),
"git_status" => git_status::module(context),
"golang" => golang::module(context),
"guix_shell" => guix_shell::module(context),
"haskell" => haskell::module(context),
"helm" => helm::module(context),
"hg_branch" => hg_branch::module(context),
@ -233,6 +235,7 @@ pub fn description(module: &str) -> &'static str {
"git_state" => "The current git operation, and it's progress",
"git_status" => "Symbol representing the state of the repo",
"golang" => "The currently installed version of Golang",
"guix_shell" => "The guix-shell environment",
"haskell" => "The selected version of the Haskell toolchain",
"helm" => "The currently installed version of Helm",
"hg_branch" => "The active branch of the repo in your current directory",