Skip to content

Commit

Permalink
Merge pull request #15 from tsoding/notifications
Browse files Browse the repository at this point in the history
Allow none existing input files and add notifications
  • Loading branch information
rexim authored Jun 23, 2021
2 parents 9844e23 + 7d39f18 commit 8e90a0c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ jobs:
with:
command: build
args: --release --all-features
env:
RUSTFLAGS: -D warnings
- uses: actions-rs/cargo@v1
with:
command: test
env:
RUSTFLAGS: -D warnings
- uses: actions-rs/cargo@v1
with:
command: clippy
env:
RUSTFLAGS: -D warnings
- uses: actions-rs/cargo@v1
with:
command: fmt
Expand Down
114 changes: 72 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ncurses::*;
use std::cmp;
use std::env;
use std::fs::File;
use std::io::{self, BufRead, Write};
use std::io::{self, BufRead, ErrorKind, Write};
use std::ops::{Add, Mul};
use std::process;

Expand Down Expand Up @@ -202,7 +202,7 @@ fn list_first(list_curr: &mut usize) {
}

fn list_last(list: &[String], list_curr: &mut usize) {
if list.len() > 0 {
if !list.is_empty() {
*list_curr = list.len() - 1;
}
}
Expand All @@ -220,10 +220,10 @@ fn list_transfer(
}
}

fn load_state(todos: &mut Vec<String>, dones: &mut Vec<String>, file_path: &str) {
let file = File::open(file_path).unwrap();
fn load_state(todos: &mut Vec<String>, dones: &mut Vec<String>, file_path: &str) -> io::Result<()> {
let file = File::open(file_path)?;
for (index, line) in io::BufReader::new(file).lines().enumerate() {
match parse_item(&line.unwrap()) {
match parse_item(&line?) {
Some((Status::Todo, title)) => todos.push(title.to_string()),
Some((Status::Done, title)) => dones.push(title.to_string()),
None => {
Expand All @@ -232,6 +232,7 @@ fn load_state(todos: &mut Vec<String>, dones: &mut Vec<String>, file_path: &str)
}
}
}
Ok(())
}

fn save_state(todos: &[String], dones: &[String], file_path: &str) {
Expand Down Expand Up @@ -269,7 +270,21 @@ fn main() {
let mut dones = Vec::<String>::new();
let mut done_curr: usize = 0;

load_state(&mut todos, &mut dones, &file_path);
let mut notification: String;

match load_state(&mut todos, &mut dones, &file_path) {
Ok(()) => notification = format!("Loaded file {}", file_path),
Err(error) => {
if error.kind() == ErrorKind::NotFound {
notification = format!("New file {}", file_path)
} else {
panic!(
"Could not load state from file `{}`: {:?}",
file_path, error
);
}
}
};

initscr();
noecho();
Expand All @@ -290,55 +305,63 @@ fn main() {
let mut y = 0;
getmaxyx(stdscr(), &mut y, &mut x);

ui.begin(Vec2::new(0, 0), LayoutKind::Horz);
ui.begin(Vec2::new(0, 0), LayoutKind::Vert);
{
ui.begin_layout(LayoutKind::Vert);
ui.label_fixed_width(&notification, x, REGULAR_PAIR);
notification.clear();
ui.label_fixed_width("", x, REGULAR_PAIR);

ui.begin_layout(LayoutKind::Horz);
{
ui.label_fixed_width(
"TODO",
x / 2,
if panel == Status::Todo {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
for (index, todo) in todos.iter().enumerate() {
ui.begin_layout(LayoutKind::Vert);
{
ui.label_fixed_width(
&format!("- [ ] {}", todo),
"TODO",
x / 2,
if index == todo_curr && panel == Status::Todo {
if panel == Status::Todo {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
for (index, todo) in todos.iter().enumerate() {
ui.label_fixed_width(
&format!("- [ ] {}", todo),
x / 2,
if index == todo_curr && panel == Status::Todo {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
}
}
}
ui.end_layout();
ui.end_layout();

ui.begin_layout(LayoutKind::Vert);
{
ui.label_fixed_width(
"DONE",
x / 2,
if panel == Status::Done {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
for (index, done) in dones.iter().enumerate() {
ui.begin_layout(LayoutKind::Vert);
{
ui.label_fixed_width(
&format!("- [x] {}", done),
"DONE",
x / 2,
if index == done_curr && panel == Status::Done {
if panel == Status::Done {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
for (index, done) in dones.iter().enumerate() {
ui.label_fixed_width(
&format!("- [x] {}", done),
x / 2,
if index == done_curr && panel == Status::Done {
HIGHLIGHT_PAIR
} else {
REGULAR_PAIR
},
);
}
}
ui.end_layout();
}
ui.end_layout();
}
Expand Down Expand Up @@ -370,12 +393,18 @@ fn main() {
Status::Done => list_first(&mut done_curr),
},
'G' => match panel {
Status::Todo => list_last(&mut todos, &mut todo_curr),
Status::Done => list_last(&mut dones, &mut done_curr),
Status::Todo => list_last(&todos, &mut todo_curr),
Status::Done => list_last(&dones, &mut done_curr),
},
'\n' => match panel {
Status::Todo => list_transfer(&mut dones, &mut todos, &mut todo_curr),
Status::Done => list_transfer(&mut todos, &mut dones, &mut done_curr),
Status::Todo => {
list_transfer(&mut dones, &mut todos, &mut todo_curr);
notification.push_str("DONE!")
}
Status::Done => {
list_transfer(&mut todos, &mut dones, &mut done_curr);
notification.push_str("No, not done yet...")
}
},
'\t' => {
panel = panel.toggle();
Expand All @@ -386,7 +415,8 @@ fn main() {
}
}

save_state(&todos, &dones, &file_path);

endwin();

save_state(&todos, &dones, &file_path);
println!("Saved state to {}", file_path);
}

0 comments on commit 8e90a0c

Please sign in to comment.