@@ -27,15 +27,22 @@ fn main() {
2727 // Process arguments
2828 let opt = Opt :: from_args ( ) ;
2929
30+ // Require a command to run
3031 if opt. command . is_empty ( ) {
3132 println ! ( "Need to supply a command." ) ;
3233 return ;
3334 }
3435
36+ // Run command
3537 do_watchdiff ( opt) ;
3638}
3739
3840fn 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+
3946 // Setup command and arguments
4047 let mut raw = Command :: new ( & opt. command [ 0 ] ) ;
4148 let cmd = raw. args ( & opt. command [ 1 ..] ) ;
@@ -68,32 +75,44 @@ fn run_command(cmd: &mut Command) -> String {
6875}
6976
7077fn print_diff ( a : & str , b : & str ) {
78+ // Print diff time
7179 let local = Local :: now ( ) ;
7280 let banner = format ! ( "Diff at {}" , local. to_string( ) ) ;
7381 println ! ( "{}" , banner. bold( ) . underline( ) ) ;
82+
83+ // Split output into lines
7484 let orig = a. split ( '\n' ) . collect :: < Vec < & str > > ( ) ;
7585 let new = b. split ( '\n' ) . collect :: < Vec < & str > > ( ) ;
7686
87+ // Calculate the max index values
7788 let orig_max = orig. len ( ) - 1 ;
7889 let new_max = new. len ( ) - 1 ;
90+
91+ // Instantiate counters
7992 let mut orig_idx = 0 ;
8093 let mut new_idx = 0 ;
8194
95+ // Iterate through each index of the orig and new lists
8296 ' check: loop {
8397 if orig_idx == orig_max && new_idx == new_max {
98+ // If we've reached the end of both lists, we're done
8499 break ;
85100 } else if orig_idx == orig_max && new_idx < new_max {
101+ // If we've reached the end of the original, everything else was added
86102 print_all ( & new[ new_idx..new_max] , PrintType :: Add ) ;
87103 break ;
88104 } else if new_idx == new_max && orig_idx < orig_max {
105+ // If we've reached the end of the new, everything else was removed
89106 print_all ( & orig[ orig_idx..orig_max] , PrintType :: Del ) ;
90107 break ;
91108 } else if orig[ orig_idx] == new[ new_idx] {
109+ // If both values are identical, there was no change
92110 print_same ( orig[ orig_idx] ) ;
93111 orig_idx += 1 ;
94112 new_idx += 1 ;
95113 continue ;
96114 } else {
115+ // Iterate through the rest of the new list looking for the current old to identify an added item in new
97116 let tmp = new_idx;
98117 for i in tmp..new_max {
99118 if orig[ orig_idx] == new[ i] {
@@ -102,6 +121,8 @@ fn print_diff(a: &str, b: &str) {
102121 continue ' check;
103122 }
104123 }
124+
125+ // Iterate through the rest of the orig list looking for the current new to identify a removed item in orig
105126 let tmp = orig_idx;
106127 for i in tmp..orig_max {
107128 if new[ new_idx] == orig[ i] {
@@ -112,6 +133,7 @@ fn print_diff(a: &str, b: &str) {
112133 }
113134 }
114135
136+ // If nothing else was detected, the current index was both removed from orig and added in new
115137 print_del ( orig[ orig_idx] ) ;
116138 print_add ( new[ new_idx] ) ;
117139
@@ -121,22 +143,26 @@ fn print_diff(a: &str, b: &str) {
121143}
122144
123145fn print_all ( a : & [ & str ] , print : PrintType ) {
146+ // Depending on Add or Del, iterate through the slice and print each line
124147 match print {
125148 PrintType :: Add => a. iter ( ) . map ( |a| print_add ( a) ) . collect ( ) ,
126149 PrintType :: Del => a. iter ( ) . map ( |a| print_del ( a) ) . collect ( ) ,
127150 }
128151}
129152
130153fn print_add ( a : & str ) {
154+ // Print an added line
131155 let b = format ! ( " + {}" , a) ;
132156 println ! ( "{}" , b. green( ) ) ;
133157}
134158
135159fn print_del ( a : & str ) {
160+ // Print a removed line
136161 let b = format ! ( " - {}" , a) ;
137162 println ! ( "{}" , b. red( ) ) ;
138163}
139164
140165fn print_same ( a : & str ) {
166+ // Print a line
141167 println ! ( " {}" , a) ;
142168}
0 commit comments