]>
git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blob - windows.rb
5d195dd3edaa49be7d62d6ef5f7b6939a899ca3a
7 buffer
<< read_nonblock(1) while buffer
[-1] !
= "\n"
9 rescue IO
::WaitReadable => blocking
10 if (not buffer
.empty
?)
19 def print_line(str
, hscroll
=0)
21 str
[0,5].match(/\033\[3(.)m/) { |c
| #Line starts with an escape sequence. We handle that `a la xterm`
22 Ncurses
.init_pair(10, c
[1].to_i
, Ncurses
::COLOR_BLACK)
23 self.attron(Ncurses
.COLOR_PAIR(10))
25 str
= str
[5,str
.length
]
27 str
= str
.gsub("\011"," ")
28 #Any other control char is ignored and escaped
29 str
= str
.gsub(/[[:cntrl:]]/) { |m
|
33 strcut
= str
[hscroll
,str
.length
]
34 if(strcut
.nil? or strcut
.empty
?)
41 winlen
= self.getmaxx-self
.getcurx-1
43 self.addstr(str +
" "*(winlen-strlen
))
45 self.addstr(str
[0,winlen-1
]+
"…")
48 self.attroff(Ncurses
.COLOR_PAIR(10))
55 def initialize(sections
, size
)
57 @win = Ncurses
::WINDOW.new(0, size
*Ncurses
.COLS()/100, 0, 0)
81 def print_list(entry
=nil)
86 @params.each
{ |section_name
,section
|
88 @win.attron(Ncurses
::A_REVERSE)
91 @win.print_line(section
['Name'])
93 @win.attroff(Ncurses
::A_REVERSE)
105 def initialize(winsize
,winpos
,params
)
107 @win = Ncurses
::WINDOW.new(0, winsize
, 0, winpos
)
108 @panel = Ncurses
::Panel::PANEL.new(@win)
109 if(@params['Type'] == 'oneshot')
110 @buffer = Buffer
.new(0)
112 @buffer = Buffer
.new(@params['Buffer'].to_i
)
123 def move_resize(winsize
,winpos
)
124 newwin
= Ncurses
::WINDOW.new(0, winsize
, 0, winpos
)
125 Ncurses
::Panel.replace_panel(@panel, newwin
)
131 @last_update = Time
.now
132 @date = @last_update.strftime("%F %R:%S")
136 @win.border(*([0]*8))
141 @win.addnstr(@params['Name'],@win.getmaxx-
@date.length-10
)
142 @win.move(0,@win.getmaxx-
@date.length-2
)
153 Ncurses
::Panel.top_panel(@panel)
154 Ncurses
::Panel.update_panels
157 def hscroll(scroll
=0)
162 if(@hscroll > @buffer.maxlen()-@win.getmaxx+
3)
163 @hscroll = @buffer.maxlen()-@win.getmaxx+
3
168 def scroll(scroll
=0,goto
=nil,fact
=nil)
170 scroll
= (fact
* @win.getmaxy
.to_f
).to_i
171 elsif (not goto
.nil?)
172 @curr_offset = (goto
* @buffer.size()).to_i
175 #@curr_offset -= @win.getmaxy/2
176 @curr_offset +
= scroll
180 if(@curr_offset > @buffer.size()-@win.getmaxy+
2)
181 @curr_offset = @buffer.size()-@win.getmaxy+
2
191 @buffer.yield(@win.getmaxy-2
,@curr_offset) { |l
,type
|
193 if(type
== 1) then @win.attron(Ncurses
.COLOR_PAIR(1)) end
194 @win.print_line(l
,hscroll
=@hscroll)
195 if(type
== 1) then @win.attroff(Ncurses
.COLOR_PAIR(1)) end
198 if(@buffer.has_before
?)
199 @win.move(2,@win.getmaxx-1
)
200 @win.attron(Ncurses
::A_REVERSE)
202 @win.attroff(Ncurses
::A_REVERSE)
204 if(@buffer.has_after
?)
205 @win.move(@win.getmaxy-2
,@win.getmaxx-1
)
206 @win.attron(Ncurses
::A_REVERSE)
208 @win.attroff(Ncurses
::A_REVERSE)
215 @buffer.push(@proc.readline_nonblock
)
218 rescue IO
::WaitReadable
222 def update(force
=false)
223 if(@params['Type'] == 'continuous')
226 if(@params['Type'] == 'oneshot')
227 if(force
or (Time
.now
- @last_update > @params['Periodic'].to_i
))
237 if(@params['Type'] == 'oneshot')
239 Open3
.popen3(@params["Command"]) { |i
,o
,e
,t
|
240 while ((not o
.eof
?) or (not e
.eof
?))
241 rs
= IO
.select([o
,e
],nil)[0]
242 r
= (rs
[0].eof
?)? rs
[1] : rs
[0]
244 if r
.fileno
== o
.fileno
245 @buffer.push(r
.readline
)
246 elsif r
.fileno
== e
.fileno
247 @buffer.push(r
.readline
,type
=1)
251 elsif(@params['Type'] == 'continuous')
252 @proc = IO
.popen(@params["Command"])