]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blame - windows.rb
classes in separate files
[perso/Immae/Projets/Ruby/Monitor.git] / windows.rb
CommitLineData
7a1b7702
I
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