aboutsummaryrefslogtreecommitdiff
path: root/monitor.rb
diff options
context:
space:
mode:
Diffstat (limited to 'monitor.rb')
-rw-r--r--monitor.rb111
1 files changed, 9 insertions, 102 deletions
diff --git a/monitor.rb b/monitor.rb
index f61ce9b..65df79f 100644
--- a/monitor.rb
+++ b/monitor.rb
@@ -1,110 +1,18 @@
1#!/usr/bin/env ruby 1#!/usr/bin/env ruby
2 2
3require "time"
4require "ncurses" 3require "ncurses"
5require "inifile" 4require_relative 'ini_read'
6require "open3"
7require 'optparse'
8require 'ostruct'
9require_relative 'buffer'
10require_relative 'windows' 5require_relative 'windows'
6require_relative 'buffer'
11 7
12class IO 8def make_bufwins(sections)
13 def readline_nonblock
14 buffer = ""
15 buffer << read_nonblock(1) while buffer[-1] != "\n"
16 buffer
17 rescue IO::WaitReadable => blocking
18 if (not buffer.empty?)
19 ungetc(buffer)
20 end
21 raise blocking
22 end
23end
24
25class OptParse
26 def parse(args)
27
28 options = OpenStruct.new()
29 options.inifile = nil
30
31 opt_parser = OptionParser.new() do |opts|
32
33 opts.banner = "Usage: monitor [options]"
34 opts.separator ""
35
36 opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini|
37 options.inifile = ini
38 end
39 end
40 opt_parser.parse!(args)
41 return options
42 end
43end
44
45def find_ini(option_inifile)
46 if(not option_inifile.nil?)
47 inifile = option_inifile
48 elsif(ENV.has_key?('MONITOR_RC'))
49 inifile = ENV['MONITOR_RC']
50 elsif(Process.uid == 0)
51 inifile = "/etc/monitor.rc"
52 else
53 inifile = ENV['HOME']+"/.monitorrc"
54 end
55 return inifile
56end
57
58def read_ini(ini)
59 inifile = IniFile.load(ini)
60 if(inifile.nil?)
61 puts "Initialization file not found or not readable"
62 exit
63 end
64 return inifile
65end
66
67def print_line(win, str, hscroll=0)
68 revert_color = false
69 str[0,5].match(/\033\[3(.)m/) { |c| #Line starts with an escape sequence. We handle that `a la xterm`
70 Ncurses.init_pair(10, c[1].to_i, Ncurses::COLOR_BLACK)
71 win.attron(Ncurses.COLOR_PAIR(10))
72 revert_color = true
73 str = str[5,str.length]
74 }
75 str = str.gsub("\011"," ")
76 #Any other control char is ignored and escaped
77 str = str.gsub(/[[:cntrl:]]/) { |m|
78 "^"+(m.ord + 64).chr
79 }
80 if(hscroll > 0)
81 strcut = str[hscroll,str.length]
82 if(strcut.nil? or strcut.empty?)
83 str = ""
84 else
85 str = "…"+strcut
86 end
87 end
88 strlen = str.length
89 winlen = win.getmaxx-win.getcurx-1
90 if(strlen <= winlen)
91 win.addstr(str + " "*(winlen-strlen))
92 else
93 win.addstr(str[0,winlen-1]+"…")
94 end
95 if(revert_color)
96 win.attroff(Ncurses.COLOR_PAIR(10))
97 end
98end
99
100def make_bufwins(inifile)
101 bufwins = [] 9 bufwins = []
102 inifile.each_section do |section| 10 sections.each { |section_name,section|
103 bufwin = Buff_Win.new(Ncurses.COLS()-Ncurses.COLS()/4, 11 bufwin = Buff_Win.new(Ncurses.COLS()-Ncurses.COLS()/4,
104 Ncurses.COLS()/4, 12 Ncurses.COLS()/4,
105 inifile[section]) 13 section)
106 bufwins.push(bufwin) 14 bufwins.push(bufwin)
107 end 15 }
108 return bufwins 16 return bufwins
109end 17end
110 18
@@ -126,8 +34,7 @@ def redraw_all(list,bufwins,curr_bufwin)
126end 34end
127 35
128 36
129options = OptParse.new().parse(ARGV) 37inistruct = Ini_read.new()
130inifile = read_ini(find_ini(options.inifile))
131begin 38begin
132 # initialize ncurses 39 # initialize ncurses
133 Ncurses.initscr 40 Ncurses.initscr
@@ -141,8 +48,8 @@ begin
141 Ncurses.init_pair(10, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK) 48 Ncurses.init_pair(10, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK)
142 49
143 50
144 list = List_Win.new(inifile) 51 list = List_Win.new(inistruct.sections)
145 bufwins = make_bufwins(inifile) 52 bufwins = make_bufwins(inistruct.sections)
146 entry = 0 53 entry = 0
147 cur_bufwin = bufwins[entry] 54 cur_bufwin = bufwins[entry]
148 cur_bufwin.show_win() 55 cur_bufwin.show_win()