From cfddfd90ca8df888683529fcf683958da0894b10 Mon Sep 17 00:00:00 2001 From: Immae Date: Mon, 16 Jun 2014 23:04:10 +0200 Subject: Different initialization file location --- monitor.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'monitor.rb') diff --git a/monitor.rb b/monitor.rb index 249e2a3..09f9e59 100644 --- a/monitor.rb +++ b/monitor.rb @@ -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 -- cgit v1.2.3