Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.

Commit c785739

Browse files
committed
* Refactoring
* Added a text when no differences are detected * Added the ability to quickly clear differences
1 parent 4cfaff5 commit c785739

File tree

3 files changed

+142
-47
lines changed

3 files changed

+142
-47
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use iced::{Sandbox, Settings};
44
mod view;
55
mod style;
66
mod filereader;
7+
mod vector_comparer;
78

89
pub fn main() -> iced::Result {
910
view::ApplicationContext::run(Settings {

src/vector_comparer.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
pub trait IVectorComparer<T> {
2+
fn new(vec1: Vec<T>, vec2: Vec<T>) -> Self;
3+
fn get_differences(&self) -> Vec<T>;
4+
}
5+
6+
#[derive(Debug, Clone)]
7+
pub struct VectorComparer<T> {
8+
pub vec1: Vec<T>,
9+
pub vec2: Vec<T>,
10+
}
11+
12+
impl IVectorComparer<String> for VectorComparer<String> {
13+
/// Initialize a new VectorComparer for type `String`
14+
///
15+
/// # Example
16+
///
17+
/// ```rust
18+
/// let vector_comparer: VectorComparer<String> = IVectorComparer::<String>::new(vec![], vec![]);
19+
/// ```
20+
///
21+
/// # Retuns
22+
///
23+
/// A `VectorComparer` that can be used to compare two `Vec` structs of type `String`
24+
fn new(vec1: Vec<String>, vec2: Vec<String>) -> VectorComparer<String> {
25+
VectorComparer::<String> { vec1, vec2 }
26+
}
27+
28+
/// Get the differences between the two given `Vec` structs of type `String`
29+
///
30+
/// # Example
31+
///
32+
/// ```rust
33+
/// let differences: Vec<String> = vector_comparer.get_differences();
34+
/// ```
35+
///
36+
/// # Returns
37+
///
38+
/// A `Vec` struct of type `String` that contains the differences between the two given `Vec` structs of type `String`
39+
fn get_differences(&self) -> Vec<String> {
40+
if self.vec1.is_empty() {
41+
return self.vec2.clone();
42+
} else if self.vec2.is_empty() {
43+
return self.vec1.clone();
44+
}
45+
46+
let mut diff = vec![];
47+
for f in &self.vec1 {
48+
let mut included = false;
49+
for d in &self.vec2 {
50+
if f.eq(d) {
51+
included = true;
52+
}
53+
}
54+
55+
if !included {
56+
diff.push(String::from(f));
57+
}
58+
}
59+
60+
for f in &self.vec2 {
61+
let mut included = false;
62+
for d in &self.vec1 {
63+
if f.eq(d) {
64+
included = true;
65+
}
66+
}
67+
68+
if !included {
69+
let n = String::from(f);
70+
if !diff.contains(&n) {
71+
diff.push(n);
72+
}
73+
}
74+
}
75+
76+
diff
77+
}
78+
}

src/view.rs

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::filereader::FileReader;
22
use crate::style;
3+
use crate::vector_comparer::{IVectorComparer, VectorComparer};
34
use iced::{alignment, scrollable, Rule};
45
use 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

3438
impl 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

Comments
 (0)