Allow RHS to be arbitrarily long in a pager

If stdout is not a TTY, assume it's a pager and don't truncate the
RHS.
ida_star
Wilfred Hughes 2021-08-08 17:18:19 +07:00
parent 857cb77d8f
commit 3ae0b7df84
5 changed files with 31 additions and 3 deletions

@ -9,6 +9,9 @@ blank lines between them to make it clearer.
Fixed an issue where screen width was not shared evenly by LHS and
RHS.
Side-by-side display will now use the full width of the screen when
using a pager (i.e. if stdout is a not a TTY).
### Parsing
Fixed handling of `->` in Rust.

1
Cargo.lock generated

@ -89,6 +89,7 @@ checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
name = "difftastic"
version = "0.7.0"
dependencies = [
"atty",
"clap",
"colored",
"diff",

@ -21,6 +21,7 @@ rust-embed = "5.9.0"
rustc-hash = "1.1.0"
strsim = "0.10.0"
lazy_static = "1.4.0"
atty = "0.2.14"
[dev-dependencies]
pretty_assertions = "0.6.1"

@ -433,6 +433,23 @@ pub fn enforce_length(s: &str, line_length: usize) -> String {
result
}
/// Truncate any lines in `s` that are longer than `line_length`.
pub fn enforce_max_length(s: &str, line_length: usize) -> String {
let mut result = String::with_capacity(s.len());
for line in s.lines() {
// TODO: use length in chars not bytes.
if line.len() > line_length {
// Truncate.
result.push_str(&line[0..line_length]);
result.push('\n');
} else {
result.push_str(&format!("{}\n", line));
}
}
result
}
pub trait MaxLine {
fn max_line(&self) -> LineNumber;
}

@ -1,7 +1,8 @@
use atty::Stream;
use std::cmp::{max, min};
use std::collections::HashMap;
use crate::lines::{enforce_length, format_line_num, LineGroup, LineNumber};
use crate::lines::{enforce_length, enforce_max_length, format_line_num, LineGroup, LineNumber};
use crate::style::apply_colors;
use crate::syntax::{aligned_lines, MatchedPos};
@ -193,14 +194,19 @@ pub fn display(
groups,
lhs_formatted_length,
rhs_column_width,
terminal_width,
if atty::is(Stream::Stdout) {
terminal_width
} else {
// Assume a pager like less.
1000
},
);
let lhs_content_width = lhs_formatted_length - lhs_column_width;
let rhs_content_width = rhs_formatted_length - rhs_column_width;
let lhs_src = enforce_length(lhs_src, lhs_content_width);
let rhs_src = enforce_length(rhs_src, rhs_content_width);
let rhs_src = enforce_max_length(rhs_src, rhs_content_width);
let lhs_colored = apply_colors(&lhs_src, true, lhs_positions);
let rhs_colored = apply_colors(&rhs_src, false, rhs_positions);