]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blobdiff - monitor.rb
Different initialization file location
[perso/Immae/Projets/Ruby/Monitor.git] / monitor.rb
index 249e2a3c7d63cfb93f1f66ff0a5ff8a043ad176d..09f9e59cf8bb109f157d6fa5fe4fbb65fb5d4ea8 100644 (file)
@@ -4,6 +4,8 @@ require "time"
 require "ncurses"
 require "inifile"
 require "open3"
+require 'optparse'
+require 'ostruct'
 
 class IO
     def readline_nonblock
@@ -18,6 +20,26 @@ class IO
     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
+
 class List_Win
   def initialize(inifile)
     @params = inifile
@@ -194,6 +216,7 @@ class Buff_Win
       if(force or (Time.now - @last_update > @params['Periodic'].to_i))
         @buffer.clear()
         spawn_proc()
+        clear()
       end
     end
     print_buffer()
@@ -287,8 +310,25 @@ class Buffer
   end
 end
 
-def read_ini()
-  inifile = IniFile.load('monitorrc')
+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
 
@@ -352,6 +392,10 @@ def redraw_all(list,bufwins,curr_bufwin)
   list.refresh()
   curr_bufwin.refresh()
 end
+
+
+options = OptParse.new().parse(ARGV)
+inifile = read_ini(find_ini(options.inifile))
 begin
   # initialize ncurses
   Ncurses.initscr
@@ -365,8 +409,6 @@ begin
   Ncurses.init_pair(10, Ncurses::COLOR_WHITE,   Ncurses::COLOR_BLACK)
 
 
-  inifile = read_ini()
-
   list = List_Win.new(inifile)
   bufwins = make_bufwins(inifile)
   entry = 0