- custom key bindings (and informations in the window)
- Man file
- Check correctness of entries in config file (and capitalization)
-- Key to force window update
- Customization of window size
- ~Internationalization
- Respect colors from command?
--- /dev/null
+require 'optparse'
+require 'ostruct'
+require "inifile"
+
+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 Ini_read
+ attr_reader(:global,:sections)
+# attr_reader :global
+# attr_accessor :global
+ def initialize()
+ options = OptParse.new().parse(ARGV)
+ find_ini(options.inifile)
+ read_ini()
+ parse_config()
+ 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
+ end
+
+ def read_ini()
+ @config = IniFile.load(@inifile, :default=>'global')
+ if(@config.nil?)
+ puts "Initialization file not found or not readable"
+ exit
+ end
+ end
+
+ def each_section()
+ return @sections
+ end
+
+ def parse_config()
+ @global = {}
+ @sections = {}
+ @config.each_section do |section|
+ sec = @config[section]
+ if(section == "global")
+ @global = sec
+ end
+ if(not sec.has_key?('Type'))
+ puts "Section incomplete, ignored: "+ section
+ next
+ elsif(not sec.has_key?('Command'))
+ puts "Section incomplete, ignored: "+ section
+ next
+ end
+ if(sec['Type'] == "continuous")
+ if(not sec.has_key?('Buffer'))
+ sec['Buffer'] = 1000
+ end
+ end
+ if(sec['Type'] == "oneshot")
+ if(not sec.has_key?('Periodic'))
+ sec['Periodic'] = 600
+ end
+ end
+ @sections[section] = sec
+ end
+ end
+
+end
+
#!/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
end
-options = OptParse.new().parse(ARGV)
-inifile = read_ini(find_ini(options.inifile))
+inistruct = Ini_read.new()
begin
# initialize ncurses
Ncurses.initscr
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()
+require "time"
+require "open3"
+
+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 Ncurses::WINDOW
+ def print_line(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)
+ self.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 = self.getmaxx-self.getcurx-1
+ if(strlen <= winlen)
+ self.addstr(str + " "*(winlen-strlen))
+ else
+ self.addstr(str[0,winlen-1]+"…")
+ end
+ if(revert_color)
+ self.attroff(Ncurses.COLOR_PAIR(10))
+ end
+ end
+end
+
+
class List_Win
- def initialize(inifile)
- @params = inifile
+ def initialize(sections)
+ @params = sections
@win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0)
@win.border(*([0]*8))
@win.keypad(true)
@entry = entry
end
i = 0
- @params.each_section do |section|
+ @params.each { |section_name,section|
if(@entry == i)
@win.attron(Ncurses::A_REVERSE)
end
@win.move(i+1,1)
- print_line(@win,@params[section]['Name'])
+ @win.print_line(section['Name'])
if(@entry == i)
@win.attroff(Ncurses::A_REVERSE)
end
i = i+1
- end
+ }
@win.border(*([0]*8))
@win.move(0,3)
@win.addstr("Menu")
@buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type|
@win.move(j,1)
if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end
- print_line(@win,l,hscroll=@hscroll)
+ @win.print_line(l,hscroll=@hscroll)
if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end
j = j+1
}