tried to add a feature that would keep a streak
This commit is contained in:
106
src/main.rs
106
src/main.rs
@@ -1,9 +1,13 @@
|
||||
use chrono::{Datelike, Local};
|
||||
use chrono::{Datelike, Local, Duration};
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "A simple CLI tool example", long_about = None)]
|
||||
@@ -28,15 +32,108 @@ enum Commands {
|
||||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DailyPostMetadata {
|
||||
streak: u32,
|
||||
}
|
||||
|
||||
fn get_daily_filepath() -> Result<String, String> {
|
||||
let DAILY_FILEPATH = match env::var("VIMTODAY_DAILY_FILEPATH") {
|
||||
Ok(val) => val,
|
||||
Err(_e) => String::from("oops")
|
||||
};
|
||||
|
||||
return Ok(DAILY_FILEPATH);
|
||||
}
|
||||
|
||||
fn get_yesterday_filepath() -> Result<String, String> {
|
||||
let today = Local::now();
|
||||
let yesterday = today - Duration::days(1);
|
||||
let daily_filepath = get_daily_filepath().unwrap();
|
||||
|
||||
let today_date_str = format!("{}-{:02}-{:02}", yesterday.year(), yesterday.month(), yesterday.day());
|
||||
let file_name = format!("{}.md", &today_date_str);
|
||||
let folder_name = format!("{}/{}", &daily_filepath, yesterday.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 {
|
||||
return Ok(file_name);
|
||||
} else {
|
||||
return Err("File don't exist".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn read_daily_post_metadata(filepath: &str)-> Result<DailyPostMetadata, &str> {
|
||||
let mut reading_yaml: bool = false;
|
||||
let mut yaml_lines: Vec<String> = Vec::new();
|
||||
|
||||
for line in read_to_string(filepath).unwrap().lines() {
|
||||
let line_str = line.to_string();
|
||||
if line_str == "---" && reading_yaml == false {
|
||||
reading_yaml = true;
|
||||
yaml_lines.push(line_str);
|
||||
} else if line_str == "---" && reading_yaml == true {
|
||||
yaml_lines.push(line_str);
|
||||
break;
|
||||
} else if reading_yaml == true {
|
||||
yaml_lines.push(line_str);
|
||||
}
|
||||
}
|
||||
|
||||
let combined_yaml = yaml_lines.join("\\n");
|
||||
let metadata = serde_yaml::from_str(&combined_yaml.to_string());
|
||||
return match metadata {
|
||||
Ok(metadata) => Ok(metadata),
|
||||
Err(_e) => Err("No metadata found")
|
||||
};
|
||||
}
|
||||
|
||||
fn update_streak(daily_filepath: &str, file: &mut File) {
|
||||
// Get filepath from yesterday. If does not exist, return 0.
|
||||
let mut streak: u32 = 0;
|
||||
let yesterday_filepath = get_yesterday_filepath();
|
||||
|
||||
if let Err(e) = yesterday_filepath {
|
||||
let today_metadata = DailyPostMetadata {
|
||||
streak: 1,
|
||||
};
|
||||
|
||||
let stringified_yaml = serde_yaml::to_string(&today_metadata).unwrap();
|
||||
file.write_all(stringified_yaml.as_bytes()).expect("Unable to write YAML metadata");
|
||||
return ();
|
||||
}
|
||||
|
||||
let filepath = yesterday_filepath.unwrap();
|
||||
let metadata: Result<DailyPostMetadata, &str> = read_daily_post_metadata(&filepath);
|
||||
|
||||
// Get metadata from yesterday's post. If metadata does not exist, return 1.
|
||||
if let Err(e) = metadata {
|
||||
let today_metadata = DailyPostMetadata {
|
||||
streak: 2,
|
||||
};
|
||||
|
||||
let stringified_yaml = serde_yaml::to_string(&today_metadata).unwrap();
|
||||
file.write_all(stringified_yaml.as_bytes()).expect("Unable to write YAML metadata");
|
||||
return ();
|
||||
}
|
||||
|
||||
let prev_metadata: DailyPostMetadata = metadata.unwrap();
|
||||
let today_metadata = DailyPostMetadata {
|
||||
streak: prev_metadata.streak + 1,
|
||||
};
|
||||
|
||||
let stringified_yaml = serde_yaml::to_string(&today_metadata).unwrap();
|
||||
file.write_all(stringified_yaml.as_bytes()).expect("Unable to write YAML metadata");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let today = Local::now();
|
||||
let today_date_str = format!("{}-{:02}-{:02}", today.year(), today.month(), today.day());
|
||||
let DAILY_FILEPATH = get_daily_filepath().unwrap();
|
||||
|
||||
let file_path = format!("{}/{}/{}.md", &DAILY_FILEPATH, today.year(), &today_date_str);
|
||||
// Check if file is there already
|
||||
@@ -50,6 +147,9 @@ fn main() {
|
||||
// Create a new file
|
||||
let mut file = File::create(&file_path).unwrap();
|
||||
|
||||
// Write YAML
|
||||
let curent_streak = update_streak(&DAILY_FILEPATH, &mut file);
|
||||
|
||||
// Write date at the start
|
||||
let date_header = format!("#{}", &today_date_str);
|
||||
writeln!(&mut file, "{}", &date_header);
|
||||
|
||||
Reference in New Issue
Block a user