Skip to content

Commit

Permalink
feat(guix_shell): Initial implementation (#4397)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Thierry Delafontaine committed Oct 25, 2022
1 parent c3cd499 commit d4bcc51
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/bracketed-segments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)\]'

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/nerd-font-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ symbol = " "
[golang]
symbol = ""

[guix_shell]
symbol = ""

[haskell]
symbol = ""

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/plain-text-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ symbol = "git "
[golang]
symbol = "go "

[guix_shell]
symbol = "guix "

[hg_branch]
symbol = "hg "

Expand Down
34 changes: 34 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ $elixir\
$elm\
$erlang\
$golang\
$guix_shell\
$haskell\
$helm\
$java\
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/configs/guix_shell.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"zig",
// ↑ Toolchain version modules ↑
"buf",
"guix_shell",
"nix_shell",
"conda",
"meson",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub const ALL_MODULES: &[&str] = &[
"git_state",
"git_status",
"golang",
"guix_shell",
"haskell",
"helm",
"hg_branch",
Expand Down
69 changes: 69 additions & 0 deletions src/modules/guix_shell.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit d4bcc51

Please sign in to comment.