]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blobdiff - monitor.rb
Separation in sub-files
[perso/Immae/Projets/Ruby/Monitor.git] / monitor.rb
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()