aboutsummaryrefslogblamecommitdiff
path: root/monitor.rb
blob: f61ce9b8d534361191d46d7ae40222c2d6df934d (plain) (tree)
1
2
3
4
5
6
7
8
9
10





                   

                  

                          













                                                         



















                                                                                     


















                                                        



                                   











                                                                                                       














                                         


                                       




























                                                                        



                                             









                                                                  
                                                                     

 



















                                         

                                   

































                                                     
#!/usr/bin/env ruby

require "time"
require "ncurses"
require "inifile"
require "open3"
require 'optparse'
require 'ostruct'
require_relative 'buffer'
require_relative 'windows'

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)
  bufwins = []
  inifile.each_section do |section|
    bufwin = Buff_Win.new(Ncurses.COLS()-Ncurses.COLS()/4,
                           Ncurses.COLS()/4,
                           inifile[section])
    bufwins.push(bufwin)
  end
  return bufwins
end

def update_buffers(bufwins)
  bufwins.each do |bufwin|
    bufwin.update()
  end
end

def redraw_all(list,bufwins,curr_bufwin)
  bufwins.each do |bufwin|
    bufwin.move_resize(Ncurses.COLS()-Ncurses.COLS()/4,Ncurses.COLS()/4)
  end
  list.resize(Ncurses.LINES(), Ncurses.COLS()/4)
  list.clear()
  list.print_list()
  list.refresh()
  curr_bufwin.refresh()
end


options = OptParse.new().parse(ARGV)
inifile = read_ini(find_ini(options.inifile))
begin
  # initialize ncurses
  Ncurses.initscr
  Ncurses.start_color
  Ncurses.cbreak           # provide unbuffered input
  Ncurses.noecho           # turn off input echoing
  #Ncurses.nonl             # turn off newline translation
  #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)


  list = List_Win.new(inifile)
  bufwins = make_bufwins(inifile)
  entry = 0
  cur_bufwin = bufwins[entry]
  cur_bufwin.show_win()
  list.print_list()
  while(ch = list.getch()) do
    case(ch)
    when "n".ord
      entry = (entry +1) % bufwins.length
      cur_bufwin = bufwins[entry]
      cur_bufwin.show_win()
      list.print_list(entry=entry)
    when "p".ord
      entry = (entry -1) % bufwins.length
      cur_bufwin = bufwins[entry]
      cur_bufwin.show_win()
      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
      cur_bufwin.hscroll(scroll=-1)
    when Ncurses::KEY_RIGHT
      cur_bufwin.hscroll(scroll=1)
    when Ncurses::KEY_DOWN
      cur_bufwin.scroll(scroll=-1)
    when Ncurses::KEY_UP
      cur_bufwin.scroll(scroll=1)
    when Ncurses::KEY_NPAGE
      cur_bufwin.scroll(scroll=0,goto=nil,fact=-0.75)
    when Ncurses::KEY_PPAGE
      cur_bufwin.scroll(scroll=0,goto=nil,fact=0.75)
    when Ncurses::KEY_HOME
      cur_bufwin.scroll(scroll=0,goto=1.0)
    when Ncurses::KEY_END
      cur_bufwin.scroll(scroll=0,goto=0.0)
    when Ncurses::ERR
      update_buffers(bufwins)
      cur_bufwin.show_win()
    when "q".ord
      break
    else
      next
    end
  end

ensure
  Ncurses.echo
  Ncurses.nocbreak
  Ncurses.nl
  Ncurses.endwin
end