Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Emre YILMAZ <[email protected]>
  • Loading branch information
delirehberi committed May 27, 2024
1 parent 6884368 commit f2a3ae8
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@ struct Config{
const APP_NAME: &str = "tiller";

fn main() -> io::Result<()> {
let config_path = dirs::home_dir()
.expect("failed to find home dir")
.join(format!(".config/{}/config.json",APP_NAME))
;
let config_data = read_to_string(config_path)?;
let config:Config = from_str(&config_data).expect("Invalid config format");
let config = config()?;

let Config { editor, til_folder, repo_path } = config;
let full_til_path = format!("{}/{}",repo_path,til_folder);

let temp_file = NamedTempFile::new()?;
let temp_path = temp_file.path().to_str().unwrap().to_string();

Command::new(editor)
.arg(&temp_path)
.status()
.expect("Failed to open editor");

let input_content = read_to_string(temp_path)?;
let input_content = get_content(editor)?;
if input_content.len() < 1 {
println!("You can't save an empty til!");
std::process::exit(1)
}

let new_file_name = get_next_file_name(&full_til_path)?;

Expand All @@ -50,6 +41,31 @@ fn main() -> io::Result<()> {
let mut new_file = File::create(&new_file_path)?;
write!(new_file,"{} \n {}", prepend_content, input_content)?;

publish(&new_file_path,&repo_path,&new_file_name)
}

fn get_next_file_name(folder: &str) -> io::Result<String> {
let paths = fs::read_dir(folder)?
.filter_map(Result::ok)
.filter(|e| e.path().extension().and_then(|s| s.to_str())==Some("md"))
.collect::<Vec<_>>();
let max_num = paths.iter()
.filter_map(|e| e.path().file_stem().and_then(|s| s.to_str()).and_then(|s| s.parse::<u8>().ok()))
.max()
.unwrap_or(0);
Ok(format!("{:02}.md",max_num + 1))
}

fn update_prepend_content(template: &str, filename: &str) -> io::Result<String> {
let date = Local::now().format("%Y-%m-%dT%H:%M:%S").to_string();
let filename_wo_ext = filename.replace(".md","");
let content = template
.replace("$TITLE",&filename_wo_ext)
.replace("$DATE",&date);
Ok(content)
}

fn publish(new_file_path: &str,repo_path: &str,new_file_name: &str) -> io::Result<()>{
Command::new("git")
.args(&["add", &new_file_path])
.current_dir(&repo_path)
Expand All @@ -67,28 +83,28 @@ fn main() -> io::Result<()> {
.current_dir(&repo_path)
.status()
.expect("Failed to push");

Ok(())

}
fn config()->io::Result<Config>{

fn get_next_file_name(folder: &str) -> io::Result<String> {
let paths = fs::read_dir(folder)?
.filter_map(Result::ok)
.filter(|e| e.path().extension().and_then(|s| s.to_str())==Some("md"))
.collect::<Vec<_>>();
let max_num = paths.iter()
.filter_map(|e| e.path().file_stem().and_then(|s| s.to_str()).and_then(|s| s.parse::<u8>().ok()))
.max()
.unwrap_or(0);
Ok(format!("{:02}.md",max_num + 1))
let config_path = dirs::home_dir()
.expect("failed to find home dir")
.join(format!(".config/{}/config.json",APP_NAME))
;
let config_data = read_to_string(config_path)?;
let config:Config = from_str(&config_data).expect("Invalid config format");

Ok(config)
}

fn update_prepend_content(template: &str, filename: &str) -> io::Result<String> {
let date = Local::now().format("%Y-%m-%dT%H:%M:%S").to_string();
let filename_wo_ext = filename.replace(".md","");
let content = template
.replace("$TITLE",&filename_wo_ext)
.replace("$DATE",&date);
Ok(content)
fn get_content(editor: String) -> io::Result<String>{
let temp_file = NamedTempFile::new()?;
let temp_path = temp_file.path().to_str().unwrap().to_string();

Command::new(editor)
.arg(&temp_path)
.status()
.expect("Failed to open editor");

read_to_string(temp_path)
}

0 comments on commit f2a3ae8

Please sign in to comment.