Skip to content

Instantly share code, notes, and snippets.

@seven1m
Last active March 24, 2020 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seven1m/e2973274520b25db8f7a7d9155cc8566 to your computer and use it in GitHub Desktop.
Save seven1m/e2973274520b25db8f7a7d9155cc8566 to your computer and use it in GitHub Desktop.
simple clipboard history script for Sway window manager and dmenu (might work with other Wayland window managers too)
#!/usr/bin/env ruby
# https://mpov.timmorgan.org/clipboard-history-in-sway-window-manager/
require 'json'
STORE = "#{ENV['HOME']}/.clipboard-history"
LIMIT = 100
unless File.exist?(STORE)
File.open(STORE, 'w') { |f| f.write('[]') }
end
puts 'read clips'
clips = JSON.load(File.read(STORE))
if ARGV[0] == 'menu'
puts 'show menu'
sel = nil
IO.popen("dmenu -p 'select a paste'", 'r+') do |io|
io.write(clips.map(&:to_json).join("\n"))
io.close_write
sel = io.read
end
exit if sel.empty?
IO.popen('wl-copy --type text/plain', 'w') { |io| io.write(JSON.parse(sel)) }
else
loop do
clip = `wl-paste -n`
if !clip.strip.empty? && clip != clips.first
puts 'add clip'
clips = ([clip] + clips).uniq[0...LIMIT]
File.open(STORE, 'w') do |file|
file.puts(JSON.pretty_generate(clips))
end
end
sleep 5
end
end
@wesbluemarine
Copy link

I have this error on Debian Buster:

clipd:30:in block in <main>': undefined method first' for nil:NilClass (NoMethodError)

@seven1m
Copy link
Author

seven1m commented Jan 12, 2020

Sorry about that. The script assumes there is something in the file I suppose. This should fix it:

echo ‘[]’ > ~/.clipboard-history

@wesbluemarine
Copy link

wesbluemarine commented Jan 12, 2020

Thanks!
It works but every 5 seconds sway resize the current windows.
If I resolve the issue I'd like to use a terminal+fzf instead of dmenu, something like this

EDIT
The issue was for an old version of wl-clipboard!

@bugaevc
Copy link

bugaevc commented Mar 23, 2020

Hi! @seven1m, I wanted to contact you for some time regarding your blog post you've linked: wl-clipboard 2.0 has introduced the --watch mode, which means it will notify you when the selection changes instead of you having to poll for it every few seconds.

See the man page, bugaevc/wl-clipboard#39 (comment) and bugaevc/wl-clipboard#59 for more info & discussion.

Please update your script & add an update to your blog post (or even write a new one). I'm trying to get everyone off of polling the clipboard to using the --watch mode 😄

@seven1m
Copy link
Author

seven1m commented Mar 24, 2020

Awesome! I'll see about getting this script updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment