@@ -27,15 +27,22 @@ fn main() {
27
27
// Process arguments
28
28
let opt = Opt :: from_args ( ) ;
29
29
30
+ // Require a command to run
30
31
if opt. command . is_empty ( ) {
31
32
println ! ( "Need to supply a command." ) ;
32
33
return ;
33
34
}
34
35
36
+ // Run command
35
37
do_watchdiff ( opt) ;
36
38
}
37
39
38
40
fn do_watchdiff ( opt : Opt ) {
41
+ // Print origin time
42
+ let local = Local :: now ( ) ;
43
+ let banner = format ! ( "Origin at {}" , local. to_string( ) ) ;
44
+ println ! ( "{}" , banner. bold( ) . underline( ) ) ;
45
+
39
46
// Setup command and arguments
40
47
let mut raw = Command :: new ( & opt. command [ 0 ] ) ;
41
48
let cmd = raw. args ( & opt. command [ 1 ..] ) ;
@@ -68,32 +75,44 @@ fn run_command(cmd: &mut Command) -> String {
68
75
}
69
76
70
77
fn print_diff ( a : & str , b : & str ) {
78
+ // Print diff time
71
79
let local = Local :: now ( ) ;
72
80
let banner = format ! ( "Diff at {}" , local. to_string( ) ) ;
73
81
println ! ( "{}" , banner. bold( ) . underline( ) ) ;
82
+
83
+ // Split output into lines
74
84
let orig = a. split ( '\n' ) . collect :: < Vec < & str > > ( ) ;
75
85
let new = b. split ( '\n' ) . collect :: < Vec < & str > > ( ) ;
76
86
87
+ // Calculate the max index values
77
88
let orig_max = orig. len ( ) - 1 ;
78
89
let new_max = new. len ( ) - 1 ;
90
+
91
+ // Instantiate counters
79
92
let mut orig_idx = 0 ;
80
93
let mut new_idx = 0 ;
81
94
95
+ // Iterate through each index of the orig and new lists
82
96
' check: loop {
83
97
if orig_idx == orig_max && new_idx == new_max {
98
+ // If we've reached the end of both lists, we're done
84
99
break ;
85
100
} else if orig_idx == orig_max && new_idx < new_max {
101
+ // If we've reached the end of the original, everything else was added
86
102
print_all ( & new[ new_idx..new_max] , PrintType :: Add ) ;
87
103
break ;
88
104
} else if new_idx == new_max && orig_idx < orig_max {
105
+ // If we've reached the end of the new, everything else was removed
89
106
print_all ( & orig[ orig_idx..orig_max] , PrintType :: Del ) ;
90
107
break ;
91
108
} else if orig[ orig_idx] == new[ new_idx] {
109
+ // If both values are identical, there was no change
92
110
print_same ( orig[ orig_idx] ) ;
93
111
orig_idx += 1 ;
94
112
new_idx += 1 ;
95
113
continue ;
96
114
} else {
115
+ // Iterate through the rest of the new list looking for the current old to identify an added item in new
97
116
let tmp = new_idx;
98
117
for i in tmp..new_max {
99
118
if orig[ orig_idx] == new[ i] {
@@ -102,6 +121,8 @@ fn print_diff(a: &str, b: &str) {
102
121
continue ' check;
103
122
}
104
123
}
124
+
125
+ // Iterate through the rest of the orig list looking for the current new to identify a removed item in orig
105
126
let tmp = orig_idx;
106
127
for i in tmp..orig_max {
107
128
if new[ new_idx] == orig[ i] {
@@ -112,6 +133,7 @@ fn print_diff(a: &str, b: &str) {
112
133
}
113
134
}
114
135
136
+ // If nothing else was detected, the current index was both removed from orig and added in new
115
137
print_del ( orig[ orig_idx] ) ;
116
138
print_add ( new[ new_idx] ) ;
117
139
@@ -121,22 +143,26 @@ fn print_diff(a: &str, b: &str) {
121
143
}
122
144
123
145
fn print_all ( a : & [ & str ] , print : PrintType ) {
146
+ // Depending on Add or Del, iterate through the slice and print each line
124
147
match print {
125
148
PrintType :: Add => a. iter ( ) . map ( |a| print_add ( a) ) . collect ( ) ,
126
149
PrintType :: Del => a. iter ( ) . map ( |a| print_del ( a) ) . collect ( ) ,
127
150
}
128
151
}
129
152
130
153
fn print_add ( a : & str ) {
154
+ // Print an added line
131
155
let b = format ! ( " + {}" , a) ;
132
156
println ! ( "{}" , b. green( ) ) ;
133
157
}
134
158
135
159
fn print_del ( a : & str ) {
160
+ // Print a removed line
136
161
let b = format ! ( " - {}" , a) ;
137
162
println ! ( "{}" , b. red( ) ) ;
138
163
}
139
164
140
165
fn print_same ( a : & str ) {
166
+ // Print a line
141
167
println ! ( " {}" , a) ;
142
168
}
0 commit comments