11use crate :: filereader:: FileReader ;
22use crate :: style;
3+ use crate :: vector_comparer:: { IVectorComparer , VectorComparer } ;
34use iced:: { alignment, scrollable, Rule } ;
45use iced:: {
56 button, text_input, Alignment , Button , Column , Container , Element , Length , Radio , Row , Sandbox ,
@@ -15,6 +16,7 @@ pub enum Message {
1516 SelectFirstFilePressed ,
1617 SelectSecondFilePressed ,
1718 ComparePressed ,
19+ ClearComparePressed ,
1820}
1921
2022#[ derive( Default ) ]
@@ -27,8 +29,10 @@ pub struct ApplicationContext {
2729 pub btn_select_first_file : button:: State ,
2830 pub btn_select_second_file : button:: State ,
2931 pub btn_compare : button:: State ,
32+ pub btn_clean_compare : button:: State ,
3033 pub scrollable : scrollable:: State ,
3134 pub differences : Vec < String > ,
35+ pub has_compared : bool ,
3236}
3337
3438impl Sandbox for ApplicationContext {
@@ -49,6 +53,7 @@ impl Sandbox for ApplicationContext {
4953 Message :: SelectFirstFilePressed => {
5054 let path = FileDialog :: new ( )
5155 . add_filter ( "Text file" , & [ "txt" ] )
56+ . add_filter ( "All files" , & [ "*" ] )
5257 . show_open_single_file ( )
5358 . unwrap ( ) ;
5459
@@ -62,6 +67,7 @@ impl Sandbox for ApplicationContext {
6267 Message :: SelectSecondFilePressed => {
6368 let path = FileDialog :: new ( )
6469 . add_filter ( "Text file" , & [ "txt" ] )
70+ . add_filter ( "All files" , & [ "*" ] )
6571 . show_open_single_file ( )
6672 . unwrap ( ) ;
6773
@@ -94,7 +100,7 @@ impl Sandbox for ApplicationContext {
94100 MessageDialog :: new ( )
95101 . set_type ( MessageType :: Error )
96102 . set_title ( "text-diff" )
97- . set_text ( & format ! ( "Error while reading file!\n {}" , e) )
103+ . set_text ( & format ! ( "Error while reading file {} !\n {}" , & self . first_file , e) )
98104 . show_alert ( )
99105 . unwrap ( ) ;
100106 return ;
@@ -107,46 +113,26 @@ impl Sandbox for ApplicationContext {
107113 MessageDialog :: new ( )
108114 . set_type ( MessageType :: Error )
109115 . set_title ( "text-diff" )
110- . set_text ( & format ! ( "Error while reading file!\n {}" , e) )
116+ . set_text ( & format ! ( "Error while reading file {} !\n {}" , & self . second_file , e) )
111117 . show_alert ( )
112118 . unwrap ( ) ;
113119 return ;
114120 }
115121 } ;
116122
117- let mut diff = vec ! [ ] ;
118- for f in & lines_first_file {
119- let mut included = false ;
120- for d in & lines_second_file {
121- if f. eq ( d) {
122- included = true ;
123- }
124- }
125-
126- if !included {
127- diff. push ( String :: from ( f) ) ;
128- }
129- }
123+ let vector_comparer: VectorComparer < String > =
124+ IVectorComparer :: < String > :: new ( lines_first_file, lines_second_file) ;
130125
131- for f in & lines_second_file {
132- let mut included = false ;
133- for d in & lines_first_file {
134- if f. eq ( d) {
135- included = true ;
136- }
137- }
138-
139- if !included {
140- let n = String :: from ( f) ;
141- if !diff. contains ( & n) {
142- diff. push ( n) ;
143- }
144- }
145- }
146-
147- self . differences = diff;
126+ self . differences = vector_comparer. get_differences ( ) ;
127+ self . has_compared = true ;
148128 }
149129 Message :: ThemeChanged ( d) => self . theme = d,
130+ Message :: ClearComparePressed => {
131+ self . first_file = String :: new ( ) ;
132+ self . second_file = String :: new ( ) ;
133+ self . has_compared = false ;
134+ self . differences = vec ! [ ] ;
135+ }
150136 } ;
151137 }
152138
@@ -213,11 +199,44 @@ impl Sandbox for ApplicationContext {
213199 . on_press ( Message :: SelectSecondFilePressed )
214200 . style ( self . theme ) ;
215201
216- let btn_compare = Button :: new ( & mut self . btn_compare , Text :: new ( "Compare" ) )
202+ let btn_compare = Button :: new (
203+ & mut self . btn_compare ,
204+ Text :: new ( "Compare" ) . horizontal_alignment ( alignment:: Horizontal :: Center ) ,
205+ )
206+ . padding ( 10 )
207+ . min_width ( 100 )
208+ . on_press ( Message :: ComparePressed )
209+ . style ( self . theme ) ;
210+
211+ let mut compare_row = Row :: new ( ) . spacing ( 10 ) ;
212+
213+ if self . has_compared {
214+ let btn_clean_compare = Button :: new (
215+ & mut self . btn_clean_compare ,
216+ Text :: new ( "Clear" ) . horizontal_alignment ( alignment:: Horizontal :: Center ) ,
217+ )
217218 . padding ( 10 )
218- . on_press ( Message :: ComparePressed )
219+ . min_width ( 100 )
220+ . on_press ( Message :: ClearComparePressed )
219221 . style ( self . theme ) ;
220222
223+ compare_row = compare_row. push (
224+ Column :: new ( )
225+ . width ( Length :: Fill )
226+ . align_items ( Alignment :: Start )
227+ . spacing ( 20 )
228+ . push ( btn_clean_compare) ,
229+ ) ;
230+ }
231+
232+ compare_row = compare_row. push (
233+ Column :: new ( )
234+ . width ( Length :: Fill )
235+ . align_items ( Alignment :: End )
236+ . spacing ( 20 )
237+ . push ( btn_compare) ,
238+ ) ;
239+
221240 let mut content = Column :: new ( )
222241 . spacing ( 15 )
223242 . padding ( 20 )
@@ -236,27 +255,24 @@ impl Sandbox for ApplicationContext {
236255 . push ( second_file_input)
237256 . push ( btn_select_second_file) ,
238257 )
239- . push (
240- Row :: new ( ) . spacing ( 10 ) . push (
241- Column :: new ( )
242- . width ( Length :: Fill )
243- . align_items ( Alignment :: End )
244- . spacing ( 20 )
245- . push ( btn_compare) ,
246- ) ,
247- ) ;
258+ . push ( compare_row) ;
248259
249- if !self . differences . is_empty ( ) {
250- let choose_theme = self . differences . iter ( ) . fold (
260+ if self . has_compared {
261+ let mut diff_text = Text :: new ( "Differences:" ) ;
262+ if self . differences . is_empty ( ) {
263+ diff_text = Text :: new ( "No differences detected!" )
264+ }
265+
266+ let diff_column = self . differences . iter ( ) . fold (
251267 Column :: new ( )
252268 . spacing ( 10 )
253- . push ( Text :: new ( "Differences:" ) . size ( 30 ) ) ,
269+ . push ( diff_text . size ( 30 ) ) ,
254270 |column, theme| column. push ( Text :: new ( format ! ( "{}" , theme) ) ) ,
255271 ) ;
256272
257273 content = content
258274 . push ( Rule :: horizontal ( 20 ) . style ( self . theme ) )
259- . push ( choose_theme ) ;
275+ . push ( diff_column ) ;
260276 }
261277
262278 content = content
0 commit comments