diff options
Diffstat (limited to 'monitor.rb')
-rw-r--r-- | monitor.rb | 111 |
1 files changed, 9 insertions, 102 deletions
@@ -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() |