-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmuxd.rb
76 lines (64 loc) · 1.48 KB
/
tmuxd.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/ruby
require 'eventmachine'
require 'timeout'
require_relative 'lib/statusbar.rb'
require_relative 'theme.rb'
# tmux statusbar daemon class
class TmuxD
def initialize
# initialize theme
@theme = Theme.new
# import all plugins
@plugins = []
@theme.plugins.each do |plugin|
@plugins << load_plugin(plugin)
end
end
# starts the tmux statusbar daemon
def run
# run each plugin first
@plugins.each do |plugin|
run_plugin plugin
end
# start the event machine
EventMachine.run do
# create events for each plugin
@plugins.each do |plugin|
EventMachine.add_periodic_timer(plugin.interval) do
run_plugin plugin
end
end
end
end
# run a plugin, times out after 5 seconds
def run_plugin plugin
begin
Timeout::timeout(5) do
plugin.run
end
update_bar
rescue
end
end
# update each side
def update_bar
write_bar "right"
write_bar "left"
end
# write out status bar side to files
def write_bar side
output = @theme.send "format_#{side}".to_sym
@plugins.each do |plugin|
output = output.sub("{{#{plugin.class}}}", plugin.output)
end
f = File.open("#{ENV['HOME']}/.tmuxd/#{side}", "w")
f << output
f.close
end
# import plugin from plugins directory
# returns an instance of the class
def load_plugin(plugin)
require_relative "plugins/#{plugin}.rb"
Kernel.const_get(plugin).new
end
end