summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Otto <th1000s@posteo.net>2021-04-19 22:33:50 +0200
committerThomas Otto <th1000s@posteo.net>2021-04-19 22:53:11 +0200
commitf47d040702c592f1bb6def5a8240e540dcb9184b (patch)
tree5c3b43b3587bf45cbffe9ea8d3db891f8aa8bec5
parent730843c2f63ad36892d59b260e93cc3d4fe4f52b (diff)
wrap-max-lines to 1, so 0 can mean no limit
-rw-r--r--src/config.rs6
-rw-r--r--src/features/side_by_side.rs12
-rw-r--r--src/features/side_by_side_wrap.rs24
3 files changed, 24 insertions, 18 deletions
diff --git a/src/config.rs b/src/config.rs
index 9404c5e4..88538f29 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -227,7 +227,11 @@ impl From<cli::Opt> for Config {
line_buffer_size: opt.line_buffer_size,
max_line_distance: opt.max_line_distance,
max_line_distance_for_naively_paired_lines,
- max_line_length: opt.max_line_length,
+ max_line_length: if opt.side_by_side_wrap_max_lines == 0 {
+ 0
+ } else {
+ opt.max_line_length
+ },
minus_emph_style,
minus_empty_line_marker_style,
minus_file: opt.minus_file,
diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs
index ea095bf2..a427ccf4 100644
--- a/src/features/side_by_side.rs
+++ b/src/features/side_by_side.rs
@@ -534,7 +534,7 @@ pub mod tests {
let config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"40",
]);
@@ -550,7 +550,7 @@ pub mod tests {
let mut config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"28",
]);
@@ -567,7 +567,7 @@ pub mod tests {
let config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"40",
]);
@@ -586,7 +586,7 @@ pub mod tests {
let mut config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"30",
]);
@@ -603,7 +603,7 @@ pub mod tests {
let mut config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"32",
]);
@@ -620,7 +620,7 @@ pub mod tests {
let config = make_config_from_args(&[
"--side-by-side",
"--side-by-side-wrap-max-lines",
- "0",
+ "1",
"--width",
"40",
]);
diff --git a/src/features/side_by_side_wrap.rs b/src/features/side_by_side_wrap.rs
index 20891e8d..7839207f 100644
--- a/src/features/side_by_side_wrap.rs
+++ b/src/features/side_by_side_wrap.rs
@@ -58,14 +58,10 @@ where
let max_len = line_width + LINEPREFIX.len();
- // Stay defensive just in case: guard against infinite loops.
- let mut n = max_len * wrap_config.max_lines * 2;
-
let mut curr_line = Vec::new();
let mut curr_len = 0;
- // Determine the background (diff) and color (syntax) of
- // an inserted symbol.
+ // Determine the background (diff) and color (syntax) of an inserted symbol.
let symbol_style = match inline_hint_style {
Some(style) => *style,
None => *fill_style,
@@ -73,17 +69,23 @@ where
let mut stack = line.into_iter().rev().collect::<Vec<_>>();
- while !stack.is_empty()
- && result.len() + 1 < wrap_config.max_lines
- && max_len > LINEPREFIX.len()
- && n > 0
- {
- n -= 1;
+ let line_limit_reached = |result: &Vec<_>| {
+ // If only the wrap symbol and no extra text fits then wrapping is not possible.
+ let max_lines = if line_width > 1 {
+ wrap_config.max_lines
+ } else {
+ 1
+ };
+ max_lines > 0 && result.len() + 1 >= max_lines
+ };
+
+ while !stack.is_empty() && !line_limit_reached(&result) && max_len > LINEPREFIX.len() {
let (style, text, graphemes) = stack
.pop()
.map(|(style, text)| (style, text, text.grapheme_indices(true).collect::<Vec<_>>()))
.unwrap();
+
let new_sum = curr_len + graphemes.len();
let must_split = if new_sum < max_len {