diff options
author | Immae <ismael.bouya@normalesup.org> | 2014-06-29 23:08:46 +0200 |
---|---|---|
committer | Immae <ismael.bouya@normalesup.org> | 2014-06-29 23:08:46 +0200 |
commit | 332497b1a92f4257eea0c9257d041c8898c91c31 (patch) | |
tree | 828c96dbfe87b50badb2a09fadccb3736382a20d | |
parent | 7a1b77020cc139cc9240cd2c7ed2ae8990613d66 (diff) | |
download | Monitor-332497b1a92f4257eea0c9257d041c8898c91c31.tar.gz Monitor-332497b1a92f4257eea0c9257d041c8898c91c31.tar.zst Monitor-332497b1a92f4257eea0c9257d041c8898c91c31.zip |
Separation in sub-files
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | ini_read.rb | 91 | ||||
-rw-r--r-- | monitor.rb | 111 | ||||
-rw-r--r-- | windows.rb | 65 |
4 files changed, 159 insertions, 109 deletions
@@ -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 @@ | |||
1 | require 'optparse' | ||
2 | require 'ostruct' | ||
3 | require "inifile" | ||
4 | |||
5 | class 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 | ||
23 | end | ||
24 | |||
25 | |||
26 | class 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 | |||
90 | end | ||
91 | |||
@@ -1,110 +1,18 @@ | |||
1 | #!/usr/bin/env ruby | 1 | #!/usr/bin/env ruby |
2 | 2 | ||
3 | require "time" | ||
4 | require "ncurses" | 3 | require "ncurses" |
5 | require "inifile" | 4 | require_relative 'ini_read' |
6 | require "open3" | ||
7 | require 'optparse' | ||
8 | require 'ostruct' | ||
9 | require_relative 'buffer' | ||
10 | require_relative 'windows' | 5 | require_relative 'windows' |
6 | require_relative 'buffer' | ||
11 | 7 | ||
12 | class IO | 8 | def 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 | ||
23 | end | ||
24 | |||
25 | class 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 | ||
43 | end | ||
44 | |||
45 | def 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 | ||
56 | end | ||
57 | |||
58 | def 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 | ||
65 | end | ||
66 | |||
67 | def 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 | ||
98 | end | ||
99 | |||
100 | def 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 |
109 | end | 17 | end |
110 | 18 | ||
@@ -126,8 +34,7 @@ def redraw_all(list,bufwins,curr_bufwin) | |||
126 | end | 34 | end |
127 | 35 | ||
128 | 36 | ||
129 | options = OptParse.new().parse(ARGV) | 37 | inistruct = Ini_read.new() |
130 | inifile = read_ini(find_ini(options.inifile)) | ||
131 | begin | 38 | begin |
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() |
@@ -1,6 +1,59 @@ | |||
1 | require "time" | ||
2 | require "open3" | ||
3 | |||
4 | class 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 | ||
15 | end | ||
16 | |||
17 | |||
18 | class 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 | ||
51 | end | ||
52 | |||
53 | |||
1 | class List_Win | 54 | class 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 | } |