From 21af0bc9bba707969f1644cc4080eb63de29500d Mon Sep 17 00:00:00 2001 From: Jan Lelis Date: Sun, 7 Apr 2024 13:15:30 +0200 Subject: [PATCH] Change `copy`, `paste`, and `clear` to always accept keyword arguments --- CHANGELOG.md | 1 + lib/clipboard/cygwin.rb | 4 ++-- lib/clipboard/file.rb | 6 +++--- lib/clipboard/gtk.rb | 6 +++--- lib/clipboard/implementation.rb | 8 ++++---- lib/clipboard/java.rb | 6 +++--- lib/clipboard/linux_wayland.rb | 6 +++--- lib/clipboard/mac.rb | 4 ++-- lib/clipboard/version.rb | 2 +- lib/clipboard/windows.rb | 6 +++--- lib/clipboard/wsl.rb | 4 ++-- 11 files changed, 27 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20b831d..c8ba728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lib/clipboard/cygwin.rb b/lib/clipboard/cygwin.rb index 3a050a8..5585dec 100644 --- a/lib/clipboard/cygwin.rb +++ b/lib/clipboard/cygwin.rb @@ -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 diff --git a/lib/clipboard/file.rb b/lib/clipboard/file.rb index 34260a0..0143ff3 100644 --- a/lib/clipboard/file.rb +++ b/lib/clipboard/file.rb @@ -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 diff --git a/lib/clipboard/gtk.rb b/lib/clipboard/gtk.rb index 9035dc2..6144417 100644 --- a/lib/clipboard/gtk.rb +++ b/lib/clipboard/gtk.rb @@ -23,7 +23,7 @@ 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 } @@ -31,7 +31,7 @@ def copy(text) 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 @@ -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 } diff --git a/lib/clipboard/implementation.rb b/lib/clipboard/implementation.rb index ba9b3f1..0411ece 100644 --- a/lib/clipboard/implementation.rb +++ b/lib/clipboard/implementation.rb @@ -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 diff --git a/lib/clipboard/java.rb b/lib/clipboard/java.rb index 0d2f32c..4844c47 100644 --- a/lib/clipboard/java.rb +++ b/lib/clipboard/java.rb @@ -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 '' diff --git a/lib/clipboard/linux_wayland.rb b/lib/clipboard/linux_wayland.rb index 52fe781..de8726a 100644 --- a/lib/clipboard/linux_wayland.rb +++ b/lib/clipboard/linux_wayland.rb @@ -17,7 +17,7 @@ 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 @@ -25,13 +25,13 @@ def paste(might_select_primary_clipboard = nil) end end - def copy(data) + def copy(data, **) Utils.popen WRITE_COMMAND, data true end - def clear + def clear(**) `#{WRITE_COMMAND} --clear` true diff --git a/lib/clipboard/mac.rb b/lib/clipboard/mac.rb index 1cadfee..b8e84ce 100644 --- a/lib/clipboard/mac.rb +++ b/lib/clipboard/mac.rb @@ -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 diff --git a/lib/clipboard/version.rb b/lib/clipboard/version.rb index 267d5c5..5a131a9 100644 --- a/lib/clipboard/version.rb +++ b/lib/clipboard/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Clipboard - VERSION = "1.4.1" + VERSION = "2.0.0.next" end diff --git a/lib/clipboard/windows.rb b/lib/clipboard/windows.rb index 7f5427e..9e42924 100644 --- a/lib/clipboard/windows.rb +++ b/lib/clipboard/windows.rb @@ -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 ) @@ -59,7 +59,7 @@ def paste(_ = nil) User32.close end - def clear + def clear(**) User32.empty if User32.open(nil) true @@ -67,7 +67,7 @@ def clear 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 diff --git a/lib/clipboard/wsl.rb b/lib/clipboard/wsl.rb index e8421a5..4d619a4 100644 --- a/lib/clipboard/wsl.rb +++ b/lib/clipboard/wsl.rb @@ -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