X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=monitor.rb;h=09f9e59cf8bb109f157d6fa5fe4fbb65fb5d4ea8;hb=cfddfd90ca8df888683529fcf683958da0894b10;hp=5e32cf28f2d5f83e66e42763f84d50193fc80f87;hpb=8b6154e92b39be265186c82ed0112d0926ae047c;p=perso%2FImmae%2FProjets%2FRuby%2FMonitor.git diff --git a/monitor.rb b/monitor.rb index 5e32cf2..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 @@ -152,6 +174,7 @@ class Buff_Win end def print_buffer() + #clear() win_border() j = 1 @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type| @@ -185,14 +208,15 @@ class Buff_Win end end - def update() + def update(force=false) if(@params['Type'] == 'continuous') proc_readlines() end if(@params['Type'] == 'oneshot') - if(Time.now - @last_update > @params['Periodic'].to_i) + if(force or (Time.now - @last_update > @params['Periodic'].to_i)) @buffer.clear() spawn_proc() + clear() end end print_buffer() @@ -257,6 +281,7 @@ class Buffer end end def yield(size,offset=0,&block) + if(size < 0) then size = 0 end range = Range.new(0,@current-1).to_a if(@wrap) range = Range.new(@current,@size-1).to_a + range @@ -285,12 +310,41 @@ 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 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?) @@ -306,6 +360,9 @@ def print_line(win, str, hscroll=0) else win.addstr(str[0,winlen-1]+"…") end + if(revert_color) + win.attroff(Ncurses.COLOR_PAIR(10)) + end end def make_bufwins(inifile) @@ -335,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 @@ -345,10 +406,9 @@ begin #Ncurses.stdscr.intrflush(false) # turn off flush-on-interrupt Ncurses.stdscr.keypad(true) # turn on keypad mode Ncurses.init_pair(1, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK) + Ncurses.init_pair(10, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK) - inifile = read_ini() - list = List_Win.new(inifile) bufwins = make_bufwins(inifile) entry = 0 @@ -369,6 +429,8 @@ begin list.print_list(entry=entry) when 12 #ctrl+L redraw_all(list,bufwins,cur_bufwin) + when 18 #ctrl+R + cur_bufwin.update(force=true) when Ncurses::KEY_RESIZE redraw_all(list,bufwins,cur_bufwin) when Ncurses::KEY_LEFT