]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/commitdiff
Separation in sub-files
authorImmae <ismael.bouya@normalesup.org>
Sun, 29 Jun 2014 21:08:46 +0000 (23:08 +0200)
committerImmae <ismael.bouya@normalesup.org>
Sun, 29 Jun 2014 21:08:46 +0000 (23:08 +0200)
TODO
ini_read.rb [new file with mode: 0644]
monitor.rb
windows.rb

diff --git a/TODO b/TODO
index 767b4e2f9ad362c5ec2727e08c93b621d957af2b..2bb819c3003aa64fadee115b8326281731a340b0 100644 (file)
--- a/TODO
+++ b/TODO
@@ -4,7 +4,6 @@
 - custom key bindings (and informations in the window)
 - Man file
 - Check correctness of entries in config file (and capitalization)
-- Key to force window update
 - Customization of window size
 - ~Internationalization
 - Respect colors from command?
diff --git a/ini_read.rb b/ini_read.rb
new file mode 100644 (file)
index 0000000..9ae3f1b
--- /dev/null
@@ -0,0 +1,91 @@
+require 'optparse'
+require 'ostruct'
+require "inifile"
+
+class OptParse
+  def parse(args)
+    
+    options = OpenStruct.new()
+    options.inifile = nil
+
+    opt_parser = OptionParser.new() do |opts|
+      
+      opts.banner = "Usage: monitor [options]"
+      opts.separator ""
+
+      opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini|
+        options.inifile = ini
+      end
+    end
+    opt_parser.parse!(args)
+    return options
+  end
+end
+
+
+class Ini_read
+  attr_reader(:global,:sections)
+#  attr_reader :global
+#  attr_accessor :global
+  def initialize()
+    options = OptParse.new().parse(ARGV)
+    find_ini(options.inifile)
+    read_ini()
+    parse_config()
+  end
+
+  def find_ini(option_inifile)
+    if(not option_inifile.nil?)
+      @inifile = option_inifile
+    elsif(ENV.has_key?('MONITOR_RC'))
+      @inifile = ENV['MONITOR_RC']
+    elsif(Process.uid == 0)
+      @inifile = "/etc/monitor.rc"
+    else
+      @inifile = ENV['HOME']+"/.monitorrc"
+    end
+  end
+
+  def read_ini()
+    @config = IniFile.load(@inifile, :default=>'global')
+    if(@config.nil?)
+      puts "Initialization file not found or not readable"
+      exit
+    end
+  end
+  def each_section()
+    return @sections
+  end
+
+  def parse_config()
+    @global = {}
+    @sections = {}
+    @config.each_section do |section|
+      sec = @config[section]
+      if(section == "global")
+        @global = sec
+      end
+      if(not sec.has_key?('Type'))
+        puts "Section incomplete, ignored: "+ section
+        next
+      elsif(not sec.has_key?('Command'))
+        puts "Section incomplete, ignored: "+ section
+        next
+      end
+      if(sec['Type'] == "continuous")
+        if(not sec.has_key?('Buffer'))
+          sec['Buffer'] = 1000
+        end
+      end
+      if(sec['Type'] == "oneshot")
+        if(not sec.has_key?('Periodic'))
+          sec['Periodic'] = 600
+        end
+      end
+      @sections[section] = sec
+    end
+  end
+
+end
+
index f61ce9b8d534361191d46d7ae40222c2d6df934d..65df79f7028deab8de7201daed4f88405acd4967 100644 (file)
 #!/usr/bin/env ruby
 
-require "time"
 require "ncurses"
-require "inifile"
-require "open3"
-require 'optparse'
-require 'ostruct'
-require_relative 'buffer'
+require_relative 'ini_read'
 require_relative 'windows'
+require_relative 'buffer'
 
-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 OptParse
-  def parse(args)
-    
-    options = OpenStruct.new()
-    options.inifile = nil
-
-    opt_parser = OptionParser.new() do |opts|
-      
-      opts.banner = "Usage: monitor [options]"
-      opts.separator ""
-
-      opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini|
-        options.inifile = ini
-      end
-    end
-    opt_parser.parse!(args)
-    return options
-  end
-end
-
-def find_ini(option_inifile)
-  if(not option_inifile.nil?)
-    inifile = option_inifile
-  elsif(ENV.has_key?('MONITOR_RC'))
-    inifile = ENV['MONITOR_RC']
-  elsif(Process.uid == 0)
-    inifile = "/etc/monitor.rc"
-  else
-    inifile = ENV['HOME']+"/.monitorrc"
-  end
-  return inifile
-end
-
-def read_ini(ini)
-  inifile = IniFile.load(ini)
-  if(inifile.nil?)
-    puts "Initialization file not found or not readable"
-    exit
-  end
-  return inifile
-end
-
-def print_line(win, 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)
-    win.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 = win.getmaxx-win.getcurx-1
-  if(strlen <= winlen)
-    win.addstr(str + " "*(winlen-strlen))
-  else
-    win.addstr(str[0,winlen-1]+"…")
-  end
-  if(revert_color)
-    win.attroff(Ncurses.COLOR_PAIR(10))
-  end
-end
-
-def make_bufwins(inifile)
+def make_bufwins(sections)
   bufwins = []
-  inifile.each_section do |section|
+  sections.each { |section_name,section|
     bufwin = Buff_Win.new(Ncurses.COLS()-Ncurses.COLS()/4,
                            Ncurses.COLS()/4,
-                           inifile[section])
+                           section)
     bufwins.push(bufwin)
-  end
+  }
   return bufwins
 end
 
@@ -126,8 +34,7 @@ def redraw_all(list,bufwins,curr_bufwin)
 end
 
 
-options = OptParse.new().parse(ARGV)
-inifile = read_ini(find_ini(options.inifile))
+inistruct = Ini_read.new()
 begin
   # initialize ncurses
   Ncurses.initscr
@@ -141,8 +48,8 @@ begin
   Ncurses.init_pair(10, Ncurses::COLOR_WHITE,   Ncurses::COLOR_BLACK)
 
 
-  list = List_Win.new(inifile)
-  bufwins = make_bufwins(inifile)
+  list = List_Win.new(inistruct.sections)
+  bufwins = make_bufwins(inistruct.sections)
   entry = 0
   cur_bufwin = bufwins[entry]
   cur_bufwin.show_win()
index 5b2357970fd1538199e27b57d1649c48ec6f4820..96a45b94d6c81a3adaf8d6944ce36a45e7b669dc 100644 (file)
@@ -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
     }