Skip to content

Commit 5facd85

Browse files
hooksie1davidkna
andauthored
feat(nats): Add NATS Context (starship#5900)
--------- Co-authored-by: David Knaack <[email protected]>
1 parent 2221a88 commit 5facd85

File tree

10 files changed

+195
-9
lines changed

10 files changed

+195
-9
lines changed

.github/config-schema.json

+35-9
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,19 @@
10441044
}
10451045
]
10461046
},
1047+
"nats": {
1048+
"default": {
1049+
"disabled": true,
1050+
"format": "[$symbol($name )]($style)",
1051+
"style": "bold purple",
1052+
"symbol": "✉️ "
1053+
},
1054+
"allOf": [
1055+
{
1056+
"$ref": "#/definitions/NatsConfig"
1057+
}
1058+
]
1059+
},
10471060
"nim": {
10481061
"default": {
10491062
"detect_extensions": [
@@ -2082,14 +2095,12 @@
20822095
"type": "string"
20832096
},
20842097
"charging_symbol": {
2085-
"default": null,
20862098
"type": [
20872099
"string",
20882100
"null"
20892101
]
20902102
},
20912103
"discharging_symbol": {
2092-
"default": null,
20932104
"type": [
20942105
"string",
20952106
"null"
@@ -2760,14 +2771,12 @@
27602771
"type": "string"
27612772
},
27622773
"repo_root_style": {
2763-
"default": null,
27642774
"type": [
27652775
"string",
27662776
"null"
27672777
]
27682778
},
27692779
"before_repo_root_style": {
2770-
"default": null,
27712780
"type": [
27722781
"string",
27732782
"null"
@@ -4282,35 +4291,30 @@
42824291
"type": "string"
42834292
},
42844293
"user_pattern": {
4285-
"default": null,
42864294
"type": [
42874295
"string",
42884296
"null"
42894297
]
42904298
},
42914299
"symbol": {
4292-
"default": null,
42934300
"type": [
42944301
"string",
42954302
"null"
42964303
]
42974304
},
42984305
"style": {
4299-
"default": null,
43004306
"type": [
43014307
"string",
43024308
"null"
43034309
]
43044310
},
43054311
"context_alias": {
4306-
"default": null,
43074312
"type": [
43084313
"string",
43094314
"null"
43104315
]
43114316
},
43124317
"user_alias": {
4313-
"default": null,
43144318
"type": [
43154319
"string",
43164320
"null"
@@ -4467,6 +4471,28 @@
44674471
},
44684472
"additionalProperties": false
44694473
},
4474+
"NatsConfig": {
4475+
"type": "object",
4476+
"properties": {
4477+
"format": {
4478+
"default": "[$symbol($name )]($style)",
4479+
"type": "string"
4480+
},
4481+
"symbol": {
4482+
"default": "✉️ ",
4483+
"type": "string"
4484+
},
4485+
"style": {
4486+
"default": "bold purple",
4487+
"type": "string"
4488+
},
4489+
"disabled": {
4490+
"default": true,
4491+
"type": "boolean"
4492+
}
4493+
},
4494+
"additionalProperties": false
4495+
},
44704496
"NimConfig": {
44714497
"type": "object",
44724498
"properties": {

docs/config/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ $aws\
339339
$gcloud\
340340
$openstack\
341341
$azure\
342+
$nats\
342343
$direnv\
343344
$env_var\
344345
$crystal\
@@ -2937,6 +2938,35 @@ truncation_length = 4
29372938
truncation_symbol = ''
29382939
```
29392940

2941+
## NATS
2942+
2943+
The `nats` module shows the name of the current [NATS](https://nats.io) context.
2944+
2945+
### Options
2946+
2947+
| Option | Default | Description |
2948+
| ---------- | -------------------------- | ------------------------------------------------------------ |
2949+
| `symbol` | `'✉️ '` | The symbol used before the NATS context (defaults to empty). |
2950+
| `style` | `'bold purple'` | The style for the module. |
2951+
| `format` | `'[$symbol$name]($style)'` | The format for the module. |
2952+
| `disabled` | `false` | Disables the `nats` module. |
2953+
2954+
### Variables
2955+
2956+
| Variable | Example | Description |
2957+
| -------- | ----------- | ------------------------------------ |
2958+
| name | `localhost` | The name of the NATS context |
2959+
| symbol | | Mirrors the value of option `symbol` |
2960+
| style\* | | Mirrors the value of option `style` |
2961+
2962+
### Example
2963+
2964+
```toml
2965+
[nats]
2966+
format = '[$symbol]($style)'
2967+
style = 'bold purple'
2968+
```
2969+
29402970
## Nim
29412971

29422972
The `nim` module shows the currently installed version of [Nim](https://nim-lang.org/).

docs/public/presets/toml/plain-text-symbols.toml

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ symbol = "memory "
109109
[meson]
110110
symbol = "meson "
111111

112+
[nats]
113+
symbol = "nats "
114+
112115
[nim]
113116
symbol = "nim "
114117

src/configs/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub mod localip;
5555
pub mod lua;
5656
pub mod memory_usage;
5757
pub mod meson;
58+
pub mod nats;
5859
pub mod nim;
5960
pub mod nix_shell;
6061
pub mod nodejs;
@@ -218,6 +219,8 @@ pub struct FullConfig<'a> {
218219
#[serde(borrow)]
219220
meson: meson::MesonConfig<'a>,
220221
#[serde(borrow)]
222+
nats: nats::NatsConfig<'a>,
223+
#[serde(borrow)]
221224
nim: nim::NimConfig<'a>,
222225
#[serde(borrow)]
223226
nix_shell: nix_shell::NixShellConfig<'a>,

src/configs/nats.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Clone, Deserialize, Serialize)]
4+
#[cfg_attr(
5+
feature = "config-schema",
6+
derive(schemars::JsonSchema),
7+
schemars(deny_unknown_fields)
8+
)]
9+
#[serde(default)]
10+
pub struct NatsConfig<'a> {
11+
pub format: &'a str,
12+
pub symbol: &'a str,
13+
pub style: &'a str,
14+
pub disabled: bool,
15+
}
16+
17+
impl<'a> Default for NatsConfig<'a> {
18+
fn default() -> Self {
19+
NatsConfig {
20+
format: "[$symbol($name )]($style)",
21+
symbol: "✉️ ",
22+
style: "bold purple",
23+
disabled: true,
24+
}
25+
}
26+
}

src/configs/starship_root.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub const PROMPT_ORDER: &[&str] = &[
3737
"shlvl",
3838
"singularity",
3939
"kubernetes",
40+
"nats",
4041
"directory",
4142
"vcsh",
4243
"fossil_branch",

src/module.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub const ALL_MODULES: &[&str] = &[
6060
"lua",
6161
"memory_usage",
6262
"meson",
63+
"nats",
6364
"nim",
6465
"nix_shell",
6566
"nodejs",

src/modules/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ mod localip;
5252
mod lua;
5353
mod memory_usage;
5454
mod meson;
55+
mod nats;
5556
mod nim;
5657
mod nix_shell;
5758
mod nodejs;
@@ -162,6 +163,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
162163
"lua" => lua::module(context),
163164
"memory_usage" => memory_usage::module(context),
164165
"meson" => meson::module(context),
166+
"nats" => nats::module(context),
165167
"nim" => nim::module(context),
166168
"nix_shell" => nix_shell::module(context),
167169
"nodejs" => nodejs::module(context),
@@ -285,6 +287,7 @@ pub fn description(module: &str) -> &'static str {
285287
"meson" => {
286288
"The current Meson environment, if $MESON_DEVENV and $MESON_PROJECT_NAME are set"
287289
}
290+
"nats" => "The current NATS context",
288291
"nim" => "The currently installed version of Nim",
289292
"nix_shell" => "The nix-shell environment",
290293
"nodejs" => "The currently installed version of NodeJS",

src/modules/nats.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use super::{Context, Module, ModuleConfig};
2+
use serde_json as json;
3+
4+
use crate::configs::nats::NatsConfig;
5+
use crate::formatter::StringFormatter;
6+
7+
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
8+
let mut module = context.new_module("nats");
9+
let config = NatsConfig::try_load(module.config);
10+
11+
if config.disabled {
12+
return None;
13+
};
14+
15+
let ctx_str = context
16+
.exec_cmd("nats", &["context", "info", "--json"])?
17+
.stdout;
18+
let nats_context: json::Value = json::from_str(&ctx_str)
19+
.map_err(|e| {
20+
log::warn!("Error parsing nats context JSON: {}\n", e);
21+
drop(e);
22+
})
23+
.ok()?;
24+
25+
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
26+
formatter
27+
.map_meta(|var, _| match var {
28+
"symbol" => Some(config.symbol),
29+
_ => None,
30+
})
31+
.map_style(|variable| match variable {
32+
"style" => Some(Ok(config.style)),
33+
_ => None,
34+
})
35+
.map(|variable| match variable {
36+
"name" => Some(Ok(nats_context.get("name")?.as_str()?)),
37+
_ => None,
38+
})
39+
.parse(None, Some(context))
40+
});
41+
42+
module.set_segments(match parsed {
43+
Ok(segments) => segments,
44+
Err(error) => {
45+
log::warn!("Error in module `nats`:\n{}", error);
46+
return None;
47+
}
48+
});
49+
50+
Some(module)
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use nu_ansi_term::Color;
56+
use std::io;
57+
58+
use crate::test::ModuleRenderer;
59+
60+
#[test]
61+
fn show_context() -> io::Result<()> {
62+
let actual = ModuleRenderer::new("nats")
63+
.config(toml::toml! {
64+
[nats]
65+
format = "[$symbol$name](bold purple)"
66+
symbol = ""
67+
disabled = false
68+
})
69+
.collect();
70+
let expected = Some(format!("{}", Color::Purple.bold().paint("localhost")));
71+
assert_eq!(expected, actual);
72+
Ok(())
73+
}
74+
75+
#[test]
76+
fn test_with_symbol() -> io::Result<()> {
77+
let actual = ModuleRenderer::new("nats")
78+
.config(toml::toml! {
79+
[nats]
80+
format = "[$symbol$name](bold red)"
81+
symbol = "✉️ "
82+
disabled = false
83+
})
84+
.collect();
85+
let expected = Some(format!("{}", Color::Red.bold().paint("✉️ localhost")));
86+
assert_eq!(expected, actual);
87+
Ok(())
88+
}
89+
}

src/utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ Elixir 1.10 (compiled with Erlang/OTP 22)\n",
315315
stdout: String::from("LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/\n"),
316316
stderr: String::default(),
317317
}),
318+
"nats context info --json" => Some(CommandOutput{
319+
stdout: String::from("{\"name\":\"localhost\",\"url\":\"nats://localhost:4222\"}"),
320+
stderr: String::default(),
321+
}),
318322
"nim --version" => Some(CommandOutput {
319323
stdout: String::from(
320324
"\

0 commit comments

Comments
 (0)