1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 13:47:38 +00:00

feat: Add configuration to change the character for non-zero sta… (#133)

Prompt can now switch characters in addition to switching character color. Add configuration options in so that users can do either, both, or neither.
This commit is contained in:
Kevin Song 2019-08-10 14:30:30 -07:00 committed by Matan Kushner
parent 994a865d4d
commit 39598ec691
4 changed files with 70 additions and 31 deletions

View File

@ -89,16 +89,18 @@ discharging_symbol = "💀"
The `character` module shows a character (usually an arrow) beside where the text
is entered in your terminal.
The character will be green if the status of your
last command had a successful status code (zero), and will be red if the last
command had an unsuccessful status code (non-zero).
The character will tell you whether the last command was successful or not. It
can do this in two ways: by changing color (red/green) or by changing its shape
(➜/✖). The latter will only be done if `use_symbol_for_status` is set to `true`.
### Options
| Variable | Default | Description |
| ---------- | ------- | ---------------------------------------------------- |
| `symbol` | `"➜"` | The symbol used before the text input in the prompt. |
| `disabled` | `false` | Disables the `character` module. |
| `symbol` | `"➜"` | The symbol used before the text input in the prompt. |
| `error_symbol` | `"✖"` | The symbol used before text input if the previous command failed. |
| `use_symbol_for_status` | `false` | Indicate error status by changing the symbol. |
| `disabled` | `false` | Disables the `character` module. |
### Example
@ -107,6 +109,8 @@ command had an unsuccessful status code (non-zero).
[character]
symbol = ""
error_symbol = "✗"
use_symbol_for_status = true
```
## Command Duration

View File

@ -44,7 +44,7 @@ impl<'a> Module<'a> {
let mut segment = Segment::new(name);
segment.set_style(self.style);
// Use the provided value unless overwritten by config
segment.set_value(self.config_value(name).unwrap_or(value));
segment.set_value(self.config_value_str(name).unwrap_or(value));
self.segments.push(segment);
self.segments.last_mut().unwrap()
@ -96,7 +96,7 @@ impl<'a> Module<'a> {
}
/// Get a module's config value as a string
fn config_value(&self, key: &str) -> Option<&str> {
pub fn config_value_str(&self, key: &str) -> Option<&str> {
self.config.and_then(|config| config.get_as_str(key))
}

View File

@ -10,17 +10,29 @@ use ansi_term::Color;
/// - If the exit-code was anything else, the arrow will be formatted with
/// `COLOR_FAILURE` (red by default)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const PROMPT_CHAR: &str = "";
const SUCCESS_CHAR: &str = "";
const FAILURE_CHAR: &str = "";
let color_success = Color::Green.bold();
let color_failure = Color::Red.bold();
let mut module = context.new_module("character")?;
module.get_prefix().set_value("");
let symbol = module.new_segment("symbol", PROMPT_CHAR);
let arguments = &context.arguments;
if arguments.value_of("status_code").unwrap_or("0") == "0" {
let use_symbol = module
.config_value_bool("use_symbol_for_status")
.unwrap_or(false);
let exit_success = arguments.value_of("status_code").unwrap_or("0") == "0";
/* If an error symbol is set in the config, use symbols to indicate
success/failure, in addition to color */
let symbol = if use_symbol && !exit_success {
module.new_segment("error_symbol", FAILURE_CHAR)
} else {
module.new_segment("symbol", SUCCESS_CHAR)
};
if exit_success {
symbol.set_style(color_success.bold());
} else {
symbol.set_style(color_failure.bold());

View File

@ -1,7 +1,7 @@
use ansi_term::Color;
use std::io;
use crate::common;
use crate::common::{self, TestCommand};
#[test]
fn char_module_success_status() -> io::Result<()> {
@ -26,26 +26,49 @@ fn char_module_success_status() -> io::Result<()> {
fn char_module_failure_status() -> io::Result<()> {
let expected = format!("{} ", Color::Red.bold().paint(""));
// Error status code 1
let output = common::render_module("character")
.arg("--status=1")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
let exit_values = ["1", "54321", "-5000"];
// Random non-zero status code
let output = common::render_module("character")
.arg("--status=54321")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
// Negative status code!?
let output = common::render_module("character")
.arg("--status=-5000")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
for status in exit_values.iter() {
let arg = format!("--status={}", status);
let output = common::render_module("character").arg(arg).output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
}
Ok(())
}
#[test]
fn char_module_symbolyes_status() -> io::Result<()> {
let expected_fail = format!("{} ", Color::Red.bold().paint(""));
let expected_success = format!("{} ", Color::Green.bold().paint(""));
let exit_values = ["1", "54321", "-5000"];
// Test failure values
for status in exit_values.iter() {
let arg = format!("--status={}", status);
let output = common::render_module("character")
.use_config(toml::toml! {
[character]
use_symbol_for_status = true
})
.arg(arg)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected_fail, actual);
}
// Test success
let output = common::render_module("character")
.use_config(toml::toml! {
[character]
use_symbol_for_status = true
})
.arg("--status=0")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected_success, actual);
Ok(())
}