This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsd.rb
86 lines (68 loc) · 2.27 KB
/
dsd.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
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby
require 'eventmachine'
require 'date'
require 'ffi'
require 'yaml'
require 'trollop'
require 'daemons'
require_relative 'em-simplerepl'
module Xname
extend FFI::Library
ffi_lib 'xname'
attach_function :xname, [ :string ], :int
end
class ObservableArray < Array
attr_accessor :callback, :suppress
def initialize(&callback)
@callback = callback
end
def []=(index, value)
super(index, value)
@callback.call unless @suppress
self
end
end
class DSD < ObservableArray
attr_accessor :timers
def initialize(configuration, &callback)
@callback = callback
@timers = {}
configuration["items"].each.with_index do |item, index|
item = item["item"]
@timers.merge!({
item["name"].to_sym => EM.add_periodic_timer(item["period"]) do
self[index] = self.send item["type"], item["args"]
end
})
end
end
def time(hash = {"format" => "%Y-%m-%d %H:%M:%S"})
DateTime.now.strftime hash["format"]
end
def file(hash = {})
File.read(hash["path"]).strip + hash["unit"]
end
def array_from_file(hash = {})
ends = hash["range"].split('..').map { |s| Integer(s) }
File.read(hash["path"]).strip.split[ends[0]..ends[1]].join " "
end
def formatted_string_from_file(hash = {})
sprintf hash["format"], File.read(hash["path"]).strip
end
end
opts = Trollop::options do
opt :config, "Configuration file", :type => :io, :default => File.open("#{ENV["HOME"]}/.dsd.conf")
opt :daemon, "Daemonize on startup", :type => :flag, :default => true
opt :debug, "Save debug log at dsd.log", :type => :flag, :default => false
opt :repl, "Start a repl", :type => :flag, :default => true
end
ConfigHash = YAML.parse(opts[:config].read).to_ruby
Daemons.daemonize({:app_name => "dsd", :backtrace => opts[:debug], :ontop => not(opts[:daemon])})
EM.run do
$statusbar = DSD.new(ConfigHash["statusbar"]) do
$statusbar.suppress = true
Xname.xname $statusbar.reverse.join " | "
EM.add_timer(0.5) { $statusbar.suppress = false }
end
EventMachine::start_server '127.0.0.1', ConfigHash["repl"]["port"], SimpleRepl if opts[:repl]
end