Skip to content

Commit

Permalink
Merge pull request #13 from jfding/new_cmd_import
Browse files Browse the repository at this point in the history
New cmd import to extract tasks from any specified markdown file
  • Loading branch information
jfding authored Sep 16, 2024
2 parents 1baaa2f + 5c97c65 commit d86fac1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ pub enum Commands {

/// -> postpone all uncompeleted of today to INBOX
Postp,

/// -> import uncompeleted task in any markdown file to current
Import{
#[arg(short, long)]
file: String,
},
}

impl Default for Cli {
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ fn main() {
Some(Commands::Postp) => {
TaskBox::postp(inbox_path.clone().as_path().parent().unwrap(), inbox_path)
}

Some(Commands::Import{ file }) => {
let mut todo = TaskBox::new(inbox_path);
todo.import(file)
}
}
}
39 changes: 33 additions & 6 deletions src/taskbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;
use std::path::{Path, PathBuf};
use regex::Regex;
use colored::Colorize;

use chrono::*;
use std::ops::*;
Expand All @@ -27,7 +28,7 @@ pub struct TaskBox {

impl TaskBox {
pub fn new (fpath: PathBuf) -> Self {
let title = fpath.file_stem().and_then(|s| s.to_str()).unwrap_or("TODO").to_string();
let title = fpath.file_stem().and_then(|s| s.to_str()).unwrap().to_string();

if !fpath.exists() {
fs::File::create(&fpath).expect("Failed to create file");
Expand Down Expand Up @@ -100,16 +101,13 @@ impl TaskBox {
}

fn _move_in(&mut self, todo_in: &mut TaskBox) {
use colored::Colorize;
fn __friendly_name(name_in: Option<&String>) -> &str {
if Some(&get_today()) == name_in {
"today"
} else if Some(&get_tomorrow()) == name_in {
"tomorrow"
} else if Some(&get_yesterday()) == name_in {
"yesterday"
} else if Some("TODO") == name_in.map(|x| x.as_str()) {
"Inbox"
} else {
name_in.map(|x| x.as_str()).unwrap()
}
Expand All @@ -123,10 +121,10 @@ impl TaskBox {

println!("{}  {} ↩️",
__friendly_name(todo_in.title.as_ref()).green(),
__friendly_name(self.title.as_ref()).red());
__friendly_name(self.title.as_ref()).blue());

for task in tasks {
println!(" 󰄗 {}", task);
println!("{} : {}", " 󰄗".to_string().red(), task);
self.tasks.push((task.clone(), false));
}

Expand Down Expand Up @@ -251,4 +249,33 @@ impl TaskBox {
let mut todo = TaskBox::new(inbox_path);
todo._move_in(&mut today_todo)
}

// specified markdown file -> cur
pub fn import(&mut self, mdfile: String) {
let fpath = Path::new(&mdfile);
if ! fpath.is_file() {
eprintln!("not a file or not exists: {}", mdfile.red());
std::process::exit(1)
}
println!("importing {} ↩️", mdfile.purple());

let mut newt = Vec::new();
for rline in fs::read_to_string(fpath).expect("cannot read file").lines() {
let line = rline.trim();
if line.is_empty() { continue }

if line.starts_with(PREFIX) {
println!("{} : {}", " 󰄗".to_string().red(), &line[6..]);
newt.push((line[6..].to_string(), false))
}
}

if newt.is_empty() {
println!("{} found", "nothing".to_string().yellow())
} else {
self._load();
self.tasks.append(&mut newt);
self._dump();
}
}
}
3 changes: 2 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cmd_lib::*;
use colored::Colorize;

const DATA_BASE : &str = ".local/share/todor";
const INBOX_NAME :&str = "INBOX";

pub fn get_inbox_file(dir: Option<String>, inbox: Option<String>) -> PathBuf {
let base_path = dir.map(PathBuf::from).unwrap_or_else(|| {
Expand All @@ -15,7 +16,7 @@ pub fn get_inbox_file(dir: Option<String>, inbox: Option<String>) -> PathBuf {
});
fs::create_dir_all(&base_path).expect("Failed to create base directory");

base_path.join(inbox.unwrap_or("TODO".to_string())).with_extension("md")
base_path.join(inbox.unwrap_or(INBOX_NAME.to_string())).with_extension("md")
}

pub fn glance_all(inbox_path: &Path) {
Expand Down

0 comments on commit d86fac1

Please sign in to comment.