Skip to content

Commit

Permalink
Add expiremental OSC52 implementation (only Clipboard.copy)
Browse files Browse the repository at this point in the history
  • Loading branch information
janlelis committed Apr 7, 2024
1 parent 40a9fca commit e151a2a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Set required Ruby to 3.0
* `Clipboard.copy` and `.clear` now always return true instead of the pasted string

## New Features
* Add expiremental OSC52 implementation (only `Clipboard.copy`)

### Refactorings / Minor API Changes
* All implementations are now based on `Clipboard::Implementation`
* Move implementation detection to `utils.rb`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Lets you access the clipboard from everywhere. Currently supported platforms:
- WSL (Windows Subsystem for Linux)
- Gtk+ (Cross Platform Widget Toolkit)
- Java (on JRuby)
- *Experimental:* OSC52 (ANSI escape sequence) **only copying**

Supported Rubies: **3.3**, **3.2**, **3.1**, **3.0**

Expand Down
13 changes: 7 additions & 6 deletions lib/clipboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Clipboard
autoload :Cygwin, 'clipboard/cygwin'
autoload :Wsl, 'clipboard/wsl'
autoload :Gtk, 'clipboard/gtk'
autoload :Osc52, 'clipboard/osc52'
end
autoload :Windows, 'clipboard/windows'
autoload :File, 'clipboard/file'
Expand All @@ -32,16 +33,16 @@ def self.implementation=(val)
@implementation = val
end

def paste(*args)
Clipboard.implementation.paste(*args)
def paste(...)
Clipboard.implementation.paste(...)
end

def clear(*args)
Clipboard.implementation.clear(*args)
def clear(...)
Clipboard.implementation.clear(...)
end

def copy(*args)
Clipboard.implementation.copy(*args)
def copy(...)
Clipboard.implementation.copy(...)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/clipboard/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module Implementation
# Implement paste
# Should take an optional argument
def paste(_clipboard_name = nil)
raise NotImplementedError, "paste not supported by implementation"
raise NotImplementedError, "pasting not supported by this implementation, try another"
end

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

# Can be used to add a native clear implementation
Expand Down
43 changes: 43 additions & 0 deletions lib/clipboard/osc52.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require "pty"

require_relative "implementation"
require_relative "utils"

module Clipboard
module Osc52
include Implementation
extend self

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

def copy(data, clipboard: "all")
selections = clipboard == "all" ? SELECTIONS : [clipboard]
selections.each{ |selection|
selection_option = clipboard == "primary" ? "p" : "c"
data_base64 = [data].pack("m0")
print OSC52 % {
selection_option: "selection_option",
data_base64: "data_base64",
}
}

true
end

def clear(clipboard: "all")
selections = clipboard == "all" ? SELECTIONS : [clipboard]
selections.each{ |selection|
selection_option = clipboard == "primary" ? "p" : "c"
print OSC52 % {
selection_option: selection_option,
data_base64: ?!
}
}

true
end
end
end

0 comments on commit e151a2a

Please sign in to comment.