Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_ticker.lua
bloba8f3a9a29c5a01b14df451b39c49e7bc8e520c89
1 -- Authors: Matus Telgarsky <mtelgars@andrew.cmu.edu>
2 -- License: Unknown
3 -- Last Changed: Unknown
4 --
5 -- little ticker boondoggle
6 --
7 -- Selects amongst a set of specified commands and scrolls them not only line by line
8 -- but also within each line. Waits and marks ends of lines, beginning of command.
9 -- Should work great with an rss reader though I didn't care to try.
11 -- Author: Matus Telgarsky < mtelgars at andrew dot cmu dot edu >
13 -- I couldn't sleep and felt like learning lua.
17 local settings = {
18 timer = nil,
19 commands = {
20 'printf "hello world"',
21 'fortune',
22 'df --si',
24 random = true,
25 line_len = 50,
26 step = 2,
27 new = {
28 hint = "critical",
29 interval = 1 * 1000,
31 line = {
32 hint = "important",
33 interval = 1 * 1000,
35 scroll = {
36 hint = "normal",
37 interval = 0.375 * 1000,
39 update_fun = nil, --forward decl hack
42 local message = {
43 fd = nil,
44 s = nil,
45 len = nil,
46 pos = nil,
49 local function ticker_timer(style)
50 statusd.inform("ticker_hint", settings[style].hint)
51 settings.timer:set(settings[style].interval, settings.update_fun)
52 end
54 local function ticker_line_init()
55 message.len = string.len(message.s)
56 message.pos = 0
57 end
59 local function ticker_single()
60 return settings.commands[1]
61 end
63 local function ticker_random()
64 --don't do same twice, uniformly distribute chances across others
65 local newpos = math.random(1, settings.commands.count-1)
66 if newpos >= settings.commands.pos then
67 newpos = newpos + 1
68 end
70 settings.commands.pos = newpos
71 return settings.commands[settings.commands.pos]
72 end
74 local function ticker_rotate()
75 local c = settings.commands[settings.commands.pos]
77 if settings.commands.pos == settings.commands.count then
78 settings.commands.pos = 1
79 else
80 settings.commands.pos = settings.commands.pos + 1
81 end
83 return c
84 end
86 local function ticker_update()
87 if message.fd == nil then
88 message.fd = io.popen(settings.commands.get(), 'r')
89 message.s = message.fd:read()
90 if message.s then --XXX this is a bug workaround!
91 ticker_line_init()
92 end
93 ticker_timer('new')
94 elseif message.s == nil then
95 message.s = message.fd:read()
96 if message.s == nil then
97 message.fd:close()
98 message.fd = nil
99 else
100 ticker_line_init()
102 ticker_timer('line')
103 elseif message.pos + settings.line_len >= message.len then
104 message.s = nil --hang out at the end of a line
105 ticker_timer('line')
106 else
107 message.pos = message.pos + settings.step
108 ticker_timer('scroll')
111 if message.s ~= nil then
112 statusd.inform("ticker",
113 string.sub(message.s, message.pos, message.pos + settings.line_len))
117 if statusd ~= nil then
118 settings.timer = statusd.create_timer()
119 statusd.inform("ticker_template", string.rep('x', settings.line_len))
121 settings.update_fun = ticker_update
122 settings.commands.count = #(settings.commands)
123 if settings.commands.count == 1 then
124 settings.commands.get = ticker_single
125 elseif settings.random then
126 settings.commands.pos = 1
127 settings.commands.get = ticker_random
128 else
129 settings.commands.pos = 1
130 settings.commands.get = ticker_rotate
133 ticker_update()