trim persistence files

pull/9143/head
Ingrid 7 months ago
parent bf0429004b
commit 838a9b6c12

@ -51,3 +51,16 @@ pub fn read_history<T: for<'a> Deserialize<'a>>(filepath: PathBuf) -> Vec<T> {
}, },
} }
} }
pub fn trim_history<T: Clone + Serialize + for<'a> Deserialize<'a>>(
filepath: PathBuf,
limit: usize,
) {
// TODO: can we remove this clone?
let history: Vec<T> = 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);
}
}

@ -25,7 +25,7 @@ use crate::{
compositor::{Compositor, Event}, compositor::{Compositor, Event},
config::Config, config::Config,
handlers, handlers,
job::Jobs, job::{Job, Jobs},
keymap::Keymaps, keymap::Keymaps,
ui::{self, overlay::overlaid}, ui::{self, overlay::overlaid},
}; };
@ -303,6 +303,27 @@ impl Application {
jobs: Jobs::new(), jobs: Jobs::new(),
lsp_progress: LspProgressMap::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) Ok(app)
} }

@ -1,7 +1,7 @@
use helix_core::Selection; use helix_core::Selection;
use helix_loader::{ use helix_loader::{
clipboard_file, command_histfile, file_histfile, clipboard_file, command_histfile, file_histfile,
persistence::{push_history, read_history, write_history}, persistence::{push_history, read_history, trim_history, write_history},
search_histfile, search_histfile,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -9,7 +9,7 @@ use std::path::PathBuf;
use crate::view::ViewPosition; use crate::view::ViewPosition;
#[derive(Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileHistoryEntry { pub struct FileHistoryEntry {
pub path: PathBuf, pub path: PathBuf,
pub view_position: ViewPosition, pub view_position: ViewPosition,
@ -34,6 +34,10 @@ pub fn read_file_history() -> Vec<FileHistoryEntry> {
read_history(file_histfile()) read_history(file_histfile())
} }
pub fn trim_file_history(limit: usize) {
trim_history::<FileHistoryEntry>(file_histfile(), limit)
}
pub fn push_reg_history(register: char, line: &String) { pub fn push_reg_history(register: char, line: &String) {
let filepath = match register { let filepath = match register {
':' => command_histfile(), ':' => command_histfile(),
@ -54,12 +58,20 @@ pub fn read_command_history() -> Vec<String> {
hist hist
} }
pub fn trim_command_history(limit: usize) {
trim_history::<String>(command_histfile(), limit)
}
pub fn read_search_history() -> Vec<String> { pub fn read_search_history() -> Vec<String> {
let mut hist = read_reg_history(search_histfile()); let mut hist = read_reg_history(search_histfile());
hist.reverse(); hist.reverse();
hist hist
} }
pub fn trim_search_history(limit: usize) {
trim_history::<String>(search_histfile(), limit)
}
pub fn write_clipboard_file(values: &Vec<String>) { pub fn write_clipboard_file(values: &Vec<String>) {
write_history(clipboard_file(), values) write_history(clipboard_file(), values)
} }

Loading…
Cancel
Save