summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/config/README.md14
-rw-r--r--src/module.rs4
-rw-r--r--src/modules/character.rs20
-rw-r--r--tests/testsuite/character.rs55
4 files changed, 66 insertions, 27 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 772fa6b41..d86ae3391 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -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
diff --git a/src/module.rs b/src/module.rs
index ade5d9e5d..4c5787573 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -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))
}
diff --git a/src/modules/character.rs b/src/modules/character.rs
index 6d6a4a1e1..7946cff49 100644
--- a/src/modules/character.rs
+++ b/src/modules/character.rs
@@ -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());
diff --git a/tests/testsuite/character.rs b/tests/testsuite/character.rs
index 21b284eae..4c9fcaf5f 100644
--- a/tests/testsuite/character.rs
+++ b/tests/testsuite/character.rs
@@ -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);
+ 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);
+ }
- // Negative status code!?
+ 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")
- .arg("--status=-5000")
+ .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, actual);
+ assert_eq!(expected_success, actual);
Ok(())
}