From a00142d595e53af782e0ec1ef2140fd64a8a9ccd Mon Sep 17 00:00:00 2001 From: jooaf Date: Mon, 16 Dec 2024 16:32:29 -0600 Subject: [PATCH 1/2] Adding in load backup command to replace the old thoth notes file --- Cargo.lock | 4 ++-- src/cli.rs | 16 ++++++++++++++-- src/main.rs | 5 ++++- src/ui_handler.rs | 5 +++-- src/utils.rs | 5 ++--- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c6797f..4f7a34b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "adler" @@ -1412,7 +1412,7 @@ dependencies = [ [[package]] name = "thoth-cli" -version = "0.1.62" +version = "0.1.66" dependencies = [ "anyhow", "arboard", diff --git a/src/cli.rs b/src/cli.rs index c4da201..7b17838 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use crate::EditorClipboard; +use crate::{get_save_backup_file_path, load_textareas, save_textareas, EditorClipboard}; use anyhow::{bail, Result}; use std::{ fs::File, @@ -11,13 +11,14 @@ use clap::{Parser, Subcommand}; use crate::get_save_file_path; #[derive(Parser)] -#[command(author = env!("CARGO_PKG_AUTHORS"), version = env!("CARGO_PKG_VERSION"), about, long_about = None)] +#[command(author = env!("CARGO_PKG_AUTHORS"), version = env!("CARGO_PKG_VERSION"), about, long_about = None, rename_all = "snake_case")] pub struct Cli { #[command(subcommand)] pub command: Option, } #[derive(Subcommand)] +#[command(rename_all = "snake_case")] pub enum Commands { /// Add a new block to the scratchpad Add { @@ -28,6 +29,8 @@ pub enum Commands { }, /// List all of the blocks within your thoth scratchpad List, + /// Load backup file as the main thoth markdown file + LoadBackup, /// Delete a block by name Delete { /// The name of the block to be deleted @@ -74,6 +77,15 @@ pub fn list_blocks() -> Result<()> { Ok(()) } +pub fn replace_from_backup() -> Result<()> { + let (backup_textareas, backup_textareas_titles) = load_textareas(get_save_backup_file_path())?; + save_textareas( + &backup_textareas, + &backup_textareas_titles, + get_save_file_path(), + ) +} + pub fn view_block(name: &str) -> Result<()> { let file = File::open(get_save_file_path())?; let reader = BufReader::new(file); diff --git a/src/main.rs b/src/main.rs index 6a898d8..50099fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use std::{ thread, }; use thoth_cli::{ - cli::{add_block, copy_block, delete_block, list_blocks, view_block}, + cli::{add_block, copy_block, delete_block, list_blocks, replace_from_backup, view_block}, get_save_backup_file_path, EditorClipboard, }; use thoth_cli::{ @@ -48,6 +48,9 @@ fn main() -> Result<()> { Some(Commands::List) => { list_blocks()?; } + Some(Commands::LoadBackup) => { + replace_from_backup()?; + } Some(Commands::Delete { name }) => { delete_block(name)?; } diff --git a/src/ui_handler.rs b/src/ui_handler.rs index cd459fd..df3d398 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -39,8 +39,9 @@ pub struct UIState { impl UIState { pub fn new() -> Result { let mut scrollable_textarea = ScrollableTextArea::new(); - if get_save_file_path().exists() { - let (loaded_textareas, loaded_titles) = load_textareas()?; + let main_save_path = get_save_file_path(); + if main_save_path.exists() { + let (loaded_textareas, loaded_titles) = load_textareas(main_save_path)?; for (textarea, title) in loaded_textareas.into_iter().zip(loaded_titles) { scrollable_textarea.add_textarea(textarea, title); } diff --git a/src/utils.rs b/src/utils.rs index b0e8394..5982a4d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,3 @@ -use crate::get_save_file_path; use anyhow::Result; use std::io::{BufRead, Write}; use std::path::PathBuf; @@ -25,8 +24,8 @@ pub fn save_textareas(textareas: &[TextArea], titles: &[String], file_path: Path Ok(()) } -pub fn load_textareas() -> Result<(Vec>, Vec)> { - let file = File::open(get_save_file_path())?; +pub fn load_textareas(file_path: PathBuf) -> Result<(Vec>, Vec)> { + let file = File::open(file_path)?; let reader = BufReader::new(file); let mut textareas = Vec::with_capacity(10); let mut titles = Vec::with_capacity(10); From 44d3ca66454e714e21b73d752bf0d4b95e4b67ae Mon Sep 17 00:00:00 2001 From: jooaf Date: Mon, 16 Dec 2024 16:34:20 -0600 Subject: [PATCH 2/2] save the backup file with the main file upon quitting --- src/ui_handler.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ui_handler.rs b/src/ui_handler.rs index df3d398..1bfee11 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -1,4 +1,4 @@ -use crate::EditorClipboard; +use crate::{get_save_backup_file_path, EditorClipboard}; use anyhow::{bail, Result}; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode, KeyModifiers}, @@ -305,6 +305,11 @@ fn handle_normal_input( &state.scrollable_textarea.titles, get_save_file_path(), )?; + save_textareas( + &state.scrollable_textarea.textareas, + &state.scrollable_textarea.titles, + get_save_backup_file_path(), + )?; return Ok(true); } state.scrollable_textarea.textareas[state.scrollable_textarea.focused_index].input(key);