diff --git a/src/main.rs b/src/main.rs index b4feb2a42..85f09a23e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod intervals; mod lines; mod parse; mod positions; +mod side_by_side; mod style; mod syntax; use clap::{App, AppSettings, Arg}; @@ -12,12 +13,8 @@ use std::path::Path; use typed_arena::Arena; use crate::dijkstra::mark_syntax; -use crate::lines::{ - apply_groups, enforce_length, format_line_num, join_overlapping, lhs_printable_width, - rhs_printable_width, visible_groups, MaxLine, -}; +use crate::lines::{join_overlapping, visible_groups, MaxLine}; use crate::parse::{find_lang, parse, parse_lines, ConfigDir}; -use crate::style::apply_colors; use crate::syntax::{change_positions, init_info, matching_lines}; fn read_or_die(path: &str) -> Vec { @@ -40,10 +37,6 @@ fn read_or_die(path: &str) -> Vec { } } -fn term_width() -> Option { - term_size::dimensions().map(|(w, _)| w) -} - /// Do these bytes look like a binary (non-textual) format? fn is_probably_binary(bytes: &[u8]) -> bool { // If more than 20 of the first 1,000 characters are not valid @@ -116,8 +109,6 @@ fn main() { .to_string() .replace("\t", " "); - let terminal_width = term_width().unwrap_or(80); - let arena = Arena::new(); let (lhs, rhs) = match &lang { @@ -149,38 +140,15 @@ fn main() { } groups = join_overlapping(groups); - let lhs_column_width = format_line_num(groups.last().unwrap().max_visible_lhs().0).len(); - let rhs_column_width = format_line_num(groups.last().unwrap().max_visible_rhs().0).len(); - - let lhs_formatted_length = - lhs_printable_width(&lhs_src, &groups, lhs_column_width, terminal_width); - let rhs_formatted_length = rhs_printable_width( - &rhs_src, - &groups, - lhs_formatted_length, - rhs_column_width, - terminal_width, - ); - - 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 lhs_colored = apply_colors(&lhs_src, true, &lhs_positions); - let rhs_colored = apply_colors(&rhs_src, false, &rhs_positions); - print!( "{}", - apply_groups( - &lhs_colored, - &rhs_colored, - &groups, + side_by_side::display( + &lhs_src, + &rhs_src, + &lhs_positions, + &rhs_positions, &lhs_matched_lines, - lhs_content_width, - rhs_content_width, - lhs_column_width, - rhs_column_width, + &groups ) ); println!(); diff --git a/src/side_by_side.rs b/src/side_by_side.rs new file mode 100644 index 000000000..db8e9ecc6 --- /dev/null +++ b/src/side_by_side.rs @@ -0,0 +1,55 @@ +use std::collections::HashMap; + +use crate::lines::{ + apply_groups, enforce_length, format_line_num, lhs_printable_width, rhs_printable_width, + LineGroup, LineNumber, +}; +use crate::style::apply_colors; +use crate::syntax::MatchedPos; + +fn term_width() -> Option { + term_size::dimensions().map(|(w, _)| w) +} + +pub fn display( + lhs_src: &str, + rhs_src: &str, + lhs_positions: &[MatchedPos], + rhs_positions: &[MatchedPos], + lhs_matched_lines: &HashMap, + groups: &[LineGroup], +) -> String { + let lhs_column_width = format_line_num(groups.last().unwrap().max_visible_lhs().0).len(); + let rhs_column_width = format_line_num(groups.last().unwrap().max_visible_rhs().0).len(); + + let terminal_width = term_width().unwrap_or(80); + + let lhs_formatted_length = + lhs_printable_width(lhs_src, groups, lhs_column_width, terminal_width); + let rhs_formatted_length = rhs_printable_width( + rhs_src, + groups, + lhs_formatted_length, + rhs_column_width, + terminal_width, + ); + + 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 lhs_colored = apply_colors(&lhs_src, true, lhs_positions); + let rhs_colored = apply_colors(&rhs_src, false, rhs_positions); + + apply_groups( + &lhs_colored, + &rhs_colored, + groups, + lhs_matched_lines, + lhs_content_width, + rhs_content_width, + lhs_column_width, + rhs_column_width, + ) +}