Skip to content

Commit

Permalink
Change copy, paste, and clear to always accept keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
janlelis committed Apr 7, 2024
1 parent 3cd8b0a commit 21af0bc
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Add expiremental OSC52 implementation (only `Clipboard.copy`)

### Refactorings / Minor API Changes
* Change `copy`, `paste`, and `clear` to always accept keyword arguments
* All implementations are now based on `Clipboard::Implementation`
* Move implementation detection to `utils.rb`

Expand Down
4 changes: 2 additions & 2 deletions lib/clipboard/cygwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module Cygwin
include Implementation
extend self

def paste(_ = nil)
def paste(_ = nil, **)
::File.read("/dev/clipboard")
end

def copy(data)
def copy(data, **)
::File.open("/dev/clipboard", "w"){ |f| f.write(data) }

true
Expand Down
6 changes: 3 additions & 3 deletions lib/clipboard/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ module File

FILE = ::File.expand_path("~/.clipboard")

def copy(text)
::File.open(FILE, 'w', 0o0600) { |f| f.write(text) } rescue ''
def copy(data, **)
::File.open(FILE, 'w', 0o0600) { |f| f.write(data) } rescue ''

true
end

def paste(_ = nil)
def paste(_ = nil, **)
::File.read(FILE) rescue ''
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/clipboard/gtk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ module Gtk
end
end

def copy(text)
def copy(text, **)
CLIPBOARDS.each{ |which|
::Gtk::Clipboard.get(Gdk::Selection.const_get(which)).set_text(text).store
}

true
end

def paste(which = nil)
def paste(which = nil, **)
if !which || !CLIPBOARDS.include?(which_normalized = which.to_s.upcase)
which_normalized = CLIPBOARDS.first
end
Expand All @@ -41,7 +41,7 @@ def paste(which = nil)
).wait_for_text || ""
end

def clear
def clear(**)
CLIPBOARDS.each{ |which|
::Gtk::Clipboard.get(Gdk::Selection.const_get(which)).clear
}
Expand Down
8 changes: 4 additions & 4 deletions lib/clipboard/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ module Implementation

# Implement paste
# Should take an optional argument
def paste(_clipboard_name = nil)
def paste(_clipboard_name = nil, **)
raise NotImplementedError, "pasting not supported by this implementation, try another"
end

# Takes the data to copy as argument
# Should return true
def copy(_data)
def copy(_data, **)
raise NotImplementedError, "copying not supported by this implementation, try another"
end

# Can be used to add a native clear implementation
# Should return true
def clear
copy ''
def clear(...)
copy('', ...)

true
end
Expand Down
6 changes: 3 additions & 3 deletions lib/clipboard/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ module Java

FLAVOR = ::Java::JavaAwtDatatransfer::DataFlavor.stringFlavor

def copy(text)
selection_string = ::Java::JavaAwtDatatransfer::StringSelection.new text
def copy(data, **)
selection_string = ::Java::JavaAwtDatatransfer::StringSelection.new data
::Java::JavaAwt::Toolkit.default_toolkit.system_clipboard.set_contents selection_string, nil

true
end

def paste(_ = nil)
def paste(_ = nil, **)
::Java::JavaAwt::Toolkit.default_toolkit.system_clipboard.get_data(FLAVOR)
rescue
''
Expand Down
6 changes: 3 additions & 3 deletions lib/clipboard/linux_wayland.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ module LinuxWayland
"Please install it or try a different implementation"
end

def paste(might_select_primary_clipboard = nil)
def paste(might_select_primary_clipboard = nil, **)
if might_select_primary_clipboard == "primary"
`#{READ_COMMAND} --primary`
else
`#{READ_COMMAND}`
end
end

def copy(data)
def copy(data, **)
Utils.popen WRITE_COMMAND, data

true
end

def clear
def clear(**)
`#{WRITE_COMMAND} --clear`

true
Expand Down
4 changes: 2 additions & 2 deletions lib/clipboard/mac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module Mac
include Implementation
extend self

def paste(_ = nil)
def paste(_ = nil, **)
`pbpaste`
end

def copy(data)
def copy(data, **)
Utils.popen "pbcopy", data

true
Expand Down
2 changes: 1 addition & 1 deletion lib/clipboard/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Clipboard
VERSION = "1.4.1"
VERSION = "2.0.0.next"
end
6 changes: 3 additions & 3 deletions lib/clipboard/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Kernel32
end

# see http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx
def paste(_ = nil)
def paste(_ = nil, **)
return String.new unless User32.open(nil)

hclip = User32.get( CF_UNICODETEXT )
Expand All @@ -59,15 +59,15 @@ def paste(_ = nil)
User32.close
end

def clear
def clear(**)
User32.empty if User32.open(nil)

true
ensure
User32.close
end

def copy(data_to_copy)
def copy(data_to_copy, **)
if User32.open(nil)
User32.empty
data = data_to_copy.encode(Encoding::UTF_16LE) # TODO: catch bad encodings
Expand Down
4 changes: 2 additions & 2 deletions lib/clipboard/wsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module Wsl
include Implementation
extend self

def paste(_ = nil)
def paste(_ = nil, **)
`powershell.exe -Command Get-Clipboard`
end

def copy(data)
def copy(data, **)
Utils.popen "clip.exe", data

true
Expand Down

0 comments on commit 21af0bc

Please sign in to comment.