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

Truncate long lines #290

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ pub struct Opt {
/// Text to display in front of a renamed file path.
pub file_renamed_label: String,

#[structopt(long = "max-line-length", default_value = "512")]
/// Truncate lines longer than this. To prevent any truncation, set to zero. Note that
/// syntax-highlighting very long lines (e.g. minified .js) will be very slow if they are not
/// truncated.
pub max_line_length: usize,

/// The width of underline/overline decorations. Use --width=variable to extend decorations and
/// background colors to the end of the text only. Otherwise background colors extend to the
/// full terminal width.
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Config {
pub max_buffered_lines: usize,
pub max_line_distance: f64,
pub max_line_distance_for_naively_paired_lines: f64,
pub max_line_length: usize,
pub minus_emph_style: Style,
pub minus_empty_line_marker_style: Style,
pub minus_file: Option<PathBuf>,
Expand Down Expand Up @@ -170,6 +171,7 @@ impl From<cli::Opt> for Config {
max_buffered_lines: 32,
max_line_distance: opt.max_line_distance,
max_line_distance_for_naively_paired_lines,
max_line_length: opt.max_line_length,
minus_emph_style,
minus_empty_line_marker_style,
minus_file: opt.minus_file.map(|s| s.clone()),
Expand Down
5 changes: 5 additions & 0 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ where

while let Some(Ok(raw_line_bytes)) = lines.next() {
let raw_line = String::from_utf8_lossy(&raw_line_bytes);
let raw_line = if config.max_line_length > 0 && raw_line.len() > config.max_line_length {
ansi::truncate_str(&raw_line, config.max_line_length, &config.truncation_symbol)
} else {
raw_line
};
let line = ansi::strip_ansi_codes(&raw_line).to_string();
if source == Source::Unknown {
source = detect_source(&line);
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub fn set_options(
inspect_raw_lines,
keep_plus_minus_markers,
max_line_distance,
max_line_length,
// Hack: minus-style must come before minus-*emph-style because the latter default
// dynamically to the value of the former.
minus_style,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_raw_output_matches_git_on_full_repo_history
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
DELTA="./target/release/delta --no-gitconfig --raw"
DELTA="./target/release/delta --no-gitconfig --raw --max-line-length 0"
ANSIFILTER="./etc/bin/ansifilter"
GIT_ARGS="log --patch --stat --numstat"
diff -u <(git $GIT_ARGS | $ANSIFILTER) <(git $GIT_ARGS | $DELTA | $ANSIFILTER)