pipehandler.03-clickable.rb 1.99 KB
Newer Older
epsi sayidina's avatar
epsi sayidina committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
require_relative 'output'

# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe

def handle_command_event(monitor, event) 
  # find out event origin
  column = event.split("\t")
  origin = column[0]
    
  tag_cmds = ['tag_changed', 'tag_flags', 'tag_added', 'tag_removed']
  title_cmds = ['window_title_changed', 'focus_changed']

  case origin
  when 'reload'
    os.system('pkill lemonbar')
  when 'quit_panel'
    exit
  when *tag_cmds       # splat operator
    set_tag_value(monitor)
  when *title_cmds     # splat operator
epsi sayidina's avatar
epsi sayidina committed
22 23
    title = column.length > 2 ? column[2] : ''
    set_windowtitle(title)
epsi sayidina's avatar
epsi sayidina committed
24 25 26
  end
end

epsi sayidina's avatar
epsi sayidina committed
27
def content_init(monitor, lemon_stdin)
epsi sayidina's avatar
epsi sayidina committed
28 29 30 31 32
  # initialize statusbar before loop
  set_tag_value(monitor)
  set_windowtitle('')
      
  text = get_statusbar_text(monitor)
epsi sayidina's avatar
epsi sayidina committed
33
  lemon_stdin.puts(text)
epsi sayidina's avatar
epsi sayidina committed
34 35
end

epsi sayidina's avatar
epsi sayidina committed
36
def content_walk(monitor, lemon_stdin)
epsi sayidina's avatar
epsi sayidina committed
37
  # start an io
epsi sayidina's avatar
epsi sayidina committed
38 39
  command_in = 'herbstclient --idle'
  
epsi sayidina's avatar
epsi sayidina committed
40 41
  IO.popen(command_in, "r") do |io_idle|
    while io_idle do 
epsi sayidina's avatar
epsi sayidina committed
42 43
      # read next event, trim newline
      event = (io_idle.gets).strip
epsi sayidina's avatar
epsi sayidina committed
44 45 46
      handle_command_event(monitor, event)
        
      text = get_statusbar_text(monitor)
epsi sayidina's avatar
epsi sayidina committed
47
      lemon_stdin.puts(text)
epsi sayidina's avatar
epsi sayidina committed
48
    end
epsi sayidina's avatar
epsi sayidina committed
49
    io_idle.close()
epsi sayidina's avatar
epsi sayidina committed
50 51 52 53 54
  end
end

def run_lemon(monitor, parameters)
  command_out  = 'lemonbar ' + parameters
epsi sayidina's avatar
epsi sayidina committed
55

epsi sayidina's avatar
epsi sayidina committed
56
  # note the r+ mode
epsi sayidina's avatar
epsi sayidina committed
57 58
  IO.popen(command_out, 'r+') do |io_lemon| 

epsi sayidina's avatar
epsi sayidina committed
59
    pid_content = fork do 
epsi sayidina's avatar
epsi sayidina committed
60 61
      content_init(monitor, io_lemon)
      content_walk(monitor, io_lemon) # loop for each event
epsi sayidina's avatar
epsi sayidina committed
62
    end
epsi sayidina's avatar
epsi sayidina committed
63
    Process.detach(pid_content)
epsi sayidina's avatar
epsi sayidina committed
64

epsi sayidina's avatar
epsi sayidina committed
65 66 67
    # CPU hog caveat when using 'pkill lemonbar'
    # Abnormal lemonbar process termination, will make this loop go wild

epsi sayidina's avatar
epsi sayidina committed
68 69 70
    IO.popen('sh', 'w') do |io_sh|
      while io_lemon do
        io_sh.puts io_lemon.gets
epsi sayidina's avatar
epsi sayidina committed
71 72
      end
        
epsi sayidina's avatar
epsi sayidina committed
73
      io_sh.close()
epsi sayidina's avatar
epsi sayidina committed
74
    end
epsi sayidina's avatar
epsi sayidina committed
75 76
 
    io_lemon.close()
epsi sayidina's avatar
epsi sayidina committed
77 78 79 80 81 82 83
  end
end

def detach_lemon(monitor, parameters)
  # warning: Signal.trap is application wide
  Signal.trap("PIPE", "EXIT")
    
epsi sayidina's avatar
epsi sayidina committed
84 85
  pid_lemon = fork { run_lemon(monitor, parameters) }
  Process.detach(pid_lemon)
epsi sayidina's avatar
epsi sayidina committed
86
end