| 1 |
[2mdiff --git xsrc/plusminus_header_highlighter.rs xsrc/plusminus_header_highlighter.rs[0m |
| 2 |
[2mindex 5bb5056..c354931 100644[0m |
| 3 |
[1m--- [0mxsrc/[1mplusminus_header_highlighter.rs[0m |
| 4 |
[1m+++ [0mxsrc/[1mplusminus_header_highlighter.rs[0m |
| 5 |
[36m@@ -6,7 +6,7 @@ [1muse crate::lines_highlighter::{LinesHighlighter, Response};[0m |
| 6 |
use crate::refiner::to_highlighted_tokens; |
| 7 |
use crate::string_future::StringFuture; |
| 8 |
use crate::token_collector::{ |
| 9 |
[31m- lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,[0m |
| 10 |
[32m+ [7malign_tabs,[27m lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,[0m |
| 11 |
LINE_STYLE_OLD_FILENAME, |
| 12 |
}; |
| 13 |
|
| 14 |
[36m@@ -92,8 +92,11 @@ [1mimpl PlusMinusHeaderHighlighter {[0m |
| 15 |
to_highlighted_tokens(&self.old_name, &self.new_name); |
| 16 |
|
| 17 |
lowlight_timestamp(&mut old_tokens); |
| 18 |
[31m- unhighlight_git_prefix(&mut old_tokens);[0m |
| 19 |
lowlight_timestamp(&mut new_tokens); |
| 20 |
[32m+[0m |
| 21 |
[32m+ align_tabs(&mut old_tokens, &mut new_tokens);[0m |
| 22 |
[32m+[0m |
| 23 |
[32m+ unhighlight_git_prefix(&mut old_tokens);[0m |
| 24 |
unhighlight_git_prefix(&mut new_tokens); |
| 25 |
|
| 26 |
let old_filename = render(&LINE_STYLE_OLD_FILENAME, "--- ", &old_tokens); |
| 27 |
[36m@@ -108,3 +111,34 @@ [1mimpl PlusMinusHeaderHighlighter {[0m |
| 28 |
return highlighted; |
| 29 |
} |
| 30 |
} |
| 31 |
[32m+[0m |
| 32 |
[32m+#[cfg(test)][0m |
| 33 |
[32m+mod tests {[0m |
| 34 |
[32m+ use crate::ansi::remove_ansi_escape_codes;[0m |
| 35 |
[32m+[0m |
| 36 |
[32m+ use super::*;[0m |
| 37 |
[32m+[0m |
| 38 |
[32m+ #[test][0m |
| 39 |
[32m+ fn test_align_timestamps() {[0m |
| 40 |
[32m+ let mut test_me =[0m |
| 41 |
[32m+ PlusMinusHeaderHighlighter::from_line("--- x.txt\t2023-12-15 15:43:29").unwrap();[0m |
| 42 |
[32m+ let mut response = test_me[0m |
| 43 |
[32m+ .consume_line([0m |
| 44 |
[32m+ "+++ /Users/johan/src/riff/README.md\t2024-01-29 14:56:40",[0m |
| 45 |
[32m+ &ThreadPool::new(1),[0m |
| 46 |
[32m+ )[0m |
| 47 |
[32m+ .unwrap();[0m |
| 48 |
[32m+ assert_eq!(LineAcceptance::AcceptedDone, response.line_accepted);[0m |
| 49 |
[32m+ assert_eq!(1, response.highlighted.len());[0m |
| 50 |
[32m+[0m |
| 51 |
[32m+ let mut highlighted = response.highlighted[0].get().to_string().into_bytes();[0m |
| 52 |
[32m+ remove_ansi_escape_codes(&mut highlighted);[0m |
| 53 |
[32m+ let plain = String::from_utf8(highlighted).unwrap();[0m |
| 54 |
[32m+[0m |
| 55 |
[32m+ assert_eq!([0m |
| 56 |
[32m+ "--- x.txt 2023-12-15 15:43:29\n\[0m |
| 57 |
[32m+ +++ /Users/johan/src/riff/README.md 2024-01-29 14:56:40\n",[0m |
| 58 |
[32m+ plain.as_str()[0m |
| 59 |
[32m+ );[0m |
| 60 |
[32m+ }[0m |
| 61 |
[32m+}[0m |
| 62 |
[2mdiff --git xsrc/token_collector.rs xsrc/token_collector.rs[0m |
| 63 |
[2mindex b58c50f..e4d4152 100644[0m |
| 64 |
[1m--- [0mxsrc/[1mtoken_collector.rs[0m |
| 65 |
[1m+++ [0mxsrc/[1mtoken_collector.rs[0m |
| 66 |
[36m@@ -1,3 +1,5 @@[0m |
| 67 |
[32m+use std::cmp;[0m |
| 68 |
[32m+[0m |
| 69 |
use crate::ansi::AnsiStyle; |
| 70 |
use crate::ansi::Color::Default; |
| 71 |
use crate::ansi::Color::Green; |
| 72 |
[36m@@ -311,6 +313,38 @@ [1mpub fn highlight_nonleading_tabs(tokens: &mut [StyledToken]) {[0m |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
[32m+pub(crate) fn align_tabs(old: &mut [StyledToken], new: &mut [StyledToken]) {[0m |
| 77 |
[32m+ let old_tab_index_token = old.iter().position(|token| token.token == "\t");[0m |
| 78 |
[32m+ if old_tab_index_token.is_none() {[0m |
| 79 |
[32m+ return;[0m |
| 80 |
[32m+ }[0m |
| 81 |
[32m+ let old_tab_index_token = old_tab_index_token.unwrap();[0m |
| 82 |
[32m+ let old_tab_index_char = old[0m |
| 83 |
[32m+ .iter()[0m |
| 84 |
[32m+ .take(old_tab_index_token)[0m |
| 85 |
[32m+ .map(|token| token.token.chars().count())[0m |
| 86 |
[32m+ .sum::<usize>();[0m |
| 87 |
[32m+[0m |
| 88 |
[32m+ let new_tab_index_token = new.iter().position(|token| token.token == "\t");[0m |
| 89 |
[32m+ if new_tab_index_token.is_none() {[0m |
| 90 |
[32m+ return;[0m |
| 91 |
[32m+ }[0m |
| 92 |
[32m+ let new_tab_index_token = new_tab_index_token.unwrap();[0m |
| 93 |
[32m+ let new_tab_index_char = new[0m |
| 94 |
[32m+ .iter()[0m |
| 95 |
[32m+ .take(new_tab_index_token)[0m |
| 96 |
[32m+ .map(|token| token.token.chars().count())[0m |
| 97 |
[32m+ .sum::<usize>();[0m |
| 98 |
[32m+[0m |
| 99 |
[32m+ let old_spaces =[0m |
| 100 |
[32m+ " ".repeat(2 + cmp::max(old_tab_index_char, new_tab_index_char) - old_tab_index_char);[0m |
| 101 |
[32m+ let new_spaces =[0m |
| 102 |
[32m+ " ".repeat(2 + cmp::max(old_tab_index_char, new_tab_index_char) - new_tab_index_char);[0m |
| 103 |
[32m+[0m |
| 104 |
[32m+ old[old_tab_index_token].token = old_spaces;[0m |
| 105 |
[32m+ new[new_tab_index_token].token = new_spaces;[0m |
| 106 |
[32m+}[0m |
| 107 |
[32m+[0m |
| 108 |
/// Highlight single space between two highlighted tokens |
| 109 |
pub fn bridge_consecutive_highlighted_tokens(tokens: &mut [StyledToken]) { |
| 110 |
enum FoundState { |