Skip to content

Commit

Permalink
Add clipboard provider configuration (helix-editor#8826)
Browse files Browse the repository at this point in the history
This change adds the `clipboard-provider` setting to the `editor` section
of configuration.

This option can have values of:
- `none` (use an internal buffer)
(on windows only)
- `windows` (use native windows clipboard)
(on MacOS only)
- `macos` (use pbcopy/pbpaste)
(on neiter of the above)
- `wayland`
- `xclip`
- `xsel`
- `win23yank` (for wsl)
(on all targets with "term")
- `termux`
- `tmux`
- `term` (osc codes)
- `custom` (see below for the configuration)

Note for a custom provider the configurations should look like:
```toml
[editor.clipboard-provider.config]
copy = ["tee", "test.txt"]
paste = ["cat", "test.txt"]
primary-copy = ["tee", "test-primary.txt"] // optional
primary-copy = ["cat", "test-primary.txt"] // optional
```

This can be configured at runtime with the usual:
```
set clipboard-provider term
```
Note: I was unable to work out a syntax expression for setting a `custom`
provider at runtime. In my opinion this is probably a fine limitation to
have but I am curious if there is a correct way I couldn't work out.

This ports over the previous provider selection logic so hopefully the
same default behaviour should apply.

I updated the health command to reflect the provider.
Note: this required reading the user configurations within the health command
which warrants discussion as this seems to not have been done before.

This is my first contribution, I am a C++ developer by profession and
a rust hobyist at best so nits and style updates very welcome.

Note: This adds the `nonempty` crate as a new dependency.
This is because I wanted a way to fail parsing custom commands if they
were empty and didn't want to write custom parsign Serde code.
I do not know what the process is for considering new dependencies and
am very willing to change this for an alternative solution when presented
with a better one! I looked for a `serde` annotation as I thought that
was likely but couldn't find one.
  • Loading branch information
AlfGalf committed Jun 12, 2024
1 parent 62655e9 commit 642be71
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 168 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions helix-term/src/health.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::config::{Config, ConfigLoadError};
use crossterm::{
style::{Color, Print, Stylize},
tty::IsTty,
};
use helix_core::config::{default_lang_config, user_lang_config};
use helix_loader::grammar::load_runtime_file;
use helix_view::clipboard::get_clipboard_provider;
use std::io::Write;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -53,7 +53,6 @@ pub fn general() -> std::io::Result<()> {
let lang_file = helix_loader::lang_config_file();
let log_file = helix_loader::log_file();
let rt_dirs = helix_loader::runtime_dirs();
let clipboard_provider = get_clipboard_provider();

if config_file.exists() {
writeln!(stdout, "Config file: {}", config_file.display())?;
Expand Down Expand Up @@ -92,7 +91,6 @@ pub fn general() -> std::io::Result<()> {
writeln!(stdout, "{}", msg.yellow())?;
}
}
writeln!(stdout, "Clipboard provider: {}", clipboard_provider.name())?;

Ok(())
}
Expand All @@ -101,8 +99,25 @@ pub fn clipboard() -> std::io::Result<()> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

let board = get_clipboard_provider();
match board.name().as_ref() {
let config = match Config::load_default() {
Ok(config) => config,
Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => {
Config::default()
}
Err(err) => {
writeln!(stdout, "{}", "Configuration file malformed".red())?;
writeln!(stdout, "{}", err.to_string())?;
return Ok(());
}
};

match config
.editor
.clipboard_provider
.get_provider()
.name()
.as_ref()
{
"none" => {
writeln!(
stdout,
Expand Down
1 change: 0 additions & 1 deletion helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ log = "~0.4"

parking_lot = "0.12.3"


[target.'cfg(windows)'.dependencies]
clipboard-win = { version = "5.3", features = ["std"] }

Expand Down
Loading

0 comments on commit 642be71

Please sign in to comment.