1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 21:57:41 +00:00

feat: create shell module (#2329)

* Add the shell module

This module allows to quickly identify which shell is currently used, in case someone frequently switches between them.

* Updated documentation with shell module.

Co-authored-by: mro <mro@fedorabox.localdomain>
This commit is contained in:
Mikołaj Rosowski 2021-02-20 14:40:49 +00:00 committed by GitHub
parent d52edaa661
commit 37d3425d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 316 additions and 0 deletions

View File

@ -236,6 +236,7 @@ $jobs\
$battery\
$time\
$status\
$shell\
$character"""
```
@ -2217,6 +2218,46 @@ The module will be shown if any of the following conditions are met:
format = "via [⚙️ $version](red bold)"
```
## Shell
The `shell` module shows an indicator for currently used shell.
::: tip
This module is disabled by default.
To enable it, set `disabled` to `false` in your configuration file.
:::
### Options
| Option | Default | Description |
| ---------------------- | ------------- | --------------------------------------------- |
| `bash_indicator` | `bsh` | A format string used to represent bash. |
| `fish_indicator` | `fsh` | A format string used to represent fish. |
| `zsh_indicator` | `zsh` | A format string used to represent zsh. |
| `powershell_indicator` | `psh` | A format string used to represent powershell. |
| `ion_indicator` | `ion` | A format string used to represent ion. |
| `elvish_indicator` | `esh` | A format string used to represent elvish. |
| `format` | `$indicator ` | The format for the module. |
| `disabled` | `true` | Disables the `shell` module. |
### Variables
| Variable | Default | Description |
| ----------- | ------- | ---------------------------------------------------------- |
| indicator | | Mirrors the value of `indicator` for currently used shell. |
### Examples
```toml
# ~/.config/starship.toml
[shell]
fish_indicator = ""
powershell_indicator = "_"
disabled = false
```
## SHLVL
The `shlvl` module shows the current SHLVL ("shell level") environment variable, if it is

View File

@ -42,6 +42,7 @@ pub mod purescript;
pub mod python;
pub mod ruby;
pub mod rust;
pub mod shell;
pub mod shlvl;
pub mod singularity;
mod starship_root;

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

@ -0,0 +1,30 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct ShellConfig<'a> {
pub format: &'a str,
pub bash_indicator: &'a str,
pub fish_indicator: &'a str,
pub zsh_indicator: &'a str,
pub powershell_indicator: &'a str,
pub ion_indicator: &'a str,
pub elvish_indicator: &'a str,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for ShellConfig<'a> {
fn new() -> Self {
ShellConfig {
format: "$indicator ",
bash_indicator: "bsh",
fish_indicator: "fsh",
zsh_indicator: "zsh",
powershell_indicator: "psh",
ion_indicator: "ion",
elvish_indicator: "esh",
disabled: true,
}
}
}

View File

@ -71,6 +71,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"battery",
"time",
"status",
"shell",
"character",
];

View File

@ -56,6 +56,7 @@ pub const ALL_MODULES: &[&str] = &[
"php",
"swift",
"terraform",
"shell",
"shlvl",
"singularity",
"status",

View File

@ -43,6 +43,7 @@ mod purescript;
mod python;
mod ruby;
mod rust;
mod shell;
mod shlvl;
mod singularity;
mod status;
@ -113,6 +114,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"python" => python::module(context),
"ruby" => ruby::module(context),
"rust" => rust::module(context),
"shell" => shell::module(context),
"shlvl" => shlvl::module(context),
"singularity" => singularity::module(context),
"swift" => swift::module(context),
@ -191,6 +193,7 @@ pub fn description(module: &str) -> &'static str {
"ruby" => "The currently installed version of Ruby",
"rust" => "The currently installed version of Rust",
"swift" => "The currently installed version of Swift",
"shell" => "The currently used shell indicator",
"shlvl" => "The current value of SHLVL",
"status" => "The status of the last command",
"terraform" => "The currently selected terraform workspace and version",

239
src/modules/shell.rs Normal file
View File

@ -0,0 +1,239 @@
use super::{Context, Module, RootModuleConfig, Shell};
use crate::configs::shell::ShellConfig;
use crate::formatter::StringFormatter;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("shell");
let config: ShellConfig = ShellConfig::try_load(module.config);
if config.disabled {
return None;
}
let shell = context.shell;
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"indicator" => match shell {
Shell::Bash => Some(config.bash_indicator),
Shell::Fish => Some(config.fish_indicator),
Shell::Zsh => Some(config.zsh_indicator),
Shell::PowerShell => Some(config.powershell_indicator),
Shell::Ion => Some(config.ion_indicator),
Shell::Elvish => Some(config.elvish_indicator),
Shell::Unknown => None,
},
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `shell`: \n{}", error);
return None;
}
});
Some(module)
}
#[cfg(test)]
mod tests {
use crate::context::Shell;
use crate::test::ModuleRenderer;
use ansi_term::Color;
#[test]
fn test_none_if_disabled() {
let expected = None;
let actual = ModuleRenderer::new("shell").shell(Shell::Bash).collect();
assert_eq!(expected, actual);
}
#[test]
fn test_none_if_unknown_shell() {
let expected = None;
let actual = ModuleRenderer::new("shell").shell(Shell::Unknown).collect();
assert_eq!(expected, actual);
}
#[test]
fn test_bash_default_format() {
let expected = Some(format!("{} ", "bsh"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Bash)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_bash_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("bash")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Bash)
.config(toml::toml! {
[shell]
bash_indicator = "[bash](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_fish_default_format() {
let expected = Some(format!("{} ", "fsh"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Fish)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_fish_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("fish")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Fish)
.config(toml::toml! {
[shell]
fish_indicator = "[fish](cyan bold)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_zsh_default_format() {
let expected = Some(format!("{} ", "zsh"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Zsh)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_zsh_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("zsh")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Bash)
.config(toml::toml! {
[shell]
bash_indicator = "[zsh](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_powershell_default_format() {
let expected = Some(format!("{} ", "psh"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::PowerShell)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_powershell_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("powershell")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::PowerShell)
.config(toml::toml! {
[shell]
powershell_indicator = "[powershell](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_ion_default_format() {
let expected = Some(format!("{} ", "ion"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Ion)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_ion_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("ion")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Ion)
.config(toml::toml! {
[shell]
ion_indicator = "[ion](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_elvish_default_format() {
let expected = Some(format!("{} ", "esh"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Elvish)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_elvish_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("elvish")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Elvish)
.config(toml::toml! {
[shell]
elvish_indicator = "[elvish](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
}