Skip to content

Commit

Permalink
Merge pull request #8 from wujackwill/nightly
Browse files Browse the repository at this point in the history
feat: add todo-cli-app sync
  • Loading branch information
wujackwill authored Feb 9, 2024
2 parents fd92608 + e2db8b8 commit 778d8a2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 19 deletions.
47 changes: 46 additions & 1 deletion Cargo.lock

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

35 changes: 18 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
[package]
name = "todo-cli-app"
version = "0.3.3"
edition = "2021"
authors = ["Jack Will <[email protected]>"]
license = "MIT"
description = "yet another todo CLI app written in Rust"
readme = "README.md"
homepage = "https://github.com/wujackwill/todo"
repository = "https://github.com/wujackwill/todo"
keywords = ["cli"]
categories = ["command-line-utilities"]


[dependencies]
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] }
[package]
name = "todo-cli-app"
version = "0.4.0"
edition = "2021"
authors = ["Jack Will <[email protected]>"]
license = "MIT"
description = "yet another todo CLI app written in Rust"
readme = "README.md"
homepage = "https://github.com/wujackwill/todo"
repository = "https://github.com/wujackwill/todo"
keywords = ["cli"]
categories = ["command-line-utilities"]


[dependencies]
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] }
regex = "1.10.3"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Commands:
clear clear all tasks
sort sort tasks
edit edit a task
sync add tags for the tasks sync from other device
help Print this message or the help of the given subcommand(s)
Options:
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub enum Commands {

/// edit a task
EDIT { number: usize },

/// add tags for the tasks sync from other device
SYNC {},
}

pub fn check_file(file_path: &str) -> Result<File> {
Expand Down
39 changes: 38 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::prelude::*;
use std::io::{BufRead, BufReader};
use std::{env, io};
use todo_cli_app::*;
use regex::Regex;

fn main() {
let args = Cli::parse();
Expand Down Expand Up @@ -181,11 +182,47 @@ fn main() {
}

Err(e) => println!("Error: {}", e),
},

}
Commands::SYNC {} => {
// Read the file and create a buffered reader
let file = File::open(&file_path).expect("Unable to open file");
let reader = BufReader::new(file);
let mut contents = String::new();
let mut line_number = 0;
let re_skip = Regex::new(r"^\[x\]").unwrap();
let re_num = Regex::new(r"^\[(\d+)\]").unwrap();

// Iterate over each line in the file
for line in reader.lines() {
// Unwrap the line or exit if there's an error
let line = line.expect("Unable to read line");

if re_skip.is_match(&line) {
// If line starts with "[x]", append it to contents without modifying
contents.push_str(&line);
} else {
// If the line doesn't start with "[x]", increment line number and prepend it with the new line number
line_number += 1;
let line_with_number = if let Some(caps) = re_num.captures(&line) {
// If the line already has a number, replace it with the new line number
format!("[{}] {}", line_number, &line[caps[1].len() + 2..].trim_start())
} else {
// If the line doesn't have a number, add the new line number
format!("[{}] {}", line_number, line.trim_start())
};
contents.push_str(&line_with_number);
}

// Add a newline character to the end of the line
contents.push('\n');
}
fs::write(&file_path, contents).expect("Unable to write file");

let contents =
fs::read_to_string(&file_path).expect("Should have been able to read the file");

println!("{contents}");
}
}
}

0 comments on commit 778d8a2

Please sign in to comment.