1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
# requires dmenu, sselp, xclip
# `kill -s USR1 $PID' invokes selection process
CLIPBOARD_VERSION = "0.1"
CLIPBOARD_RELEASE_DATE = "2008-07-22"

FONT="-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*"
MENUBGCOLOR="#006699"
MENUFGCOLOR="#ffffff"
MENUSELBGCOLOR="#eeeeee"
MENUSELFGCOLOR="#222222"
NO_OF_BUFF = 10
REFRESH_PERIOD= 1

selection = ''
oldselection = ''
sel_buffer = Array.new

class String 
  def lazy_shellescape 
    self.dup.gsub(/([`"\\])/) { |s| '\\' << s }
  end 
end 

command = "ps ax|grep '#{$0}'|grep -v grep"
proc = `#{command}`.strip.scan(/^[0-9]+/)[0].to_i
system "kill -9 #{proc}" if proc > 0 and proc != $$

trap("SIGUSR1") do
  list = Array.new
  sel_buffer.each { |curr| list.push(curr.gsub(/\n/, " ").lazy_shellescape.concat("\n")) }
  list[-1].reverse.gsub!(/\n/, "")
  puts list
  choice = `echo "#{list.reverse.to_s}"|dmenu -fn "#{FONT}" -nb "#{MENUBGCOLOR}" \
    -nf "#{MENUFGCOLOR}" -sb "#{MENUSELBGCOLOR}" -sf "#{MENUSELFGCOLOR}"`.lazy_shellescape
  puts "Auswahl: #{choice}"
  puts "liste: #{list}listenende"
  if(choice.length > 0)
    obj_id = list.rindex(choice+"\n") || obj_id = list.rindex(choice)
    print obj_id, ": ", choice, "\n"
    selection = sel_buffer[obj_id]
    oldselection = selection
    puts selection
    cmd = %Q{echo "#{selection.lazy_shellescape}" | xclip -i}
    puts cmd
    system cmd
  end
end

loop do
  begin
    selection = `sselp`.strip # sselp is much faster than xclip
    if selection != oldselection
      oldselection = selection
      sel_buffer.push(selection)
      sel_buffer.uniq!
      sel_buffer.shift if sel_buffer.length > NO_OF_BUFF
      puts "#{sel_buffer.length}: #{selection}"
    end
  rescue
    $stderr.puts $!
  end
  sleep REFRESH_PERIOD
end