60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
use std::fs;
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
use std::process::Command;
|
|
|
|
use chrono::Local;
|
|
|
|
use crate::config::Settings;
|
|
use crate::error::StreamdError;
|
|
use crate::extract::parse_markdown_file;
|
|
|
|
pub fn run() -> Result<(), StreamdError> {
|
|
let settings = Settings::load()?;
|
|
let streamd_directory = &settings.base_folder;
|
|
|
|
let timestamp = Local::now().format("%Y%m%d-%H%M%S").to_string();
|
|
let preliminary_file_name = format!("{}_wip.md", timestamp);
|
|
let preliminary_path = Path::new(streamd_directory).join(&preliminary_file_name);
|
|
|
|
// Create initial file with heading
|
|
let content = "# ";
|
|
let mut file = fs::File::create(&preliminary_path)?;
|
|
file.write_all(content.as_bytes())?;
|
|
drop(file);
|
|
|
|
// Open in editor
|
|
let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
|
|
let status = Command::new(&editor).arg(&preliminary_path).status()?;
|
|
|
|
if !status.success() {
|
|
return Err(StreamdError::IoError(std::io::Error::other(
|
|
"Editor exited with non-zero status",
|
|
)));
|
|
}
|
|
|
|
// Read the edited content
|
|
let edited_content = fs::read_to_string(&preliminary_path)?;
|
|
let parsed_content =
|
|
parse_markdown_file(preliminary_path.to_string_lossy().as_ref(), &edited_content);
|
|
|
|
// Determine final filename based on markers
|
|
let final_file_name = if let Some(ref shard) = parsed_content.shard {
|
|
if !shard.markers.is_empty() {
|
|
format!("{} {}.md", timestamp, shard.markers.join(" "))
|
|
} else {
|
|
format!("{}.md", timestamp)
|
|
}
|
|
} else {
|
|
format!("{}.md", timestamp)
|
|
};
|
|
|
|
let final_path = Path::new(streamd_directory).join(&final_file_name);
|
|
|
|
// Rename the file
|
|
fs::rename(&preliminary_path, &final_path)?;
|
|
|
|
println!("Saved as {}", final_file_name);
|
|
|
|
Ok(())
|
|
}
|