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
@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