aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorImmae <immae@immae.eu>2014-06-18 23:50:55 +0200
committerImmae <immae@immae.eu>2014-06-18 23:50:55 +0200
commit7a1b77020cc139cc9240cd2c7ed2ae8990613d66 (patch)
tree00912cb19d534a768982a002b93dd4b8ae6395ad
parentcfddfd90ca8df888683529fcf683958da0894b10 (diff)
downloadMonitor-7a1b77020cc139cc9240cd2c7ed2ae8990613d66.tar.gz
Monitor-7a1b77020cc139cc9240cd2c7ed2ae8990613d66.tar.zst
Monitor-7a1b77020cc139cc9240cd2c7ed2ae8990613d66.zip
classes in separate files
-rw-r--r--buffer.rb67
-rw-r--r--monitor.rb272
-rw-r--r--windows.rb205
3 files changed, 274 insertions, 270 deletions
diff --git a/buffer.rb b/buffer.rb
new file mode 100644
index 0000000..006aaf4
--- /dev/null
+++ b/buffer.rb
@@ -0,0 +1,67 @@
1class Buffer
2 def initialize(size)
3 @size = size
4 @buff = []
5 @buff_type = []
6 @current = 0
7 @wrap = false
8 @before = false
9 @after = false
10 end
11 def size()
12 return @buff.length
13 end
14 def maxlen()
15 maxlen = 0
16 @buff.each do |string|
17 if string.length > maxlen
18 maxlen = string.length
19 end
20 end
21 return maxlen
22 end
23
24 def push(string,type=0)
25 if(string.chomp.empty?) then string = " " end
26 string.split( /\r?\n/ ).each do |line|
27 @buff[@current] = line
28 @buff_type[@current] = type
29 if(@size > 0)
30 @current = (1+@current) % @size
31 else
32 @current = 1+@current
33 end
34 if(@current == 0) then @wrap = true end
35 end
36 end
37 def yield(size,offset=0,&block)
38 if(size < 0) then size = 0 end
39 range = Range.new(0,@current-1).to_a
40 if(@wrap)
41 range = Range.new(@current,@size-1).to_a + range
42 end
43 range = range.last(size+offset)[0,size]
44 @before = (size+offset < @buff.length)
45 @after = (offset != 0 and size < @buff.length)
46 if(block)
47 range.each do |i|
48 yield [@buff[i],@buff_type[i]]
49 end
50 else
51 return range.collect{|r| [@buff[r],@buff_type[r]]}
52 end
53 end
54 def has_after?()
55 return @after
56 end
57 def has_before?()
58 return @before
59 end
60 def clear()
61 @current = 0
62 @buff = []
63 @buff_type = []
64 end
65end
66
67
diff --git a/monitor.rb b/monitor.rb
index 09f9e59..f61ce9b 100644
--- a/monitor.rb
+++ b/monitor.rb
@@ -6,6 +6,8 @@ require "inifile"
6require "open3" 6require "open3"
7require 'optparse' 7require 'optparse'
8require 'ostruct' 8require 'ostruct'
9require_relative 'buffer'
10require_relative 'windows'
9 11
10class IO 12class IO
11 def readline_nonblock 13 def readline_nonblock
@@ -40,276 +42,6 @@ class OptParse
40 end 42 end
41end 43end
42 44
43class List_Win
44 def initialize(inifile)
45 @params = inifile
46 @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0)
47 @win.border(*([0]*8))
48 @win.keypad(true)
49 @win.timeout(1000)
50 @win.refresh()
51 @entry = 0
52 end
53
54 def getch()
55 @win.getch()
56 end
57 def clear()
58 @win.clear()
59 end
60 def resize(x,y)
61 @win.resize(x,y)
62 end
63 def move(x,y)
64 @win.move(x,y)
65 end
66 def refresh()
67 @win.refresh()
68 end
69
70 def print_list(entry=nil)
71 if(not entry.nil?)
72 @entry = entry
73 end
74 i = 0
75 @params.each_section do |section|
76 if(@entry == i)
77 @win.attron(Ncurses::A_REVERSE)
78 end
79 @win.move(i+1,1)
80 print_line(@win,@params[section]['Name'])
81 if(@entry == i)
82 @win.attroff(Ncurses::A_REVERSE)
83 end
84 i = i+1
85 end
86 @win.border(*([0]*8))
87 @win.move(0,3)
88 @win.addstr("Menu")
89 @win.refresh()
90 end
91end
92
93class Buff_Win
94 def initialize(winsize,winpos,params)
95 @params = params
96 @win = Ncurses::WINDOW.new(0, winsize, 0, winpos)
97 @panel = Ncurses::Panel::PANEL.new(@win)
98 if(@params['Type'] == 'oneshot')
99 @buffer = Buffer.new(0)
100 else
101 @buffer = Buffer.new(@params['Buffer'].to_i)
102 end
103 @proc = nil
104 @curr_offset = 0
105 @hscroll = 0
106 update_date()
107
108 spawn_proc()
109 print_buffer()
110 end
111
112 def move_resize(winsize,winpos)
113 newwin = Ncurses::WINDOW.new(0, winsize, 0, winpos)
114 Ncurses::Panel.replace_panel(@panel, newwin)
115 Ncurses.delwin(@win)
116 @win = newwin
117 print_buffer()
118 end
119 def update_date()
120 @last_update = Time.now
121 @date = @last_update.strftime("%F %R:%S")
122 end
123
124 def win_border()
125 @win.border(*([0]*8))
126 win_header()
127 end
128 def win_header()
129 @win.move(0,3)
130 @win.addnstr(@params['Name'],@win.getmaxx-@date.length-10)
131 @win.move(0,@win.getmaxx-@date.length-2)
132 @win.addstr(@date)
133 end
134 def refresh()
135 @win.refresh()
136 end
137 def clear()
138 @win.clear()
139 end
140
141 def show_win()
142 Ncurses::Panel.top_panel(@panel)
143 Ncurses::Panel.update_panels
144 end
145
146 def hscroll(scroll=0)
147 @hscroll += scroll
148 if(@hscroll < 0)
149 @hscroll = 0
150 end
151 if(@hscroll > @buffer.maxlen()-@win.getmaxx+3)
152 @hscroll = @buffer.maxlen()-@win.getmaxx+3
153 end
154 print_buffer()
155 refresh()
156 end
157 def scroll(scroll=0,goto=nil,fact=nil)
158 if (not fact.nil?)
159 scroll = (fact * @win.getmaxy.to_f).to_i
160 elsif (not goto.nil?)
161 @curr_offset = (goto * @buffer.size()).to_i
162 scroll = 0
163 end
164 #@curr_offset -= @win.getmaxy/2
165 @curr_offset += scroll
166 if(@curr_offset < 0)
167 @curr_offset = 0
168 end
169 if(@curr_offset > @buffer.size()-@win.getmaxy+2)
170 @curr_offset = @buffer.size()-@win.getmaxy+2
171 end
172 print_buffer()
173 refresh()
174 end
175
176 def print_buffer()
177 #clear()
178 win_border()
179 j = 1
180 @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type|
181 @win.move(j,1)
182 if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end
183 print_line(@win,l,hscroll=@hscroll)
184 if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end
185 j = j+1
186 }
187 if(@buffer.has_before?)
188 @win.move(2,@win.getmaxx-1)
189 @win.attron(Ncurses::A_REVERSE)
190 @win.addstr("↑")
191 @win.attroff(Ncurses::A_REVERSE)
192 end
193 if(@buffer.has_after?)
194 @win.move(@win.getmaxy-2,@win.getmaxx-1)
195 @win.attron(Ncurses::A_REVERSE)
196 @win.addstr("↓")
197 @win.attroff(Ncurses::A_REVERSE)
198 end
199 end
200
201 def proc_readlines()
202 begin
203 while true
204 @buffer.push(@proc.readline_nonblock)
205 update_date()
206 end
207 rescue IO::WaitReadable
208 end
209 end
210
211 def update(force=false)
212 if(@params['Type'] == 'continuous')
213 proc_readlines()
214 end
215 if(@params['Type'] == 'oneshot')
216 if(force or (Time.now - @last_update > @params['Periodic'].to_i))
217 @buffer.clear()
218 spawn_proc()
219 clear()
220 end
221 end
222 print_buffer()
223 end
224
225 def spawn_proc()
226 if(@params['Type'] == 'oneshot')
227 update_date()
228 Open3.popen3(@params["Command"]) { |i,o,e,t|
229 while ((not o.eof?) or (not e.eof?))
230 rs = IO.select([o,e],nil)[0]
231 r = (rs[0].eof?)? rs[1] : rs[0]
232
233 if r.fileno == o.fileno
234 @buffer.push(r.readline)
235 elsif r.fileno == e.fileno
236 @buffer.push(r.readline,type=1)
237 end
238 end
239 }
240 elsif(@params['Type'] == 'continuous')
241 @proc = IO.popen(@params["Command"])
242 proc_readlines()
243 end
244 end
245end
246
247class Buffer
248 def initialize(size)
249 @size = size
250 @buff = []
251 @buff_type = []
252 @current = 0
253 @wrap = false
254 @before = false
255 @after = false
256 end
257 def size()
258 return @buff.length
259 end
260 def maxlen()
261 maxlen = 0
262 @buff.each do |string|
263 if string.length > maxlen
264 maxlen = string.length
265 end
266 end
267 return maxlen
268 end
269
270 def push(string,type=0)
271 if(string.chomp.empty?) then string = " " end
272 string.split( /\r?\n/ ).each do |line|
273 @buff[@current] = line
274 @buff_type[@current] = type
275 if(@size > 0)
276 @current = (1+@current) % @size
277 else
278 @current = 1+@current
279 end
280 if(@current == 0) then @wrap = true end
281 end
282 end
283 def yield(size,offset=0,&block)
284 if(size < 0) then size = 0 end
285 range = Range.new(0,@current-1).to_a
286 if(@wrap)
287 range = Range.new(@current,@size-1).to_a + range
288 end
289 range = range.last(size+offset)[0,size]
290 @before = (size+offset < @buff.length)
291 @after = (offset != 0 and size < @buff.length)
292 if(block)
293 range.each do |i|
294 yield [@buff[i],@buff_type[i]]
295 end
296 else
297 return range.collect{|r| [@buff[r],@buff_type[r]]}
298 end
299 end
300 def has_after?()
301 return @after
302 end
303 def has_before?()
304 return @before
305 end
306 def clear()
307 @current = 0
308 @buff = []
309 @buff_type = []
310 end
311end
312
313def find_ini(option_inifile) 45def find_ini(option_inifile)
314 if(not option_inifile.nil?) 46 if(not option_inifile.nil?)
315 inifile = option_inifile 47 inifile = option_inifile
diff --git a/windows.rb b/windows.rb
new file mode 100644
index 0000000..5b23579
--- /dev/null
+++ b/windows.rb
@@ -0,0 +1,205 @@
1class List_Win
2 def initialize(inifile)
3 @params = inifile
4 @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0)
5 @win.border(*([0]*8))
6 @win.keypad(true)
7 @win.timeout(1000)
8 @win.refresh()
9 @entry = 0
10 end
11
12 def getch()
13 @win.getch()
14 end
15 def clear()
16 @win.clear()
17 end
18 def resize(x,y)
19 @win.resize(x,y)
20 end
21 def move(x,y)
22 @win.move(x,y)
23 end
24 def refresh()
25 @win.refresh()
26 end
27
28 def print_list(entry=nil)
29 if(not entry.nil?)
30 @entry = entry
31 end
32 i = 0
33 @params.each_section do |section|
34 if(@entry == i)
35 @win.attron(Ncurses::A_REVERSE)
36 end
37 @win.move(i+1,1)
38 print_line(@win,@params[section]['Name'])
39 if(@entry == i)
40 @win.attroff(Ncurses::A_REVERSE)
41 end
42 i = i+1
43 end
44 @win.border(*([0]*8))
45 @win.move(0,3)
46 @win.addstr("Menu")
47 @win.refresh()
48 end
49end
50
51class Buff_Win
52 def initialize(winsize,winpos,params)
53 @params = params
54 @win = Ncurses::WINDOW.new(0, winsize, 0, winpos)
55 @panel = Ncurses::Panel::PANEL.new(@win)
56 if(@params['Type'] == 'oneshot')
57 @buffer = Buffer.new(0)
58 else
59 @buffer = Buffer.new(@params['Buffer'].to_i)
60 end
61 @proc = nil
62 @curr_offset = 0
63 @hscroll = 0
64 update_date()
65
66 spawn_proc()
67 print_buffer()
68 end
69
70 def move_resize(winsize,winpos)
71 newwin = Ncurses::WINDOW.new(0, winsize, 0, winpos)
72 Ncurses::Panel.replace_panel(@panel, newwin)
73 Ncurses.delwin(@win)
74 @win = newwin
75 print_buffer()
76 end
77 def update_date()
78 @last_update = Time.now
79 @date = @last_update.strftime("%F %R:%S")
80 end
81
82 def win_border()
83 @win.border(*([0]*8))
84 win_header()
85 end
86 def win_header()
87 @win.move(0,3)
88 @win.addnstr(@params['Name'],@win.getmaxx-@date.length-10)
89 @win.move(0,@win.getmaxx-@date.length-2)
90 @win.addstr(@date)
91 end
92 def refresh()
93 @win.refresh()
94 end
95 def clear()
96 @win.clear()
97 end
98
99 def show_win()
100 Ncurses::Panel.top_panel(@panel)
101 Ncurses::Panel.update_panels
102 end
103
104 def hscroll(scroll=0)
105 @hscroll += scroll
106 if(@hscroll < 0)
107 @hscroll = 0
108 end
109 if(@hscroll > @buffer.maxlen()-@win.getmaxx+3)
110 @hscroll = @buffer.maxlen()-@win.getmaxx+3
111 end
112 print_buffer()
113 refresh()
114 end
115 def scroll(scroll=0,goto=nil,fact=nil)
116 if (not fact.nil?)
117 scroll = (fact * @win.getmaxy.to_f).to_i
118 elsif (not goto.nil?)
119 @curr_offset = (goto * @buffer.size()).to_i
120 scroll = 0
121 end
122 #@curr_offset -= @win.getmaxy/2
123 @curr_offset += scroll
124 if(@curr_offset < 0)
125 @curr_offset = 0
126 end
127 if(@curr_offset > @buffer.size()-@win.getmaxy+2)
128 @curr_offset = @buffer.size()-@win.getmaxy+2
129 end
130 print_buffer()
131 refresh()
132 end
133
134 def print_buffer()
135 #clear()
136 win_border()
137 j = 1
138 @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type|
139 @win.move(j,1)
140 if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end
141 print_line(@win,l,hscroll=@hscroll)
142 if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end
143 j = j+1
144 }
145 if(@buffer.has_before?)
146 @win.move(2,@win.getmaxx-1)
147 @win.attron(Ncurses::A_REVERSE)
148 @win.addstr("↑")
149 @win.attroff(Ncurses::A_REVERSE)
150 end
151 if(@buffer.has_after?)
152 @win.move(@win.getmaxy-2,@win.getmaxx-1)
153 @win.attron(Ncurses::A_REVERSE)
154 @win.addstr("↓")
155 @win.attroff(Ncurses::A_REVERSE)
156 end
157 end
158
159 def proc_readlines()
160 begin
161 while true
162 @buffer.push(@proc.readline_nonblock)
163 update_date()
164 end
165 rescue IO::WaitReadable
166 end
167 end
168
169 def update(force=false)
170 if(@params['Type'] == 'continuous')
171 proc_readlines()
172 end
173 if(@params['Type'] == 'oneshot')
174 if(force or (Time.now - @last_update > @params['Periodic'].to_i))
175 @buffer.clear()
176 spawn_proc()
177 clear()
178 end
179 end
180 print_buffer()
181 end
182
183 def spawn_proc()
184 if(@params['Type'] == 'oneshot')
185 update_date()
186 Open3.popen3(@params["Command"]) { |i,o,e,t|
187 while ((not o.eof?) or (not e.eof?))
188 rs = IO.select([o,e],nil)[0]
189 r = (rs[0].eof?)? rs[1] : rs[0]
190
191 if r.fileno == o.fileno
192 @buffer.push(r.readline)
193 elsif r.fileno == e.fileno
194 @buffer.push(r.readline,type=1)
195 end
196 end
197 }
198 elsif(@params['Type'] == 'continuous')
199 @proc = IO.popen(@params["Command"])
200 proc_readlines()
201 end
202 end
203end
204
205