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

Commit 7c42763

Browse files
committed
* Added workflow for testing code format and tests
* Design takes up less space * Refactoring of existing code * Make use of native dialogs with improved translations
1 parent 07e1b8f commit 7c42763

File tree

8 files changed

+114
-89
lines changed

8 files changed

+114
-89
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ name: Deploy
33
on:
44
release:
55
types: [ published ]
6-
push:
7-
branches:
8-
- master
96

107
jobs:
118
build:
129
runs-on: ubuntu-latest
1310

1411
steps:
1512
- name: Checkout
16-
uses: actions/checkout@v1
13+
uses: actions/checkout@v3
1714

1815
- name: Install latest rust toolchain
1916
uses: actions-rs/toolchain@v1
@@ -39,7 +36,7 @@ jobs:
3936

4037
steps:
4138
- name: Checkout
42-
uses: actions/checkout@v1
39+
uses: actions/checkout@v3
4340

4441
- name: Install latest rust toolchain
4542
uses: actions-rs/toolchain@v1
@@ -64,7 +61,7 @@ jobs:
6461

6562
steps:
6663
- name: Checkout
67-
uses: actions/checkout@v1
64+
uses: actions/checkout@v3
6865

6966
- name: Install latest rust toolchain
7067
uses: actions-rs/toolchain@v1

.github/workflows/test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened]
6+
push:
7+
branches:
8+
- master
9+
- development
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
19+
- name: Install latest rust toolchain
20+
uses: actions-rs/toolchain@v1
21+
with:
22+
toolchain: stable
23+
default: true
24+
override: true
25+
26+
- name: Check code formatting
27+
run: cargo fmt --all -- --check
28+
29+
- name: Test code
30+
run: cargo test --verbose --all

Cargo.lock

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ authors = ["CodeDead <admin@codedead.com>"]
66
description = "A cross-platform GUI for comparing two text files"
77
readme = "README.md"
88
repository = "https://github.com/CodeDead/text-diff-rs"
9+
license = "GPL-3.0"
910
license-file = "LICENSE"
1011
keywords = ["gui", "ui", "text-diff", "interface", "codedead", "diff", "difference"]
1112

1213
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1314

1415
[dependencies]
1516
iced = { git = "https://github.com/iced-rs/iced" }
16-
native-dialog = "0.6.3"
17-
serde = { version = "1.0", features = ["derive"] }
17+
native-dialog = { git = "https://github.com/CodeDead/native-dialog-rs" }
18+
serde = { version = "1.0" }
1819
serde_json = "1.0"
1920

2021
[profile.release]
File renamed without changes.

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use iced::window;
22
use iced::{Sandbox, Settings};
33

4-
mod filereader;
4+
mod file_reader;
55
mod style;
66
mod vector_comparer;
77
mod vector_exporter;
@@ -11,7 +11,7 @@ pub fn main() -> iced::Result {
1111
view::ApplicationContext::run(Settings {
1212
id: Some(String::from("text-diff")),
1313
window: window::Settings {
14-
size: (800, 800),
14+
size: (800, 720),
1515
position: window::Position::Centered,
1616
..window::Settings::default()
1717
},

src/style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ mod dark {
383383
a: 0.8,
384384
..if is_checked { ACTIVE } else { SURFACE }
385385
}
386-
.into(),
386+
.into(),
387387
..self.active(is_checked)
388388
}
389389
}

src/view.rs

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ffi::OsStr;
22
use std::path::Path;
33

4-
use crate::filereader::FileReader;
4+
use crate::file_reader::FileReader;
55
use crate::style;
66
use crate::vector_comparer::{IVectorComparer, VectorComparer};
77
use crate::vector_exporter::{ExportType, IVectorExporter, VectorExporter};
@@ -41,6 +41,30 @@ pub struct ApplicationContext {
4141
pub has_compared: bool,
4242
}
4343

44+
impl ApplicationContext {
45+
/// Display a native alert
46+
///
47+
/// # Example
48+
///
49+
/// ```rust
50+
/// display_alert("hello", "world", MessageType::Info)
51+
/// ```
52+
///
53+
/// # Arguments
54+
///
55+
/// * `title` - The alert title
56+
/// * `content` - the content of the alert
57+
/// * `message_type` - The `MessageType` for the alert
58+
fn display_alert(&self, title: &str, content: &str, message_type: MessageType) {
59+
MessageDialog::new()
60+
.set_type(message_type)
61+
.set_title(title)
62+
.set_text(content)
63+
.show_alert()
64+
.unwrap();
65+
}
66+
}
67+
4468
impl Sandbox for ApplicationContext {
4569
type Message = Message;
4670

@@ -86,12 +110,12 @@ impl Sandbox for ApplicationContext {
86110
}
87111
Message::ComparePressed => {
88112
if self.first_file.is_empty() || self.second_file.is_empty() {
89-
MessageDialog::new()
90-
.set_type(MessageType::Warning)
91-
.set_title("text-diff")
92-
.set_text("Please select two files first!")
93-
.show_alert()
94-
.unwrap();
113+
ApplicationContext::display_alert(
114+
&self,
115+
"text-diff",
116+
"Please select two files first!",
117+
MessageType::Warning,
118+
);
95119
return;
96120
}
97121

@@ -103,31 +127,25 @@ impl Sandbox for ApplicationContext {
103127
let lines_first_file = match lines_first_file {
104128
Ok(d) => d,
105129
Err(e) => {
106-
MessageDialog::new()
107-
.set_type(MessageType::Error)
108-
.set_title("text-diff")
109-
.set_text(&format!(
110-
"Error while reading file {}!\n{}",
111-
&self.first_file, e
112-
))
113-
.show_alert()
114-
.unwrap();
130+
ApplicationContext::display_alert(
131+
&self,
132+
"text-diff",
133+
&format!("Error while reading file {}!\n{}", &self.first_file, e),
134+
MessageType::Error,
135+
);
115136
return;
116137
}
117138
};
118139

119140
let lines_second_file = match lines_second_file {
120141
Ok(d) => d,
121142
Err(e) => {
122-
MessageDialog::new()
123-
.set_type(MessageType::Error)
124-
.set_title("text-diff")
125-
.set_text(&format!(
126-
"Error while reading file {}!\n{}",
127-
&self.second_file, e
128-
))
129-
.show_alert()
130-
.unwrap();
143+
ApplicationContext::display_alert(
144+
&self,
145+
"text-diff",
146+
&format!("Error while reading file {}!\n{}", &self.second_file, e),
147+
MessageType::Error,
148+
);
131149
return;
132150
}
133151
};
@@ -179,23 +197,20 @@ impl Sandbox for ApplicationContext {
179197
Ok(_) => return,
180198
Err(e) => match e {
181199
crate::vector_exporter::ExportError::IoError(e) => {
182-
MessageDialog::new()
183-
.set_type(MessageType::Error)
184-
.set_title("text-diff")
185-
.set_text(&format!("Error while writing to file {}!\n{}", &path, e))
186-
.show_alert()
187-
.unwrap();
200+
ApplicationContext::display_alert(
201+
&self,
202+
"text-diff",
203+
&format!("Error while writing to file {}!\n{}", &path, e),
204+
MessageType::Error,
205+
);
188206
}
189207
crate::vector_exporter::ExportError::JsonError(e) => {
190-
MessageDialog::new()
191-
.set_type(MessageType::Error)
192-
.set_title("text-diff")
193-
.set_text(&format!(
194-
"Error while creating JSON for file {}!\n{}",
195-
&path, e
196-
))
197-
.show_alert()
198-
.unwrap();
208+
ApplicationContext::display_alert(
209+
&self,
210+
"text-diff",
211+
&format!("Error while creating JSON for file {}!\n{}", &path, e),
212+
MessageType::Error,
213+
);
199214
}
200215
},
201216
};
@@ -206,15 +221,11 @@ impl Sandbox for ApplicationContext {
206221
fn view(&mut self) -> Element<'_, Self::Message> {
207222
let title = Text::new("text-diff")
208223
.width(Length::Fill)
209-
.size(85)
210-
.color([0.5, 0.5, 0.5])
224+
.size(80)
211225
.horizontal_alignment(alignment::Horizontal::Center);
212226

213227
let choose_theme = style::Theme::ALL.iter().fold(
214-
Row::new()
215-
.width(Length::Fill)
216-
.align_items(Alignment::Center)
217-
.spacing(10),
228+
Row::new().width(Length::Fill).spacing(10),
218229
|row, theme| {
219230
row.push(
220231
Radio::new(
@@ -330,15 +341,16 @@ impl Sandbox for ApplicationContext {
330341
diff_text = Text::new("No differences detected!")
331342
}
332343

333-
let diff_column = self.differences.iter().fold(
334-
Column::new().spacing(10),
335-
|column, theme| column.push(Text::new(format!("- {}", theme))),
336-
);
344+
let diff_column = self
345+
.differences
346+
.iter()
347+
.fold(Column::new().spacing(10), |column, theme| {
348+
column.push(Text::new(format!("- {}", theme)))
349+
});
337350

338351
let scroll_container = Column::new().width(Length::Fill).push(diff_column);
339352
let scroll = Scrollable::new(&mut self.scrollable)
340-
.push(Container::new(scroll_container)
341-
.width(Length::Fill))
353+
.push(Container::new(scroll_container).width(Length::Fill))
342354
.max_height(150)
343355
.style(self.theme);
344356

@@ -357,19 +369,19 @@ impl Sandbox for ApplicationContext {
357369
.on_press(Message::ExportPressed)
358370
.style(self.theme);
359371

360-
content = content.push(
361-
Column::new()
362-
.width(Length::Fill)
363-
.align_items(Alignment::End)
364-
.spacing(20)
365-
.push(btn_export),
366-
);
372+
content = content
373+
.push(
374+
Column::new()
375+
.width(Length::Fill)
376+
.align_items(Alignment::End)
377+
.spacing(20)
378+
.push(btn_export),
379+
)
380+
.push(Rule::horizontal(20).style(self.theme));
367381
}
368382
}
369383

370-
content = content
371-
.push(Rule::horizontal(20).style(self.theme))
372-
.push(choose_theme);
384+
content = content.push(choose_theme);
373385

374386
Container::new(content)
375387
.width(Length::Fill)

0 commit comments

Comments
 (0)