Skip to content

Commit

Permalink
Allow clipboard: keyword argument for paste + validate selection name
Browse files Browse the repository at this point in the history
  • Loading branch information
janlelis committed Apr 7, 2024
1 parent 276f41a commit 35464e4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
### Major Changes
* Required Ruby to 3.0
* `Clipboard.copy` and `.clear` now always return true instead of the pasted string
* Linux-based `Clipboard .copy` methods now take a clipboard argument `clipboard:`
* Linux-based `Clipboard.copy` methods now take a clipboard argument `clipboard:`
to choose which clipboard to copy to (default is "all").
For example: `Clipboard.copy("data", clipboard: "primary")` copies only to primary clipboard
* Linux-based `Clipboard.paste` methods use either the `clipboard:` keyword argument or the
positional argument for choosing the clipboard to paste from
* Allow choosing clipboard implementation with camel-cased symbol/string:
`Clipboard.implementation = :linux_wayland`

Expand All @@ -18,6 +20,8 @@
* Change `copy`, `paste`, and `clear` to always accept keyword arguments
* All implementations are now based on `Clipboard::Implementation`
* Move implementation detection to `utils.rb`
* Be sure to pass a valid lowercase clipboard selection name (or "all") when choosing clipboard,
will now raise `ArgumentError` if clipboard name is not supported

### Other
* Add more specs and run them on GitHub Actions CI
Expand Down
13 changes: 8 additions & 5 deletions lib/clipboard/gtk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ module Gtk
end
end

def paste(which = nil, **)
if !which || !CLIPBOARDS.include?(which.to_s.downcase)
which = CLIPBOARDS.first
end
def paste(which = nil, clipboard: "clipboard")
selection = which || clipboard
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

::Gtk::Clipboard.get(
Gdk::Selection.const_get(which.to_s.upcase)
Gdk::Selection.const_get(selection.to_s.upcase)
).wait_for_text || ""
end

def copy(data, clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

::Gtk::Clipboard.get(Gdk::Selection.const_get(selection.to_s.upcase)).set_text(data).store
}

Expand All @@ -45,6 +46,8 @@ def copy(data, clipboard: "all")
def clear(clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

::Gtk::Clipboard.get(Gdk::Selection.const_get(selection.to_s.upcase)).clear
}

Expand Down
11 changes: 6 additions & 5 deletions lib/clipboard/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ module Linux
"If your system is Wayland-based, please install wl-clipboard"
end

def paste(which = nil, **)
if !which || !CLIPBOARDS.include?(which_normalized = which.to_s.downcase)
which_normalized = CLIPBOARDS.first
end
def paste(which = nil, clipboard: "clipboard")
selection = which || clipboard
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

`#{READ_COMMAND} #{SELECTION[which_normalized]} 2> /dev/null`
`#{READ_COMMAND} #{SELECTION[selection]} 2> /dev/null`
end

def copy(data, clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

Utils.popen "#{WRITE_COMMAND} #{SELECTION[selection]}", data, READ_OUTPUT_STREAM
}

Expand Down
10 changes: 9 additions & 1 deletion lib/clipboard/linux_wayland.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ module LinuxWayland
"Please install it or try a different implementation"
end

def paste(selection = nil, **)
def paste(which = nil, clipboard: "clipboard")
selection = which || clipboard
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

primary_flag = selection == "primary" ? " --primary" : ""

`#{READ_COMMAND}#{primary_flag}`
end

def copy(data, clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

primary_flag = selection == "primary" ? " --primary" : ""
Utils.popen("#{WRITE_COMMAND}#{primary_flag}", data)
}
Expand All @@ -37,6 +43,8 @@ def copy(data, clipboard: "all")
def clear(clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

primary_flag = selection.to_s == "primary" ? " --primary" : ""
`#{WRITE_COMMAND}#{primary_flag} --clear`
}
Expand Down
6 changes: 5 additions & 1 deletion lib/clipboard/osc52.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ module Osc52
include Implementation
extend self

CLIPBOARDS = ["clipboard", "primary"].freeze
CLIPBOARDS = %w[clipboard primary].freeze
OSC52 = "]52;%<selection_option>s;%<data_base64>s"

def copy(data, clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

print OSC52 % {
selection_option: selection == "primary" ? "p" : "c",
data_base64: [data].pack("m0"),
Expand All @@ -25,6 +27,8 @@ def copy(data, clipboard: "all")
def clear(clipboard: "all")
selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
selections.each{ |selection|
raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

print OSC52 % {
selection_option: selection == "primary" ? "p" : "c",
data_base64: ?! # anything non-base64 / question mark clears
Expand Down

0 comments on commit 35464e4

Please sign in to comment.