Skip to content

Commit dcabad2

Browse files
authored
Merge pull request #27 from deg4uss3r/rth/add_styles_to_line_diff
Add optional style changing to insert/remove using the config on line…
2 parents 65a7f9b + f1d855b commit dcabad2

File tree

5 files changed

+87
-20
lines changed

5 files changed

+87
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Changed
1717

18+
- Added the ability to change the style for line_diff via `ContextConfig` (#27)
19+
1820
### Removed
1921

2022
## 0.7.1

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,45 @@ void func3(){}
122122
println!("diff_lines:");
123123
println!("{}", diff_lines(code1_a, code1_b));
124124
```
125+
#### Diff Lines with Custom Config
126+
127+
![diff_lines](screens/diff_lines_format-color.png)
128+
129+
```rust
130+
use prettydiff::{diff_lines, text:ContextConfig};
131+
use owo_colors::Style;
132+
133+
let context = ContextConfig {
134+
context_size: 2,
135+
skipping_marker: "...",
136+
remove_color: Style::new().red().underline(),
137+
insert_color: Style::new().purple().bold(),
138+
};
139+
140+
let code1_a = r#"
141+
void func1() {
142+
x += 1
143+
}
144+
145+
void func2() {
146+
x += 2
147+
}
148+
"#;
149+
let code1_b = r#"
150+
void func1(a: u32) {
151+
x += 1
152+
}
153+
154+
void functhreehalves() {
155+
x += 1.5
156+
}
157+
158+
void func2() {
159+
x += 2
160+
}
161+
162+
void func3(){}
163+
"#;
164+
println!("diff_lines:");
165+
println!("{}", diff_lines(code1_a, code1_b).format_with_context(Some(context), true));
166+
```
24.9 KB
Loading

src/lcs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ where
4141
Table { x, y, table }
4242
}
4343

44-
fn seq_iter(&self) -> TableIter<T> {
44+
fn seq_iter(&self) -> TableIter<'_, T> {
4545
TableIter {
4646
x: self.x.len(),
4747
y: self.y.len(),
4848
table: self,
4949
}
5050
}
51-
fn get_match(&self, x: usize, y: usize, len: usize) -> Match<T> {
51+
fn get_match(&self, x: usize, y: usize, len: usize) -> Match<'_, T> {
5252
Match {
5353
x,
5454
y,
@@ -58,7 +58,7 @@ where
5858
}
5959

6060
/// Returns matches between X and Y
61-
pub fn matches(&self) -> Vec<Match<T>> {
61+
pub fn matches(&self) -> Vec<Match<'_, T>> {
6262
let mut matches: Vec<Match<T>> = Vec::new();
6363
for (x, y) in self.seq_iter() {
6464
if let Some(last) = matches.last_mut() {
@@ -76,7 +76,7 @@ where
7676
}
7777

7878
/// Returns matches between X and Y with zero-len match at the end
79-
pub fn matches_zero(&self) -> Vec<Match<T>> {
79+
pub fn matches_zero(&self) -> Vec<Match<'_, T>> {
8080
let mut matches = self.matches();
8181
matches.push(self.get_match(self.x.len(), self.y.len(), 0));
8282
matches

src/text.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ fn color_multilines(color: AnsiColors, s: &str) -> String {
213213
pub struct ContextConfig<'a> {
214214
pub context_size: usize,
215215
pub skipping_marker: &'a str,
216+
pub remove_color: Style,
217+
pub insert_color: Style,
218+
}
219+
220+
impl<'a> Default for ContextConfig<'a> {
221+
fn default() -> Self {
222+
ContextConfig {
223+
context_size: 0,
224+
skipping_marker: "",
225+
remove_color: Style::new().red().strikethrough(),
226+
insert_color: Style::new().green(),
227+
}
228+
}
216229
}
217230

218231
/// Container for line-by-line text diff result. Can be pretty-printed by Display trait.
@@ -414,12 +427,8 @@ impl<'a> LineChangeset<'a> {
414427
table.print(f)
415428
}
416429

417-
fn remove_color(&self, a: &str) -> String {
418-
a.red().strikethrough().to_string()
419-
}
420-
421-
fn insert_color(&self, a: &str) -> String {
422-
a.green().to_string()
430+
fn apply_style(&self, a: &str, style: Style) -> String {
431+
a.style(style).to_string()
423432
}
424433

425434
/// Returns formatted string with colors
@@ -457,16 +466,17 @@ impl<'a> LineChangeset<'a> {
457466
display_line_numbers: bool,
458467
prefix_size: usize,
459468
line_counter: &mut usize,
469+
remove_style: Style,
460470
) -> String {
461471
lines
462472
.iter()
463473
.map(|line| {
464474
let res = if display_line_numbers {
465475
// Pad and align the line number to the right
466476
format!("{:>size$} ", *line_counter, size = prefix_size - 1)
467-
+ &self.remove_color(line)
477+
+ &self.apply_style(line, remove_style)
468478
} else {
469-
" ".repeat(prefix_size) + &self.remove_color(line)
479+
" ".repeat(prefix_size) + &self.apply_style(line, remove_style)
470480
};
471481
*line_counter += 1;
472482
res
@@ -476,10 +486,10 @@ impl<'a> LineChangeset<'a> {
476486
}
477487

478488
/// Formats lines in DiffOp::Insert
479-
fn format_insert(&self, lines: &[&str], prefix_size: usize) -> String {
489+
fn format_insert(&self, lines: &[&str], prefix_size: usize, insert_style: Style) -> String {
480490
lines
481491
.iter()
482-
.map(|line| " ".repeat(prefix_size) + &self.insert_color(line))
492+
.map(|line| " ".repeat(prefix_size) + &self.apply_style(line, insert_style))
483493
.reduce(|acc, line| acc + "\n" + &line)
484494
.unwrap()
485495
}
@@ -498,14 +508,20 @@ impl<'a> LineChangeset<'a> {
498508
} else {
499509
0
500510
};
501-
let skipping_marker_size = if let Some(ContextConfig {
502-
skipping_marker, ..
511+
512+
let (skipping_marker_size, remove_color, insert_color) = if let Some(ContextConfig {
513+
skipping_marker,
514+
remove_color,
515+
insert_color,
516+
..
503517
}) = context_config
504518
{
505-
skipping_marker.len()
519+
(skipping_marker.len(), remove_color, insert_color)
506520
} else {
507-
0
521+
let c = ContextConfig::default();
522+
(c.skipping_marker.len(), c.remove_color, c.insert_color)
508523
};
524+
509525
let prefix_size = max(line_number_size, skipping_marker_size) + 1;
510526

511527
let mut next_line = 1;
@@ -520,6 +536,7 @@ impl<'a> LineChangeset<'a> {
520536
Some(ContextConfig {
521537
context_size,
522538
skipping_marker,
539+
..
523540
}) => {
524541
let mut lines = a;
525542
if !at_beginning {
@@ -559,21 +576,25 @@ impl<'a> LineChangeset<'a> {
559576
}
560577
}
561578
},
562-
basic::DiffOp::Insert(a) => out.push(self.format_insert(a, prefix_size)),
579+
basic::DiffOp::Insert(a) => {
580+
out.push(self.format_insert(a, prefix_size, insert_color))
581+
}
563582
basic::DiffOp::Remove(a) => out.push(self.format_remove(
564583
a,
565584
display_line_numbers,
566585
prefix_size,
567586
&mut next_line,
587+
remove_color,
568588
)),
569589
basic::DiffOp::Replace(a, b) => {
570590
out.push(self.format_remove(
571591
a,
572592
display_line_numbers,
573593
prefix_size,
574594
&mut next_line,
595+
remove_color,
575596
));
576-
out.push(self.format_insert(b, prefix_size));
597+
out.push(self.format_insert(b, prefix_size, insert_color));
577598
}
578599
}
579600
at_beginning = false;
@@ -869,6 +890,8 @@ fn test_format_with_context() {
869890
let context = |n| ContextConfig {
870891
context_size: n,
871892
skipping_marker: "...",
893+
remove_color: Default::default(),
894+
insert_color: Default::default(),
872895
};
873896
println!(
874897
"diff_lines:\n{}\n{:?}",

0 commit comments

Comments
 (0)