dotpatina 1.5.0

dotpatina is a rust application for managing system dotfiles and configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! The diff module provides diffing behavior for files. This includes diff analysis and utilities
//! for displaying diff results.

use std::cmp::max;

use colored::{Color, Colorize};
use similar::{Change, ChangeTag, TextDiff};

/// [DiffAnalysis] provides functionality for diffs within dotpatina
pub trait DiffAnalysis {
    /// Determine if there are any changes in the diff
    fn any_changes(&self) -> bool;

    /// Get a String representation of the diff for display
    fn to_string(&self) -> String;
}

/// Details for a line output for a diff
struct DiffLine {
    /// The line number in the old file
    old_line_num: Option<usize>,

    /// The line number in the new file
    new_line_num: Option<usize>,

    /// A character indicating the type of change
    diff_char: char,

    /// The change tag for the line
    change_tag: ChangeTag,

    /// The string representation of the change
    change_string: String,

    /// The color to use for the line
    color: Option<Color>,

    /// The number of lines since the [DiffLine] that was a change
    count_from_change: isize,
}

impl DiffLine {
    fn to_string(&self, line_number_width: usize) -> String {
        let old_line_num = match self.old_line_num {
            Some(l) => l.to_string(),
            None => "".to_string(),
        };
        let new_line_num = match self.new_line_num {
            Some(l) => l.to_string(),
            None => "".to_string(),
        };
        let line = format!(
            "{} {: >num_width$} {: >num_width$} | {}",
            self.diff_char,
            old_line_num,
            new_line_num,
            self.change_string,
            num_width = line_number_width
        );
        match self.color {
            Some(color) => line.color(color).to_string(),
            None => line,
        }
    }
}

impl<'lines, O: ?Sized> DiffAnalysis for TextDiff<'lines, 'lines, '_, O>
where
    O: similar::DiffableStr,
{
    fn any_changes(&self) -> bool {
        self.iter_all_changes()
            .any(|change| change.tag() != ChangeTag::Equal)
    }

    fn to_string(&self) -> String {
        /// The number of unchanged lines to show around diff changes.
        /// This provides context to the viewer within the file.
        static DIFF_GAP_BUFFER_SIZE: isize = 4;

        // Keep a running count of line number for the old file
        // the line number will increase for the old file if lines were removed or are unchanged
        let mut old_line_count = 1;

        // Keep a running count of line number for the new file
        // the line number will increase for the new file if lines were inserted or are unchanged
        let mut new_line_count = 1;

        // Keep a count of the number of lines since a change happened.
        // If negative, there hasn't been a change yet.
        // This is used for determining what lines are far away enough from changes to skip displaying.
        let mut count_from_change = -1;

        // This closure is used for the first iteration over the changes.
        // It generates a DiffLine struct with relevant data for the diff
        // and keeps track of the variables defined above.
        let change_to_diff_line = |change: Change<&O>| match change.tag() {
            ChangeTag::Insert => {
                count_from_change = 0;
                let line = DiffLine {
                    old_line_num: None,
                    new_line_num: Some(new_line_count),
                    diff_char: '+',
                    change_tag: ChangeTag::Insert,
                    change_string: change.to_string(),
                    color: Some(Color::Green),
                    count_from_change,
                };
                new_line_count += 1;
                line
            }
            ChangeTag::Equal => {
                if count_from_change >= 0 {
                    count_from_change += 1;
                }
                let line = DiffLine {
                    old_line_num: Some(old_line_count),
                    new_line_num: Some(new_line_count),
                    diff_char: ' ',
                    change_tag: ChangeTag::Equal,
                    change_string: change.to_string(),
                    color: None,
                    count_from_change,
                };
                old_line_count += 1;
                new_line_count += 1;
                line
            }
            ChangeTag::Delete => {
                count_from_change = 0;
                let line = DiffLine {
                    old_line_num: Some(old_line_count),
                    new_line_num: None,
                    diff_char: '-',
                    change_tag: ChangeTag::Delete,
                    change_string: change.to_string(),
                    color: Some(Color::Red),
                    count_from_change,
                };
                old_line_count += 1;
                line
            }
        };

        // Iterate through the changes and collect to a vector
        let mut diff_lines = self
            .iter_all_changes()
            .map(change_to_diff_line)
            .collect::<Vec<DiffLine>>();

        // Determine the width of the line number columns
        let line_number_width = max(
            old_line_count.to_string().len(),
            new_line_count.to_string().len(),
        );

        // Keep a count of the number of lines until a change happens.
        // If negative, there won't be another change.
        // This is used for determining what lines are far away enough from changes to skip displaying.
        let mut count_to_changed = -1;

        // This closure is used in the second (reversed) iteration through the diff lines.
        // It updates the count_to_changed, and uses that value to determine (and return)
        // whether this line should be displayed.
        let mut check_diff_gap = |diff_line: &mut DiffLine| {
            match diff_line.change_tag {
                ChangeTag::Insert => {
                    count_to_changed = 0;
                }
                ChangeTag::Equal => {
                    if count_to_changed >= 0 {
                        count_to_changed += 1;
                    }
                }
                ChangeTag::Delete => {
                    count_to_changed = 0;
                }
            }

            let far_from_start = diff_line.count_from_change > DIFF_GAP_BUFFER_SIZE
                || diff_line.count_from_change < 0;
            let far_from_end = count_to_changed > DIFF_GAP_BUFFER_SIZE || count_to_changed < 0;

            // The line should be displayed if it is not far (within DIFF_GAP_BUFFER_SIZE lines)
            // from the start or end of a change.
            !far_from_start || !far_from_end
        };

        // Create a variable for the resulting string
        let mut result = String::from("");

        // Keep a running count of skipped lines
        let mut skipped_lines = 0;

        // Iterate through the list for the second time in reverse.
        // This is reversed so that we can track count_to_changed properly.
        for diff_line in diff_lines.iter_mut().rev() {
            let show_line = check_diff_gap(diff_line);
            if show_line {
                // If we are showing a line, but have been skipping lines,
                // display the number of unchanged lines
                if skipped_lines > 0 {
                    result = format!("{} unchanged lines\n", skipped_lines)
                        .blue()
                        .to_string()
                        + &result;
                }
                skipped_lines = 0;

                // Add the current line string to beginning the result.
                // This reverses the reverse iteration.
                let line = diff_line.to_string(line_number_width);
                result = line + &result;
            } else {
                skipped_lines += 1;
            }
        }

        // If we finished the second iteration and were skipping lines,
        // then display the number of unchanged lines again.
        // This is the case when there aren't any changes at the beginning of a file.
        if skipped_lines > 0 {
            result = format!("{} unchanged lines\n", skipped_lines)
                .blue()
                .to_string()
                + &result;
        }

        // Finally, return the result
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn build_test_diff() -> TextDiff<'static, 'static, 'static, str> {
        let old = r#"
        AAA
        BBB
        CCC
        "#;
        let new = r#"
        AAA
        CCC
        DDD
        "#;
        TextDiff::from_lines(old, new)
    }

    #[test]
    fn test_diff_analysis_any_changes_no_changes() {
        let old = "this is some text";
        let new = "this is some text";
        let diff = TextDiff::from_lines(old, new);

        assert!(!diff.any_changes())
    }

    #[test]
    fn test_diff_analysis_any_changes_deleted_line() {
        let old = r#"
        AAA
        BBB
        CCC
        "#;
        let new = r#"
        AAA
        CCC
        "#;
        let diff = TextDiff::from_lines(old, new);

        assert!(diff.any_changes())
    }

    #[test]
    fn test_diff_analysis_any_changes_added_line() {
        let diff = build_test_diff();
        assert!(diff.any_changes())
    }

    #[test]
    fn test_diff_to_string() {
        colored::control::set_override(false);
        let diff = build_test_diff();
        let result = diff.to_string();

        let expected_lines = [
            "  1 1 | ",
            "  2 2 |         AAA",
            "- 3   |         BBB",
            "  4 3 |         CCC",
            "+   4 |         DDD",
            "  5 5 |         ",
            "",
        ];
        assert_eq!(result, expected_lines.join("\n"));
    }

    #[test]
    fn test_diff_to_string_with_skipped_lines() {
        colored::control::set_override(false);
        let old = r#"[alias]
    lg = !git lg1
    lg1 = !git lg1-specific --all
    lg2 = !git lg2-specific --all
    lg3 = !git lg3-specific --all

[pager]
    branch = false

[core]
    editor = vim

[pull]
    rebase = false

[init]
    defaultBranch = main

[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
"#;
        let new = r#"[alias]
    lg = !git lg1
    lg1 = !git lg1-specific --all

[pager]
    branch = false

[core]
    editor = vim

[pull]
    rebase = false

[init]
    defaultBranch = main

[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true

[fetch]
    prune = true
"#;
        let diff = TextDiff::from_lines(old, new);

        let result = diff.to_string();
        let expected_lines = [
            "   1  1 | [alias]",
            "   2  2 |     lg = !git lg1",
            "   3  3 |     lg1 = !git lg1-specific --all",
            "-  4    |     lg2 = !git lg2-specific --all",
            "-  5    |     lg3 = !git lg3-specific --all",
            "   6  4 | ",
            "   7  5 | [pager]",
            "   8  6 |     branch = false",
            "   9  7 | ",
            "10 unchanged lines",
            "  20 18 |     clean = git-lfs clean -- %f",
            "  21 19 |     smudge = git-lfs smudge -- %f",
            "  22 20 |     process = git-lfs filter-process",
            "  23 21 |     required = true",
            "+    22 | ",
            "+    23 | [fetch]",
            "+    24 |     prune = true",
            "",
        ];
        assert_eq!(result, expected_lines.join("\n"));
    }

    #[test]
    fn test_diff_to_string_with_skipped_lines_at_the_start_and_end() {
        colored::control::set_override(false);
        let old = r#"[alias]
    lg = !git lg1
    lg1 = !git lg1-specific --all
    lg2 = !git lg2-specific --all
    lg3 = !git lg3-specific --all

[pager]
    branch = false

[core]
    editor = vim

[pull]
    rebase = false

[init]
    defaultBranch = main

[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
"#;
        let new = r#"[alias]
    lg = !git lg1
    lg1 = !git lg1-specific --all
    lg2 = !git lg2-specific --all
    lg3 = !git lg3-specific --all

[pager]
    branch = false

[pull]
    rebase = false

[init]
    defaultBranch = main

[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
"#;

        let diff = TextDiff::from_lines(old, new);

        let result = diff.to_string();
        let expected_lines = [
            "5 unchanged lines",
            "   6  6 | ",
            "   7  7 | [pager]",
            "   8  8 |     branch = false",
            "   9  9 | ",
            "- 10    | [core]",
            "- 11    |     editor = vim",
            "- 12    | ",
            "  13 10 | [pull]",
            "  14 11 |     rebase = false",
            "  15 12 | ",
            "  16 13 | [init]",
            "7 unchanged lines",
            "",
        ];
        assert_eq!(result, expected_lines.join("\n"));
    }

    #[test]
    fn test_diff_to_string_no_changes() {
        colored::control::set_override(false);
        let old = r#"aaa
        bbb
        ccc"#;
        let new = r#"aaa
        bbb
        ccc"#;

        let diff = TextDiff::from_lines(old, new);

        let result = diff.to_string();
        let expected_lines = ["3 unchanged lines", ""];
        assert_eq!(result, expected_lines.join("\n"));
    }
}