X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=windows.rb;fp=windows.rb;h=96a45b94d6c81a3adaf8d6944ce36a45e7b669dc;hb=332497b1a92f4257eea0c9257d041c8898c91c31;hp=5b2357970fd1538199e27b57d1649c48ec6f4820;hpb=7a1b77020cc139cc9240cd2c7ed2ae8990613d66;p=perso%2FImmae%2FProjets%2FRuby%2FMonitor.git diff --git a/windows.rb b/windows.rb index 5b23579..96a45b9 100644 --- a/windows.rb +++ b/windows.rb @@ -1,6 +1,59 @@ +require "time" +require "open3" + +class IO + def readline_nonblock + buffer = "" + buffer << read_nonblock(1) while buffer[-1] != "\n" + buffer + rescue IO::WaitReadable => blocking + if (not buffer.empty?) + ungetc(buffer) + end + raise blocking + end +end + + +class Ncurses::WINDOW + def print_line(str, hscroll=0) + revert_color = false + str[0,5].match(/\033\[3(.)m/) { |c| #Line starts with an escape sequence. We handle that `a la xterm` + Ncurses.init_pair(10, c[1].to_i, Ncurses::COLOR_BLACK) + self.attron(Ncurses.COLOR_PAIR(10)) + revert_color = true + str = str[5,str.length] + } + str = str.gsub("\011"," ") + #Any other control char is ignored and escaped + str = str.gsub(/[[:cntrl:]]/) { |m| + "^"+(m.ord + 64).chr + } + if(hscroll > 0) + strcut = str[hscroll,str.length] + if(strcut.nil? or strcut.empty?) + str = "" + else + str = "…"+strcut + end + end + strlen = str.length + winlen = self.getmaxx-self.getcurx-1 + if(strlen <= winlen) + self.addstr(str + " "*(winlen-strlen)) + else + self.addstr(str[0,winlen-1]+"…") + end + if(revert_color) + self.attroff(Ncurses.COLOR_PAIR(10)) + end + end +end + + class List_Win - def initialize(inifile) - @params = inifile + def initialize(sections) + @params = sections @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0) @win.border(*([0]*8)) @win.keypad(true) @@ -30,17 +83,17 @@ class List_Win @entry = entry end i = 0 - @params.each_section do |section| + @params.each { |section_name,section| if(@entry == i) @win.attron(Ncurses::A_REVERSE) end @win.move(i+1,1) - print_line(@win,@params[section]['Name']) + @win.print_line(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") @@ -138,7 +191,7 @@ class Buff_Win @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) + @win.print_line(l,hscroll=@hscroll) if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end j = j+1 }