8000 Feature: Adding thread to save backups by jooaf · Pull Request #13 · jooaf/thoth · GitHub
[go: up one dir, main page]

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
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub use utils::{load_textareas, save_textareas};
pub fn get_save_file_path() -> PathBuf {
home_dir().unwrap_or_default().join("thoth_notes.md")
}
pub fn get_save_backup_file_path() -> PathBuf {
home_dir().unwrap_or_default().join("thoth_notes_backup.md")
}

pub const ORANGE: ratatui::style::Color = ratatui::style::Color::Rgb(255, 165, 0);
pub const DAEMONIZE_ARG: &str = "__thoth_copy_daemonize";
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, Read};
use std::{
io::{self, Read},
thread,
};
use thoth_cli::{
cli::{add_block, copy_block, delete_block, list_blocks, view_block},
EditorClipboard,
get_save_backup_file_path, EditorClipboard,
};
use thoth_cli::{
cli::{Cli, Commands},
ui_handler::{draw_ui, handle_input, UIState},
utils::save_textareas,
};

use std::time::Duration;
Expand Down Expand Up @@ -72,6 +76,13 @@ pub fn run_ui() -> Result<()> {

let draw_interval = Duration::from_millis(33);

let copy_textareas = state.scrollable_textarea.textareas.clone();
let copy_titles = state.scrollable_textarea.titles.clone();
thread::spawn(move || loop {
let _ = save_textareas(&copy_textareas, &copy_titles, get_save_backup_file_path());
thread::sleep(Duration::from_secs(60)); // save backup every minute
});

loop {
let should_draw = state.last_draw.elapsed() >= draw_interval;
if should_draw {
Expand Down
1 change: 1 addition & 0 deletions src/ui_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ fn handle_normal_input(
save_textareas(
&state.scrollable_textarea.textareas,
&state.scrollable_textarea.titles,
get_save_file_path(),
)?;
return Ok(true);
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::get_save_file_path;
use anyhow::Result;
use std::io::{BufRead, Write};
use std::path::PathBuf;
use std::{fs::File, io::BufReader};
use tui_textarea::TextArea;

pub fn save_textareas(textareas: &[TextArea], titles: &[String]) -> Result<()> {
let mut file = File::create(get_save_file_path())?;
pub fn save_textareas(textareas: &[TextArea], titles: &[String], file_path: PathBuf) -> Result<()> {
let mut file = File::create(file_path)?;
for (textarea, title) in textareas.iter().zip(titles.iter()) {
writeln!(file, "# {}", title)?;
let content = textarea.lines().join("\n");
Expand Down
0