Skip to content

Commit f0b8b13

Browse files
authored
Popup and error messages (#13)
* Add a popup that shows main commands * Display an error when the date is in a wrong format * Refactoring * Updated documentation
1 parent 12881d6 commit f0b8b13

File tree

17 files changed

+208
-142
lines changed

17 files changed

+208
-142
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "task_rustler"
3-
version = "0.2.4"
3+
version = "0.3.1"
44
authors = ["r366y"]
55
edition = "2021"
66

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
---
1616
### Commands
17-
18-
- __e__ insert a new task
17+
- __h__ help
18+
![help popup](/pics/help.png)
19+
- __a__ insert a new task
1920
![insert task](/pics/add_task.png)
2021
- __m__ modify selected task
2122
![modify task](/pics/modify_task.png)

pics/add_task.png

58.5 KB
Loading

pics/help.png

245 KB
Loading

pics/modify_task.png

66 KB
Loading

pics/task_list.png

-66.8 KB
Loading

src/app.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl TaskList {
1818
#[derive(Debug)]
1919
pub enum InputMode {
2020
Normal,
21-
Editing,
21+
Adding,
2222
EditingExisting,
2323
}
2424
#[derive(Debug)]
@@ -36,6 +36,9 @@ pub struct App {
3636
pub input_mode: InputMode,
3737
pub input_field: InputField,
3838
pub tasks_service: TasksService,
39+
pub show_popup: bool,
40+
pub error_message: String,
41+
pub is_error: bool,
3942
}
4043

4144
impl App {
@@ -48,6 +51,9 @@ impl App {
4851
input_mode: InputMode::Normal,
4952
input_field: InputField::Title,
5053
tasks_service: TasksService::new(db_path),
54+
show_popup: false,
55+
error_message: String::new(),
56+
is_error: false,
5157
}
5258
}
5359

src/command.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::app::{App, InputField, InputMode};
2-
use crate::date::{Date, DATE_FORMAT};
2+
use crate::date::{TaskDate, DATE_FORMAT};
33
use crate::task::Task;
44
use anyhow::{Context, Result};
55

66
pub trait Command {
7-
fn execute(&mut self, app: &mut App)-> Result<()>;
7+
fn execute(&mut self, app: &mut App) -> Result<()>;
88
}
99

1010
pub struct EnterEditModeCommand;
1111

1212
impl Command for EnterEditModeCommand {
1313
fn execute(&mut self, app: &mut App) -> Result<()> {
14-
app.input_mode = InputMode::Editing;
14+
app.input_mode = InputMode::Adding;
1515
app.input_field = InputField::Title;
1616
Ok(())
1717
}
@@ -23,11 +23,12 @@ pub struct AddTaskCommand;
2323
impl Command for AddTaskCommand {
2424
fn execute(&mut self, app: &mut App) -> Result<()> {
2525
let mut t = Task::new();
26-
t.title = app.input_title.drain(..).collect();
27-
t.description = app.input_description.drain(..).collect();
2826
if !app.input_date.is_empty() {
29-
t.date = Date::try_from(app.input_date.drain(..).collect::<String>()).context("Invalid date")?;
27+
t.date = TaskDate::try_from(app.input_date.drain(..).collect::<String>())
28+
.context("Invalid date format, use dd-mm-yyyy")?;
3029
}
30+
t.title = app.input_title.drain(..).collect();
31+
t.description = app.input_description.drain(..).collect();
3132
app.tasks_service.add_new_task(&t);
3233
app.refresh_task_list();
3334
Ok(())
@@ -76,8 +77,11 @@ impl Command for StartEditingExistingTaskCommand {
7677
if let Some(index) = app.task_list.state.selected() {
7778
app.input_title = app.task_list.items[index].title.clone();
7879
app.input_description = app.task_list.items[index].description.clone();
79-
app.input_date = app.task_list.items[index].date.clone().0.map(
80-
|d| d.format(DATE_FORMAT).to_string())
80+
app.input_date = app.task_list.items[index]
81+
.date
82+
.clone()
83+
.0
84+
.map(|d| d.format(DATE_FORMAT).to_string())
8185
.unwrap_or(String::new());
8286
app.input_mode = InputMode::EditingExisting;
8387
app.input_field = InputField::Title;
@@ -92,16 +96,17 @@ pub struct FinishEditingExistingTaskCommand;
9296
impl Command for FinishEditingExistingTaskCommand {
9397
fn execute(&mut self, app: &mut App) -> Result<()> {
9498
if let Some(index) = app.task_list.state.selected() {
95-
app.task_list.items[index].title = app.input_title.drain(..).collect();
96-
app.task_list.items[index].description = app.input_description.drain(..).collect();
9799
if !app.input_date.is_empty() {
98-
app.task_list.items[index].date = Date::try_from(app.input_date.drain(..).collect::<String>())?;
100+
app.task_list.items[index].date =
101+
TaskDate::try_from(app.input_date.drain(..).collect::<String>())
102+
.context("Invalid date format, use dd-mm-yyyy")?;
99103
} else {
100-
app.task_list.items[index].date = Date(None)
104+
app.task_list.items[index].date = TaskDate(None)
101105
}
106+
app.task_list.items[index].title = app.input_title.drain(..).collect();
107+
app.task_list.items[index].description = app.input_description.drain(..).collect();
102108
app.tasks_service.update_task(&app.task_list.items[index])
103109
}
104-
app.input_mode = InputMode::Normal;
105110
Ok(())
106111
}
107112
}

src/date.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1+
use anyhow::Result;
12
use chrono::NaiveDate;
2-
use anyhow::{ Result};
33

44
pub const DATE_FORMAT: &str = "%d-%m-%Y";
55
#[derive(Debug, Eq, PartialEq, Clone)]
6-
pub struct Date(pub Option<NaiveDate>);
6+
pub struct TaskDate(pub Option<NaiveDate>);
77

8-
impl TryFrom<String> for Date {
8+
impl TryFrom<String> for TaskDate {
99
type Error = chrono::ParseError;
1010

1111
fn try_from(value: String) -> Result<Self, Self::Error> {
1212
match NaiveDate::parse_from_str(&value, DATE_FORMAT) {
13-
Ok(date) => Ok(Date(Some(date))),
13+
Ok(date) => Ok(TaskDate(Some(date))),
1414
Err(e) => Err(e),
1515
}
1616
}
1717
}
1818

19-
impl TryFrom<Date> for String {
19+
impl TryFrom<TaskDate> for String {
2020
type Error = &'static str;
2121

22-
fn try_from(value: Date) -> Result<Self, Self::Error> {
22+
fn try_from(value: TaskDate) -> Result<Self, Self::Error> {
2323
match value.0 {
2424
Some(date) => Ok(date.format(DATE_FORMAT).to_string()),
2525
None => Err("Cannot convert None to String"),
2626
}
2727
}
28-
}
28+
}

0 commit comments

Comments
 (0)