diff --git a/Cargo.lock b/Cargo.lock index 0cedd17..aed848c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,12 +20,29 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "chrono" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + [[package]] name = "clap" version = "2.33.1" @@ -56,10 +73,30 @@ version = "0.2.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701" +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +dependencies = [ + "autocfg", +] + [[package]] name = "simplememo" version = "0.1.0" dependencies = [ + "chrono", "clap", ] @@ -78,6 +115,16 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "unicode-width" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index 5e5dc5d..d8fba72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ license = "MIT" [dependencies] clap = "2.33.1" +chrono = "0.4.13" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b38f43c..28bc206 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,28 +2,47 @@ extern crate clap; use clap::{App}; use std::fs::OpenOptions; use std::io::Write; +use std::env; +use chrono::Local; fn main() -> std::io::Result<()>{ + let memofile = match env::var("MEMO_FILE") { + Ok(x) => x, + Err(_) => "note.txt".to_string(), + }; + let date_format = match env::var("MEMO_DATE_FORMAT") { + Ok(x) => x, + Err(_) => "%Y/%m/%d %H:%M:%S".to_string(), + }; let matches = App::new("memo") .about(clap::crate_description!()) .version(clap::crate_version!()) .author(clap::crate_authors!()) .args_from_usage(" 'sentence you want to note'") .get_matches(); - let sentence = match matches.value_of("sentence") { - Some(x) => x, + Some(x) => x.to_string(), None => { eprintln!("empty sentence"); std::process::exit(1); }, }; + let datetime = Local::now().format(&date_format).to_string(); + let sentence = join_sentence(datetime, sentence); + memo(memofile, sentence)?; + Ok(()) +} +fn memo(memofile: String, sentence: String) -> std::io::Result<()> { let mut file = OpenOptions::new() .append(true) .create(true) - .open("note.txt")?; + .open(memofile)?; write!(file, "{}\n", sentence)?; file.flush()?; - Ok(()) + return Ok(()); +} + +fn join_sentence(sentence1: String, sentence2: String) -> String { + format!("{} {}", sentence1, sentence2) }