Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Commands>,
}

#[derive(Subcommand)]
#[command(rename_all = "snake_case")]
pub enum Commands {
/// Add a new block to the scratchpad
Add {
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)?;
}
Expand Down
12 changes: 9 additions & 3 deletions src/ui_handler.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -39,8 +39,9 @@ pub struct UIState {
impl UIState {
pub fn new() -> Result<Self> {
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);
}
Expand Down Expand Up @@ -304,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);
Expand Down
5 changes: 2 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::get_save_file_path;
use anyhow::Result;
use std::io::{BufRead, Write};
use std::path::PathBuf;
Expand All @@ -25,8 +24,8 @@ pub fn save_textareas(textareas: &[TextArea], titles: &[String], file_path: Path
Ok(())
}

pub fn load_textareas() -> Result<(Vec<TextArea<'static>>, Vec<String>)> {
let file = File::open(get_save_file_path())?;
pub fn load_textareas(file_path: PathBuf) -> Result<(Vec<TextArea<'static>>, Vec<String>)> {
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);
Expand Down
Loading