Skip to content

Commit

Permalink
add datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
bitoku committed Aug 2, 2020
1 parent 28a3cb6 commit 3d262ad
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ license = "MIT"

[dependencies]
clap = "2.33.1"
chrono = "0.4.13"
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> '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)
}

0 comments on commit 3d262ad

Please sign in to comment.