]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blobdiff - windows.rb
Customization of window size
[perso/Immae/Projets/Ruby/Monitor.git] / windows.rb
index 5b2357970fd1538199e27b57d1649c48ec6f4820..5d195dd3edaa49be7d62d6ef5f7b6939a899ca3a 100644 (file)
@@ -1,7 +1,60 @@
+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
-    @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0)
+  def initialize(sections, size)
+    @params = sections
+    @win = Ncurses::WINDOW.new(0, size*Ncurses.COLS()/100, 0, 0)
     @win.border(*([0]*8))
     @win.keypad(true)
     @win.timeout(1000)
@@ -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
     }