Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added native Windows clipboard support #373

Merged
merged 3 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ log = "~0.4"

which = "4.1"

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

[dev-dependencies]
helix-tui = { path = "../helix-tui" }
27 changes: 26 additions & 1 deletion helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
copy => "tmux", "load-buffer", "-";
}
} else {
Box::new(provider::NopProvider)
#[cfg(target_os = "windows")]
return Box::new(provider::WindowsProvider);
Copy link
Member Author

@kirawi kirawi Jun 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the returns here because it gives you a warning otherwise. (Actually, maybe not now since it's conditional. However, I don't feel like it's that big of a deal to warrant a change. It might make it clearer, even.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think can just remove the return or you could use if cfg!(target_os = "windows") { ... }.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if cfg!(target_os = "windows") { ... } sounds good

Copy link
Member Author

@kirawi kirawi Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://doc.rust-lang.org/std/macro.cfg.html

cfg!, unlike #[cfg], does not remove any code and only evaluates to true or false. For example, all blocks in an if/else expression need to be valid when cfg! is used for the condition, regardless of what cfg! is evaluating.

Unfortunately, cfg!() would still compile the code and perform the check at runtime, which means it wouldn't compile for other platforms because clipboard-win depends upon winapi. This was what I originally did before I realized it didn't work, though the commits are now squashed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the #[cfg] macro also seems to want the following line to end in a semi-colon, so I have to make it an explicit return.


#[cfg(not(target_os = "windows"))]
return Box::new(provider::NopProvider);
}
}

Expand Down Expand Up @@ -120,6 +124,27 @@ mod provider {
}
}

#[cfg(target_os = "windows")]
#[derive(Debug)]
pub struct WindowsProvider;

#[cfg(target_os = "windows")]
impl ClipboardProvider for WindowsProvider {
fn name(&self) -> Cow<str> {
Cow::Borrowed("clipboard-win")
}

fn get_contents(&self) -> Result<String> {
let contents = clipboard_win::get_clipboard(clipboard_win::formats::Unicode)?;
kirawi marked this conversation as resolved.
Show resolved Hide resolved
Ok(contents)
}

fn set_contents(&self, contents: String) -> Result<()> {
clipboard_win::set_clipboard(clipboard_win::formats::Unicode, contents)?;
Ok(())
}
}

#[derive(Debug)]
pub struct CommandConfig {
pub prg: &'static str,
Expand Down