Last active
March 24, 2020 14:42
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
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 😄