Skip to content

Commit

Permalink
hack way to fix 'mark' bug which cannot handle subtask well
Browse files Browse the repository at this point in the history
append spaces to subtasks, which will not be displayed,
but different length of trailing spaces means belonging to
different upper level tasks.

meanwhile benefits purge cmd as well
  • Loading branch information
jfding committed Sep 17, 2024
1 parent 06fb80f commit 2fd8d47
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/taskbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ impl TaskBox {
let mut tasks = Vec::new();
let mut title = String::new();

let mut postfix_sub = String::new();
let mut last_is_sub = false;

for (index, rline) in fs::read_to_string(&self.fpath)
.expect("Failed to read file")
.lines().enumerate() {
Expand All @@ -86,19 +89,28 @@ impl TaskBox {
if index == 0 {
title = line.trim_start_matches("# ").to_string();

} else if let Some(stripped) = line.strip_prefix(PREFIX) {
tasks.push((stripped.to_string(), false))
} else if let Some(stripped) = line.strip_prefix(PREFIX_DONE) {
tasks.push((stripped.to_string(), true))
} else if line.starts_with("- [") {
if let Some(stripped) = line.strip_prefix(PREFIX) {
tasks.push((stripped.to_string(), false))
} else if let Some(stripped) = line.strip_prefix(PREFIX_DONE) {
tasks.push((stripped.to_string(), true))
} else { continue }

if last_is_sub {
last_is_sub = false;
postfix_sub += " "; // hack way to identify sub-tasks belong to diff task
}
} else {
// might be sub-tasks
let line = line.trim_start();

if let Some(stripped) = line.strip_prefix(PREFIX) {
tasks.push((SUB_PREFIX.to_owned() + stripped, false))
tasks.push((SUB_PREFIX.to_owned() + stripped + &postfix_sub, false))
} else if let Some(stripped) = line.strip_prefix(PREFIX_DONE) {
tasks.push((SUB_PREFIX.to_owned() + stripped, true))
}
tasks.push((SUB_PREFIX.to_owned() + stripped + &postfix_sub, true))
} else { continue }

last_is_sub = true;
}
}

Expand All @@ -112,7 +124,7 @@ impl TaskBox {
for (mut task, done) in self.tasks.clone() {
if let Some(left) = task.strip_prefix(SUB_PREFIX) {
content.push_str(" ");
task = left.to_owned();
task = left.trim_end().to_string();
}

if done {
Expand Down

0 comments on commit 2fd8d47

Please sign in to comment.