Jmx4Perl/J4psh/Shell.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 224 CC 24 SCAR 38.9
CC
24
Cov
0%
_init
156.0
color
20.0
_resolve_color
12.0
CRAP 49.0
1 package JMX::Jmx4Perl::J4psh::Shell;
2  
3 use strict;
4 use Term::ShellUI;
5 use Term::ANSIColor qw(:constants);
6 use Data::Dumper;
7  
8 =head1 NAME
9  
10 JMX::Jmx4Perl::J4psh::Shell - Facade to Term::ShellUI
11  
12 =head1 SYNOPSIS
13  
14 =head1 DESCRIPTION
15  
16 =head1 METHODS
17  
18 =cut
19  
20 my $USE_TERM_SIZE;
21 my $USE_SEARCH_PATH;
22 BEGIN {
23 $USE_TERM_SIZE = eval 'use Term::Size qw/chars/; 1';
24 $USE_SEARCH_PATH = eval 'use File::SearchPath qw/searchpath/; 1';
25 no warnings 'redefine';
26 *Text::Shellwords::Cursor::join_line = sub {
27 my $self = shift;
28 my $intoks = shift;
29  
30 my $str = "";
31 my $nsp = "";
32 my $last_tok = "";
33 for (@$intoks) {
34 $nsp = /^(['"])(.*)\1/ || $last_tok =~ /^(['"])(.*)\1/ ? "" : " ";
35 $str .= $nsp . $_;
36 $last_tok = $_;
37 }
38 $str =~ s/^\s*(.*?)\s*$/$1/;
39 return $str;
40 };
41 }
42  
43 sub new {
44 my $class = shift;
45 my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ };
46 bless $self,(ref($class) || $class);
47 $self->_init;
48 return $self;
49 }
50  
51 sub term {
52 return shift->{term};
53 }
54  
55 sub commands {
56 my $self = shift;
57 $self->{term}->commands(@_);
58 }
59  
60 # Run ShellUI and never return. Provide some special
61 # ReadLine treatment
62 sub run {
63 my $self = shift;
64 my $t = $self->term;
65
66 #$t->{debug_complete}=5;
67 $t->run;
68 }
69  
70 sub color {
71 my $self = shift;
72 my @colors = @_;
73 my $args = ref($colors[$#colors]) eq "HASH" ? pop @colors : {};
74 if ($self->use_color) {
75 if ($args->{escape}) {
76 return map { "\01" . $self->_resolve_color($_) . "\02" } @colors;
77 } else {
78 return map { $self->_resolve_color($_) } @colors;
79 }
80 } else {
81 return map { "" } @colors;
82 }
83 }
84  
85 sub color_theme {
86 return shift->_get_set("color_theme",@_);
87 }
88  
89 sub use_color {
90 my $self = shift;
91 my $value = shift;
92 if (defined($value)) {
93 $self->{use_color} = $value !~ /^(0|no|never|false)$/i;
94 }
95 return $self->{use_color};
96 }
97  
98  
99 sub _resolve_color {
100 my $self = shift;
101 my $c = shift;
102 my $color = $self->{color_theme}->{$c};
103 if (exists($self->{color_theme}->{$c})) {
104 return defined($color) ? $color : "";
105 } else {
106 return $c;
107 }
108 }
109  
110 # ===========================================================================
111  
112 sub _init {
113 my $self = shift;
114  
115 # Create shell object
116 my $term = new Term::ShellUI(
117 history_file => "~/.j4psh_history",
118 keep_quotes => 1,
119 );
120 $term->{parser}->{space_none} = "\"'";
121 $self->{term} = $term;
122 my $rl_attribs = $term->{term}->Attribs;
123 #$rl_attribs->{basic_word_break_characters} = " \t\n\\'`@$><;|&{(";
124 $rl_attribs->{completer_word_break_characters} = " \t\n\\";
125 $term->{term}->Attribs($rl_attribs);
126 $term->{term}->ornaments(0);
127  
128 my $config = $self->{config};
129 # Set color mode
130 $self->use_color(defined($self->{use_color}) || defined($config->{UseColor}) || "yes");
131 # Init color theme
132 $self->_init_theme($config->{theme});
133  
134 my $use_color = "yes";
135 if (exists $self->{args}->{color}) {
136 $use_color = $self->{args}->{color};
137 } elsif (exists $self->{config}->{usecolor}) {
138 $use_color = $self->{config}->{usecolor};
139 }
140 $self->use_color($use_color);
141  
142 # Force pipe, quit if less than a screen-full.
143 my @args = (
144 '-f', # force, needed for color output
145 # '-E', # Exit automatically at end of output
146 '-X' # no init
147 );
148 if ($self->use_color) {
149 # Raw control characters
150 push @args,'-R';
151 }
152 if ($ENV{LESS}) {
153 my $l = "";
154 for my $a (@args) {
155 $l .= $a . " " unless $ENV{LESS} =~ /$a/;
156 }
157 if (length($l)) {
158 chop $l;
159 $ENV{LESS} .= " " . $l;
160 }
161 } else {
162 $ENV{LESS} = join " ",@args;
163 }
164 if ($self->{config}->{pager}) {
165 $ENV{PAGER} = $self->{config}->{pager};
166 } elsif (!$ENV{PAGER}) {
167 # Try to find a suitable pager
168 if ($USE_SEARCH_PATH) {
169 for my $p (qw(less more)) {
170 my $pager = searchpath($p, env => 'PATH', exe => 1 );
171 if ($pager) {
172 $ENV{PAGER} = $pager;
173 last;
174 }
175 }
176 }
177 # No searching available, we rely on Term::Clue for finding the proper
178 # pager.
179 }
180
181 if ($ENV{PAGER} && $ENV{PAGER} =~ /more$/) {
182 # If we are using "more", disable coloring
183 $self->use_color("no");
184 }
185 }
186  
187 sub default_theme {
188 my $self = shift;
189 # Initial theme
190 my $theme_light = {
191 host => YELLOW,
192 prompt_context => BLUE,
193 prompt_empty => RED,
194 label => YELLOW,
195 domain_name => BLUE,
196 property_key => GREEN,
197 property_value => undef,
198 mbean_name => YELLOW,
199 attribute_name => GREEN,
200 operation_name => YELLOW,
201 stat_val => RED,
202 reset => RESET
203 };
204 my $theme_dark = {
205 host => YELLOW,
206 label => YELLOW,
207 prompt_context => CYAN,
208 prompt_empty => RED,
209 domain_name => YELLOW,
210 property_key => GREEN,
211 property_value => undef,
212 mbean_name => YELLOW,
213 attribute_name => GREEN,
214 operation_name => YELLOW,
215 stat_val => RED,
216 reset => RESET
217 };
218 return $theme_dark;
219 }
220  
221  
222 sub readline {
223 my $self = shift;
224 my $term = $self->term;
225 return $term->{term}->ReadLine;
226 }
227  
228 sub _init_theme {
229 my $self = shift;
230 my $theme_config = shift;
231 my $theme = $self->default_theme;
232 if ($theme_config) {
233 for my $k (keys %$theme_config) {
234 my $c = $theme_config->{$k};
235 $theme->{$k} = $c eq "undef" ? undef : Term::ANSIColor::color($c);
236 }
237 }
238 $self->{color_theme} = $theme;
239 return $theme;
240 }
241  
242 sub term_width {
243 return $USE_TERM_SIZE ? (chars())[0] : 120;
244 }
245  
246 sub term_height {
247 return $USE_TERM_SIZE ? (chars())[1] : 24;
248 }
249  
250 =head1 LICENSE
251  
252 This file is part of jmx4perl.
253  
254 Jmx4perl is free software: you can redistribute it and/or modify
255 it under the terms of the GNU General Public License as published by
256 the Free Software Foundation, either version 2 of the License, or
257 (at your option) any later version.
258  
259 jmx4perl is distributed in the hope that it will be useful,
260 but WITHOUT ANY WARRANTY; without even the implied warranty of
261 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262 GNU General Public License for more details.
263  
264 You should have received a copy of the GNU General Public License
265 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
266  
267 A commercial license is available as well. Please contact roland@cpan.org for
268 further details.
269  
270 =head1 PROFESSIONAL SERVICES
271  
272 Just in case you need professional support for this module (or Nagios or JMX in
273 general), you might want to have a look at
274 http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
275 further information (or use the contact form at http://www.consol.com/contact/)
276  
277 =head1 AUTHOR
278  
279 roland@cpan.org
280  
281 =cut
282  
283 1;
284