Skip to content

Commit

Permalink
#92 - forbid running rm,done,undone,edit,postpone without specifying …
Browse files Browse the repository at this point in the history
…tasks to process
  • Loading branch information
VladimirMarkelov committed Mar 1, 2024
1 parent 8a60865 commit 0e001df
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 48 deletions.
91 changes: 44 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ttdl"
version = "4.2.0"
version = "4.2.1"
authors = ["Vladimir Markelov <vmatroskin@gmail.com>"]
edition = "2021"
keywords = ["todotxt", "terminal", "cli", "todo", "tasks"]
Expand Down
8 changes: 8 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ttdl (4.2.1) unstable; urgency=medium

* Forbid executing some operations if no filter is specified in command
line. When running without specifying what tasks to process, the commands
rm, edit, postpone, done, undone interrupt the application with an error

-- Vladimir Markelov <vmatroskin@gmail.com> Thu, 29 Feb 2024 19:23:11 -0800

ttdl (4.2.0) unstable; urgency=medium

* New recurrence interval: business days.
Expand Down
62 changes: 62 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ fn task_is_hidden(task: &todotxt::Task) -> bool {
}
}

fn is_filter_empty(flt: &tfilter::Conf) -> bool {
if flt.regex.is_some() || flt.due.is_some() || flt.thr.is_some() || flt.rec.is_some() {
return false;
}
if flt.pri.is_some() || flt.tmr.is_some() || flt.created.is_some() || flt.finished.is_some() {
return false;
}
if let tfilter::ItemRange::None = flt.range {
} else {
return false;
}
if let tfilter::TodoStatus::Active = flt.all {
} else {
return false;
}
if !flt.include.projects.is_empty()
|| !flt.include.contexts.is_empty()
|| !flt.include.tags.is_empty()
|| !flt.include.hashtags.is_empty()
{
return false;
}
if !flt.exclude.projects.is_empty()
|| !flt.exclude.contexts.is_empty()
|| !flt.exclude.tags.is_empty()
|| !flt.exclude.hashtags.is_empty()
{
return false;
}
true
}

fn filter_tasks(tasks: &[todotxt::Task], c: &conf::Conf) -> todo::IDVec {
let mut todos = tfilter::filter(tasks, &c.flt);
if !c.show_hidden {
Expand Down Expand Up @@ -275,6 +307,13 @@ fn task_list_calendar(stdout: &mut StandardStream, tasks: &todo::TaskSlice, conf
}

fn task_done(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf::Conf) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(
stdout,
"Warning: you are going to mark all the tasks 'done'. Please specify tasks to complete."
)?;
std::process::exit(1);
}
let processed = process_tasks(stdout, tasks, conf, "completed", todo::done)?;
if processed {
if let Err(e) = todo::save(tasks, Path::new(&conf.todo_file)) {
Expand All @@ -286,6 +325,13 @@ fn task_done(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf
}

fn task_undone(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf::Conf) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(
stdout,
"Warning: you are going to undone all the tasks completed tasks. Please specify tasks to undone."
)?;
std::process::exit(1);
}
let mut flt_conf = conf.clone();
if flt_conf.flt.all == tfilter::TodoStatus::Active {
flt_conf.flt.all = tfilter::TodoStatus::Done;
Expand All @@ -302,6 +348,10 @@ fn task_undone(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &co
}

fn task_remove(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf::Conf) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(stdout, "Warning: deletion of all tasks requested. Please specify tasks to delete.")?;
std::process::exit(1);
}
let mut flt_conf = conf.clone();
if flt_conf.flt.all == tfilter::TodoStatus::Active {
flt_conf.flt.all = tfilter::TodoStatus::All;
Expand Down Expand Up @@ -378,6 +428,10 @@ fn task_clean(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &con
}

fn task_edit(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf::Conf) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(stdout, "Warning: modifying of all tasks requested. Please specify tasks to edit.")?;
std::process::exit(1);
}
let todos = filter_tasks(tasks, conf);
let action = "changed";
if todos.is_empty() {
Expand Down Expand Up @@ -425,6 +479,10 @@ fn task_add_text(
conf: &conf::Conf,
to_end: bool,
) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(stdout, "Warning: you are going to add text to all tasks. Please specify tasks to modify.")?;
std::process::exit(1);
}
let subj = match &conf.todo.subject {
None => {
writeln!(stdout, "Subject is empty")?;
Expand Down Expand Up @@ -539,6 +597,10 @@ fn task_start_stop(
}

fn task_postpone(stdout: &mut StandardStream, tasks: &mut todo::TaskVec, conf: &conf::Conf) -> io::Result<()> {
if is_filter_empty(&conf.flt) {
writeln!(stdout, "Warning: postponing of all tasks requested. Please specify tasks to postpone.")?;
std::process::exit(1);
}
let subj = match conf.todo.subject {
Some(ref s) => s,
None => {
Expand Down

0 comments on commit 0e001df

Please sign in to comment.