From cfddfd90ca8df888683529fcf683958da0894b10 Mon Sep 17 00:00:00 2001 From: Immae Date: Mon, 16 Jun 2014 23:04:10 +0200 Subject: [PATCH] Different initialization file location --- TODO | 14 +++----------- monitor.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 71df334..767b4e2 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ - sudo commands (ask for password) -- config in different places - adding/removing entries directly from the program - keep previous buffers for oneshot commands - custom key bindings (and informations in the window) @@ -8,16 +7,9 @@ - Key to force window update - Customization of window size - ~Internationalization -- Some bugs with scrolling - Respect colors from command? -bugs: -monitor.rb:264:in `last': negative array size (ArgumentError) - from monitor.rb:264:in `yield' - from monitor.rb:157:in `print_buffer' - from monitor.rb:95:in `move_resize' - from monitor.rb:345:in `block in redraw_all' - from monitor.rb:344:in `each' - from monitor.rb:344:in `redraw_all' - from monitor.rb:389:in `
' +Bugs: +- Some bugs with scrolling + 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 -- 2.41.0