Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(line_break): add break_below_width option #5760

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@
},
"line_break": {
"default": {
"break_below_width": null,
"disabled": false
},
"allOf": [
Expand Down Expand Up @@ -4325,6 +4326,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 @@ -2724,9 +2724,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")));
}
}