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},
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)
}

@ -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<FileHistoryEntry> {
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) {
let filepath = match register {
':' => command_histfile(),
@ -54,12 +58,20 @@ pub fn read_command_history() -> Vec<String> {
hist
}
pub fn trim_command_history(limit: usize) {
trim_history::<String>(command_histfile(), limit)
}
pub fn read_search_history() -> Vec<String> {
let mut hist = read_reg_history(search_histfile());
hist.reverse();
hist
}
pub fn trim_search_history(limit: usize) {
trim_history::<String>(search_histfile(), limit)
}
pub fn write_clipboard_file(values: &Vec<String>) {
write_history(clipboard_file(), values)
}

Loading…
Cancel
Save