persist clipboard

pull/9143/head
Ingrid 9 months ago
parent 588e0b4b93
commit 56f47f04f6

@ -22,6 +22,8 @@ static SEARCH_HISTFILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::On
static FILE_HISTFILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
static CLIPBOARD_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
pub fn initialize_config_file(specified_file: Option<PathBuf>) {
let config_file = specified_file.unwrap_or_else(default_config_file);
ensure_parent_dir(&config_file);
@ -52,6 +54,12 @@ pub fn initialize_file_histfile(specified_file: Option<PathBuf>) {
FILE_HISTFILE.set(file_histfile).ok();
}
pub fn initialize_clipboard_file(specified_file: Option<PathBuf>) {
let clipboard_file = specified_file.unwrap_or_else(default_clipboard_file);
ensure_parent_dir(&clipboard_file);
CLIPBOARD_FILE.set(clipboard_file).ok();
}
/// A list of runtime directories from highest to lowest priority
///
/// The priority is:
@ -191,6 +199,10 @@ pub fn file_histfile() -> PathBuf {
FILE_HISTFILE.get().map(|path| path.to_path_buf()).unwrap()
}
pub fn clipboard_file() -> PathBuf {
CLIPBOARD_FILE.get().map(|path| path.to_path_buf()).unwrap()
}
pub fn workspace_config_file() -> PathBuf {
find_workspace().0.join(".helix").join("config.toml")
}
@ -215,6 +227,10 @@ pub fn default_file_histfile() -> PathBuf {
state_dir().join("file_history")
}
pub fn default_clipboard_file() -> PathBuf {
state_dir().join("clipboard")
}
/// Merge two TOML documents, merging values from `right` onto `left`
///
/// When an array exists in both `left` and `right`, `right`'s array is

@ -6,6 +6,20 @@ use std::{
path::PathBuf,
};
pub fn write_history<T: Serialize>(filepath: PathBuf, entries: &Vec<T>) {
let file = OpenOptions::new()
.write(true)
.create(true)
.open(filepath)
// TODO: do something about this unwrap
.unwrap();
for entry in entries {
// TODO: do something about this unwrap
serialize_into(&file, &entry).unwrap();
}
}
pub fn push_history<T: Serialize>(filepath: PathBuf, entry: &T) {
let file = OpenOptions::new()
.append(true)

@ -169,6 +169,11 @@ impl Application {
.write('/', persistence::read_search_history())
// TODO: do something about this unwrap
.unwrap();
editor
.registers
.write('"', persistence::read_clipboard_file())
// TODO: do something about this unwrap
.unwrap();
let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.keys

@ -44,6 +44,7 @@ use helix_view::{
info::Info,
input::KeyEvent,
keyboard::KeyCode,
persistence,
theme::Style,
tree,
view::View,
@ -4205,6 +4206,8 @@ fn yank_impl(editor: &mut Editor, register: char) {
.collect();
let selections = values.len();
persistence::write_clipboard_file(&values);
match editor.registers.write(register, values) {
Ok(_) => editor.set_status(format!(
"yanked {selections} selection{} to register {register}",

@ -83,6 +83,7 @@ FLAGS:
helix_loader::initialize_command_histfile(None);
helix_loader::initialize_search_histfile(None);
helix_loader::initialize_file_histfile(None);
helix_loader::initialize_clipboard_file(None);
// Help has a higher priority and should be handled separately.
if args.display_help {

@ -230,7 +230,7 @@ pub fn test_syntax_loader(overrides: Option<String>) -> helix_core::syntax::Load
helix_core::syntax::Loader::new(lang.try_into().unwrap()).unwrap()
}
fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath)> {
fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath, TempPath)> {
let command_file = NamedTempFile::new()?;
let command_path = command_file.into_temp_path();
helix_loader::initialize_command_histfile(Some(command_path.to_path_buf()));
@ -243,7 +243,11 @@ fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath)> {
let file_path = file_file.into_temp_path();
helix_loader::initialize_file_histfile(Some(file_path.to_path_buf()));
Ok((command_path, search_path, file_path))
let clipboard_file = NamedTempFile::new()?;
let clipboard_path = clipboard_file.into_temp_path();
helix_loader::initialize_clipboard_file(Some(clipboard_path.to_path_buf()));
Ok((command_path, search_path, file_path, clipboard_path))
}
/// Use this for very simple test cases where there is one input
@ -253,7 +257,7 @@ pub async fn test_with_config<T: Into<TestCase>>(
app_builder: AppBuilder,
test_case: T,
) -> anyhow::Result<()> {
let (_, _, _) = init_persistence_files()?;
let (_, _, _, _) = init_persistence_files()?;
let test_case = test_case.into();
let app = app_builder.build()?;

@ -1,7 +1,7 @@
use helix_core::Selection;
use helix_loader::{
command_histfile, file_histfile,
persistence::{push_history, read_history},
clipboard_file, command_histfile, file_histfile,
persistence::{push_history, read_history, write_history},
search_histfile,
};
use serde::{Deserialize, Serialize};
@ -59,3 +59,11 @@ pub fn read_search_history() -> Vec<String> {
hist.reverse();
hist
}
pub fn write_clipboard_file(values: &Vec<String>) {
write_history(clipboard_file(), values)
}
pub fn read_clipboard_file() -> Vec<String> {
read_history(clipboard_file())
}

Loading…
Cancel
Save