X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FRuby%2FMonitor.git;a=blobdiff_plain;f=windows.rb;fp=windows.rb;h=5b2357970fd1538199e27b57d1649c48ec6f4820;hp=0000000000000000000000000000000000000000;hb=7a1b77020cc139cc9240cd2c7ed2ae8990613d66;hpb=cfddfd90ca8df888683529fcf683958da0894b10 diff --git a/windows.rb b/windows.rb new file mode 100644 index 0000000..5b23579 --- /dev/null +++ b/windows.rb @@ -0,0 +1,205 @@ +class List_Win + def initialize(inifile) + @params = inifile + @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0) + @win.border(*([0]*8)) + @win.keypad(true) + @win.timeout(1000) + @win.refresh() + @entry = 0 + end + + def getch() + @win.getch() + end + def clear() + @win.clear() + end + def resize(x,y) + @win.resize(x,y) + end + def move(x,y) + @win.move(x,y) + end + def refresh() + @win.refresh() + end + + def print_list(entry=nil) + if(not entry.nil?) + @entry = entry + end + i = 0 + @params.each_section do |section| + if(@entry == i) + @win.attron(Ncurses::A_REVERSE) + end + @win.move(i+1,1) + print_line(@win,@params[section]['Name']) + if(@entry == i) + @win.attroff(Ncurses::A_REVERSE) + end + i = i+1 + end + @win.border(*([0]*8)) + @win.move(0,3) + @win.addstr("Menu") + @win.refresh() + end +end + +class Buff_Win + def initialize(winsize,winpos,params) + @params = params + @win = Ncurses::WINDOW.new(0, winsize, 0, winpos) + @panel = Ncurses::Panel::PANEL.new(@win) + if(@params['Type'] == 'oneshot') + @buffer = Buffer.new(0) + else + @buffer = Buffer.new(@params['Buffer'].to_i) + end + @proc = nil + @curr_offset = 0 + @hscroll = 0 + update_date() + + spawn_proc() + print_buffer() + end + + def move_resize(winsize,winpos) + newwin = Ncurses::WINDOW.new(0, winsize, 0, winpos) + Ncurses::Panel.replace_panel(@panel, newwin) + Ncurses.delwin(@win) + @win = newwin + print_buffer() + end + def update_date() + @last_update = Time.now + @date = @last_update.strftime("%F %R:%S") + end + + def win_border() + @win.border(*([0]*8)) + win_header() + end + def win_header() + @win.move(0,3) + @win.addnstr(@params['Name'],@win.getmaxx-@date.length-10) + @win.move(0,@win.getmaxx-@date.length-2) + @win.addstr(@date) + end + def refresh() + @win.refresh() + end + def clear() + @win.clear() + end + + def show_win() + Ncurses::Panel.top_panel(@panel) + Ncurses::Panel.update_panels + end + + def hscroll(scroll=0) + @hscroll += scroll + if(@hscroll < 0) + @hscroll = 0 + end + if(@hscroll > @buffer.maxlen()-@win.getmaxx+3) + @hscroll = @buffer.maxlen()-@win.getmaxx+3 + end + print_buffer() + refresh() + end + def scroll(scroll=0,goto=nil,fact=nil) + if (not fact.nil?) + scroll = (fact * @win.getmaxy.to_f).to_i + elsif (not goto.nil?) + @curr_offset = (goto * @buffer.size()).to_i + scroll = 0 + end + #@curr_offset -= @win.getmaxy/2 + @curr_offset += scroll + if(@curr_offset < 0) + @curr_offset = 0 + end + if(@curr_offset > @buffer.size()-@win.getmaxy+2) + @curr_offset = @buffer.size()-@win.getmaxy+2 + end + print_buffer() + refresh() + end + + def print_buffer() + #clear() + win_border() + j = 1 + @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type| + @win.move(j,1) + if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end + print_line(@win,l,hscroll=@hscroll) + if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end + j = j+1 + } + if(@buffer.has_before?) + @win.move(2,@win.getmaxx-1) + @win.attron(Ncurses::A_REVERSE) + @win.addstr("↑") + @win.attroff(Ncurses::A_REVERSE) + end + if(@buffer.has_after?) + @win.move(@win.getmaxy-2,@win.getmaxx-1) + @win.attron(Ncurses::A_REVERSE) + @win.addstr("↓") + @win.attroff(Ncurses::A_REVERSE) + end + end + + def proc_readlines() + begin + while true + @buffer.push(@proc.readline_nonblock) + update_date() + end + rescue IO::WaitReadable + end + end + + def update(force=false) + if(@params['Type'] == 'continuous') + proc_readlines() + end + if(@params['Type'] == 'oneshot') + if(force or (Time.now - @last_update > @params['Periodic'].to_i)) + @buffer.clear() + spawn_proc() + clear() + end + end + print_buffer() + end + + def spawn_proc() + if(@params['Type'] == 'oneshot') + update_date() + Open3.popen3(@params["Command"]) { |i,o,e,t| + while ((not o.eof?) or (not e.eof?)) + rs = IO.select([o,e],nil)[0] + r = (rs[0].eof?)? rs[1] : rs[0] + + if r.fileno == o.fileno + @buffer.push(r.readline) + elsif r.fileno == e.fileno + @buffer.push(r.readline,type=1) + end + end + } + elsif(@params['Type'] == 'continuous') + @proc = IO.popen(@params["Command"]) + proc_readlines() + end + end +end + +