Adding create date header function
This commit is contained in:
19
src/main.rs
19
src/main.rs
@@ -2,6 +2,8 @@ use chrono::{Datelike, Local};
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about = "A simple CLI tool example", long_about = None)]
|
#[command(author, version, about = "A simple CLI tool example", long_about = None)]
|
||||||
@@ -36,10 +38,23 @@ fn main() {
|
|||||||
let today = Local::now();
|
let today = Local::now();
|
||||||
let today_date_str = format!("{}-{:02}-{:02}", today.year(), today.month(), today.day());
|
let today_date_str = format!("{}-{:02}-{:02}", today.year(), today.month(), today.day());
|
||||||
|
|
||||||
|
|
||||||
let file_path = format!("{}/{}/{}.md", &DAILY_FILEPATH, today.year(), &today_date_str);
|
let file_path = format!("{}/{}/{}.md", &DAILY_FILEPATH, today.year(), &today_date_str);
|
||||||
|
// Check if file is there already
|
||||||
|
let file_name = format!("{}.md", &today_date_str);
|
||||||
|
let folder_name = format!("{}/{}", &DAILY_FILEPATH, today.year());
|
||||||
|
|
||||||
|
let mut directory_iterator = std::fs::read_dir(&folder_name).unwrap();
|
||||||
|
let file_exists = directory_iterator.any(|filename| *filename.unwrap().file_name() == *file_name);
|
||||||
|
|
||||||
|
if !file_exists {
|
||||||
|
// Create a new file
|
||||||
|
let mut file = File::create(&file_path).unwrap();
|
||||||
|
|
||||||
|
// Write date at the start
|
||||||
|
let date_header = format!("#{}", &today_date_str);
|
||||||
|
writeln!(&mut file, "{}", &date_header);
|
||||||
|
}
|
||||||
|
|
||||||
println!("vim {}", file_path);
|
|
||||||
|
|
||||||
Command::new("vim")
|
Command::new("vim")
|
||||||
.arg(&file_path)
|
.arg(&file_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user