From 838a9b6c12eade281b9a8b9474d1486db9e51d7b Mon Sep 17 00:00:00 2001 From: Ingrid Date: Wed, 1 May 2024 19:06:32 +0200 Subject: [PATCH] trim persistence files --- helix-loader/src/persistence.rs | 13 +++++++++++++ helix-term/src/application.rs | 23 ++++++++++++++++++++++- helix-view/src/persistence.rs | 16 ++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/helix-loader/src/persistence.rs b/helix-loader/src/persistence.rs index f2f939501..c9ec96871 100644 --- a/helix-loader/src/persistence.rs +++ b/helix-loader/src/persistence.rs @@ -51,3 +51,16 @@ pub fn read_history Deserialize<'a>>(filepath: PathBuf) -> Vec { }, } } + +pub fn trim_history Deserialize<'a>>( + filepath: PathBuf, + limit: usize, +) { + // TODO: can we remove this clone? + let history: Vec = read_history(filepath.clone()); + if history.len() > limit { + let trim_start = history.len() - limit; + let trimmed_history = history[trim_start..].to_vec(); + write_history(filepath, &trimmed_history); + } +} diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index db08f2459..da6ac853d 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -25,7 +25,7 @@ use crate::{ compositor::{Compositor, Event}, config::Config, handlers, - job::Jobs, + job::{Job, Jobs}, keymap::Keymaps, ui::{self, overlay::overlaid}, }; @@ -303,6 +303,27 @@ impl Application { jobs: Jobs::new(), lsp_progress: LspProgressMap::new(), }; + app.jobs.add( + Job::new(async { + persistence::trim_file_history(5); + Ok(()) + }) + .wait_before_exiting(), + ); + app.jobs.add( + Job::new(async { + persistence::trim_command_history(5); + Ok(()) + }) + .wait_before_exiting(), + ); + app.jobs.add( + Job::new(async { + persistence::trim_search_history(5); + Ok(()) + }) + .wait_before_exiting(), + ); Ok(app) } diff --git a/helix-view/src/persistence.rs b/helix-view/src/persistence.rs index 9f0235769..332fc0392 100644 --- a/helix-view/src/persistence.rs +++ b/helix-view/src/persistence.rs @@ -1,7 +1,7 @@ use helix_core::Selection; use helix_loader::{ clipboard_file, command_histfile, file_histfile, - persistence::{push_history, read_history, write_history}, + persistence::{push_history, read_history, trim_history, write_history}, search_histfile, }; use serde::{Deserialize, Serialize}; @@ -9,7 +9,7 @@ use std::path::PathBuf; use crate::view::ViewPosition; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct FileHistoryEntry { pub path: PathBuf, pub view_position: ViewPosition, @@ -34,6 +34,10 @@ pub fn read_file_history() -> Vec { read_history(file_histfile()) } +pub fn trim_file_history(limit: usize) { + trim_history::(file_histfile(), limit) +} + pub fn push_reg_history(register: char, line: &String) { let filepath = match register { ':' => command_histfile(), @@ -54,12 +58,20 @@ pub fn read_command_history() -> Vec { hist } +pub fn trim_command_history(limit: usize) { + trim_history::(command_histfile(), limit) +} + pub fn read_search_history() -> Vec { let mut hist = read_reg_history(search_histfile()); hist.reverse(); hist } +pub fn trim_search_history(limit: usize) { + trim_history::(search_histfile(), limit) +} + pub fn write_clipboard_file(values: &Vec) { write_history(clipboard_file(), values) }