aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorImmae <ismael.bouya@normalesup.org>2014-06-29 23:08:46 +0200
committerImmae <ismael.bouya@normalesup.org>2014-06-29 23:08:46 +0200
commit332497b1a92f4257eea0c9257d041c8898c91c31 (patch)
tree828c96dbfe87b50badb2a09fadccb3736382a20d
parent7a1b77020cc139cc9240cd2c7ed2ae8990613d66 (diff)
downloadMonitor-332497b1a92f4257eea0c9257d041c8898c91c31.tar.gz
Monitor-332497b1a92f4257eea0c9257d041c8898c91c31.tar.zst
Monitor-332497b1a92f4257eea0c9257d041c8898c91c31.zip
Separation in sub-files
-rw-r--r--TODO1
-rw-r--r--ini_read.rb91
-rw-r--r--monitor.rb111
-rw-r--r--windows.rb65
4 files changed, 159 insertions, 109 deletions
diff --git a/TODO b/TODO
index 767b4e2..2bb819c 100644
--- a/TODO
+++ b/TODO
@@ -4,7 +4,6 @@
4- custom key bindings (and informations in the window) 4- custom key bindings (and informations in the window)
5- Man file 5- Man file
6- Check correctness of entries in config file (and capitalization) 6- Check correctness of entries in config file (and capitalization)
7- Key to force window update
8- Customization of window size 7- Customization of window size
9- ~Internationalization 8- ~Internationalization
10- Respect colors from command? 9- Respect colors from command?
diff --git a/ini_read.rb b/ini_read.rb
new file mode 100644
index 0000000..9ae3f1b
--- /dev/null
+++ b/ini_read.rb
@@ -0,0 +1,91 @@
1require 'optparse'
2require 'ostruct'
3require "inifile"
4
5class OptParse
6 def parse(args)
7
8 options = OpenStruct.new()
9 options.inifile = nil
10
11 opt_parser = OptionParser.new() do |opts|
12
13 opts.banner = "Usage: monitor [options]"
14 opts.separator ""
15
16 opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini|
17 options.inifile = ini
18 end
19 end
20 opt_parser.parse!(args)
21 return options
22 end
23end
24
25
26class Ini_read
27 attr_reader(:global,:sections)
28# attr_reader :global
29# attr_accessor :global
30 def initialize()
31 options = OptParse.new().parse(ARGV)
32 find_ini(options.inifile)
33 read_ini()
34 parse_config()
35 end
36
37 def find_ini(option_inifile)
38 if(not option_inifile.nil?)
39 @inifile = option_inifile
40 elsif(ENV.has_key?('MONITOR_RC'))
41 @inifile = ENV['MONITOR_RC']
42 elsif(Process.uid == 0)
43 @inifile = "/etc/monitor.rc"
44 else
45 @inifile = ENV['HOME']+"/.monitorrc"
46 end
47 end
48
49 def read_ini()
50 @config = IniFile.load(@inifile, :default=>'global')
51 if(@config.nil?)
52 puts "Initialization file not found or not readable"
53 exit
54 end
55 end
56
57 def each_section()
58 return @sections
59 end
60
61 def parse_config()
62 @global = {}
63 @sections = {}
64 @config.each_section do |section|
65 sec = @config[section]
66 if(section == "global")
67 @global = sec
68 end
69 if(not sec.has_key?('Type'))
70 puts "Section incomplete, ignored: "+ section
71 next
72 elsif(not sec.has_key?('Command'))
73 puts "Section incomplete, ignored: "+ section
74 next
75 end
76 if(sec['Type'] == "continuous")
77 if(not sec.has_key?('Buffer'))
78 sec['Buffer'] = 1000
79 end
80 end
81 if(sec['Type'] == "oneshot")
82 if(not sec.has_key?('Periodic'))
83 sec['Periodic'] = 600
84 end
85 end
86 @sections[section] = sec
87 end
88 end
89
90end
91
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()
diff --git a/windows.rb b/windows.rb
index 5b23579..96a45b9 100644
--- a/windows.rb
+++ b/windows.rb
@@ -1,6 +1,59 @@
1require "time"
2require "open3"
3
4class IO
5 def readline_nonblock
6 buffer = ""
7 buffer << read_nonblock(1) while buffer[-1] != "\n"
8 buffer
9 rescue IO::WaitReadable => blocking
10 if (not buffer.empty?)
11 ungetc(buffer)
12 end
13 raise blocking
14 end
15end
16
17
18class Ncurses::WINDOW
19 def print_line(str, hscroll=0)
20 revert_color = false
21 str[0,5].match(/\033\[3(.)m/) { |c| #Line starts with an escape sequence. We handle that `a la xterm`
22 Ncurses.init_pair(10, c[1].to_i, Ncurses::COLOR_BLACK)
23 self.attron(Ncurses.COLOR_PAIR(10))
24 revert_color = true
25 str = str[5,str.length]
26 }
27 str = str.gsub("\011"," ")
28 #Any other control char is ignored and escaped
29 str = str.gsub(/[[:cntrl:]]/) { |m|
30 "^"+(m.ord + 64).chr
31 }
32 if(hscroll > 0)
33 strcut = str[hscroll,str.length]
34 if(strcut.nil? or strcut.empty?)
35 str = ""
36 else
37 str = "…"+strcut
38 end
39 end
40 strlen = str.length
41 winlen = self.getmaxx-self.getcurx-1
42 if(strlen <= winlen)
43 self.addstr(str + " "*(winlen-strlen))
44 else
45 self.addstr(str[0,winlen-1]+"…")
46 end
47 if(revert_color)
48 self.attroff(Ncurses.COLOR_PAIR(10))
49 end
50 end
51end
52
53
1class List_Win 54class List_Win
2 def initialize(inifile) 55 def initialize(sections)
3 @params = inifile 56 @params = sections
4 @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0) 57 @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0)
5 @win.border(*([0]*8)) 58 @win.border(*([0]*8))
6 @win.keypad(true) 59 @win.keypad(true)
@@ -30,17 +83,17 @@ class List_Win
30 @entry = entry 83 @entry = entry
31 end 84 end
32 i = 0 85 i = 0
33 @params.each_section do |section| 86 @params.each { |section_name,section|
34 if(@entry == i) 87 if(@entry == i)
35 @win.attron(Ncurses::A_REVERSE) 88 @win.attron(Ncurses::A_REVERSE)
36 end 89 end
37 @win.move(i+1,1) 90 @win.move(i+1,1)
38 print_line(@win,@params[section]['Name']) 91 @win.print_line(section['Name'])
39 if(@entry == i) 92 if(@entry == i)
40 @win.attroff(Ncurses::A_REVERSE) 93 @win.attroff(Ncurses::A_REVERSE)
41 end 94 end
42 i = i+1 95 i = i+1
43 end 96 }
44 @win.border(*([0]*8)) 97 @win.border(*([0]*8))
45 @win.move(0,3) 98 @win.move(0,3)
46 @win.addstr("Menu") 99 @win.addstr("Menu")
@@ -138,7 +191,7 @@ class Buff_Win
138 @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type| 191 @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type|
139 @win.move(j,1) 192 @win.move(j,1)
140 if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end 193 if(type == 1) then @win.attron(Ncurses.COLOR_PAIR(1)) end
141 print_line(@win,l,hscroll=@hscroll) 194 @win.print_line(l,hscroll=@hscroll)
142 if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end 195 if(type == 1) then @win.attroff(Ncurses.COLOR_PAIR(1)) end
143 j = j+1 196 j = j+1
144 } 197 }