Skip to content

Commit

Permalink
feat(line_break): add break_below_width option
Browse files Browse the repository at this point in the history
When specified, this option will change `line_break`'s behaviour to only
insert a line break when the width of the terminal is below the given
value.  This allows users to increase the vertical space used by their
prompt only when horizontal space is at a premium.

(This is particularly useful when a right prompt is configured, as a
sufficiently long left prompt may mean it is never displayed in smaller
terminals with `line_break` completely disabled.)
  • Loading branch information
OddBloke committed Feb 6, 2024
1 parent 0455eca commit 1bce0f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@
},
"line_break": {
"default": {
"break_below_width": null,
"disabled": false
},
"allOf": [
Expand Down Expand Up @@ -4197,6 +4198,15 @@
"disabled": {
"default": false,
"type": "boolean"
},
"break_below_width": {
"default": null,
"type": [
"integer",
"null"
],
"format": "uint",
"minimum": 0.0
}
},
"additionalProperties": false
Expand Down
7 changes: 4 additions & 3 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2670,9 +2670,10 @@ The `line_break` module separates the prompt into two lines.

### Options

| Option | Default | Description |
| ---------- | ------- | ------------------------------------------------------------------ |
| `disabled` | `false` | Disables the `line_break` module, making the prompt a single line. |
| Option | Default | Description |
| ------------------- | ------- | ------------------------------------------------------------------------------ |
| `disabled` | `false` | Disables the `line_break` module, making the prompt a single line. |
| `break_below_width` | | If specified, insert a line break only when your terminal is below this width. |

### Example

Expand Down
1 change: 1 addition & 0 deletions src/configs/line_break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ use serde::{Deserialize, Serialize};
#[serde(default)]
pub struct LineBreakConfig {
pub disabled: bool,
pub break_below_width: Option<usize>,
}
42 changes: 40 additions & 2 deletions src/modules/line_break.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use super::{Context, Module};
use crate::segment::Segment;
use crate::{config::ModuleConfig, configs::line_break::LineBreakConfig, segment::Segment};

/// Creates a module for the line break
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("line_break");
let config = LineBreakConfig::try_load(module.config);

module.set_segments(vec![Segment::LineTerm]);
let should_break = match config.break_below_width {
Some(break_below_width) => context.width < break_below_width,
None => true,
};
if should_break {
module.set_segments(vec![Segment::LineTerm]);
}

Some(module)
}
Expand All @@ -20,4 +27,35 @@ mod test {
let actual = ModuleRenderer::new("line_break").collect();
assert_eq!(expected, actual);
}

fn check_break_below_width_behaviour(
break_below_width: usize,
term_width: usize,
expected: Option<String>,
) {
let config = toml::toml! {
[line_break]
break_below_width = break_below_width
};
let renderer = ModuleRenderer::new("line_break")
.config(config)
.width(term_width);
let actual = renderer.collect();
assert_eq!(expected, actual);
}

#[test]
fn break_below_width_wide_terminal() {
check_break_below_width_behaviour(80, 80, None);
}

#[test]
fn break_below_width_narrow_terminal() {
check_break_below_width_behaviour(80, 79, Some(String::from("\n")));
}

#[test]
fn break_below_width_undetected_width() {
check_break_below_width_behaviour(80, 0, Some(String::from("\n")));
}
}

0 comments on commit 1bce0f4

Please sign in to comment.