| 1 | package OSGi::Osgish::Shell; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Term::ShellUI; | ||
| 5 | use Term::ANSIColor qw(:constants); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | OSGi::Osgish::Shell - Facade to Term::ShellUI | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ }; | ||
| 22 | bless $self,(ref($class) || $class); | ||
| 23 | $self->_init; | ||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub term { | ||
| 28 | return shift->{term}; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub commands { | ||
| 32 | my $self = shift; | ||
| 33 | $self->{term}->commands(@_); | ||
| 34 | } | ||
| 35 | |||
| 36 | # Run ShellUI and never return. Provide some special | ||
| 37 | # ReadLine treatment | ||
| 38 | sub run { | ||
| 39 | my $self = shift; | ||
| 40 | my $t = $self->term; | ||
| 41 | |||
| 42 | #$t->{debug_complete}=5; | ||
| 43 | $t->run; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub color { | ||
| 47 | my $self = shift; | ||
| 48 | my @colors = @_; | ||
| 49 | my $args = ref($colors[$#colors]) eq "HASH" ? pop @colors : {}; | ||
| 50 | if ($self->use_color) { | ||
| 51 | if ($args->{escape}) { | ||
| 52 | return map { "\01" . $self->_resolve_color($_) . "\02" } @colors; | ||
| 53 | } else { | ||
| 54 | return map { $self->_resolve_color($_) } @colors; | ||
| 55 | } | ||
| 56 | } else { | ||
| 57 | return map { "" } @colors; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub color_theme { | ||
| 63 | return shift->_get_set("color_theme",@_); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub use_color { | ||
| 67 | my $self = shift; | ||
| 68 | my $value = shift; | ||
| 69 | if ($value) { | ||
| 70 | my $use_color = $value; | ||
| 71 | $use_color = 0 if $use_color =~ /^(no|never|false)$/i; | ||
| 72 | $self->{use_color} = $use_color; | ||
| 73 | } | ||
| 74 | return $self->{use_color}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _resolve_color { | ||
| 78 | my $self = shift; | ||
| 79 | my $c = shift; | ||
| 80 | my $color = $self->{color_theme}->{$c}; | ||
| 81 | if (exists($self->{color_theme}->{$c})) { | ||
| 82 | return defined($color) ? $color : ""; | ||
| 83 | } else { | ||
| 84 | return $c; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | # =========================================================================== | ||
| 89 | |||
| 90 | sub _init { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | # Create shell object | ||
| 94 | my $term = new Term::ShellUI( | ||
| 95 | history_file => "~/.agent_history", | ||
| 96 | ); | ||
| 97 | $self->{term} = $term; | ||
| 98 | $term->{term}->ornaments(0); | ||
| 99 | # Initial theme | ||
| 100 | my $theme_light = { | ||
| 101 | host => YELLOW, | ||
| 102 | bundle_active => GREEN . ON_WHITE, | ||
| 103 | bundle_installed => RED, | ||
| 104 | bundle_resolved => DARK . YELLOW, | ||
| 105 | bundle_fragment => CYAN, | ||
| 106 | service_id => DARK . GREEN, | ||
| 107 | service_interface => undef, | ||
| 108 | service_using => RED, | ||
| 109 | prompt_context => BLUE, | ||
| 110 | prompt_empty => RED, | ||
| 111 | upload_installed => DARK . GREEN, | ||
| 112 | upload_uninstalled => RED, | ||
| 113 | package_resolved => DARK . GREEN, | ||
| 114 | package_optional => DARK . YELLOW, | ||
| 115 | package_version => BLUE, | ||
| 116 | package_imported_from => RED, | ||
| 117 | package_exported_to => RED, | ||
| 118 | bundle_id => RED, | ||
| 119 | bundle_name => RED, | ||
| 120 | header_name => DARK . YELLOW, | ||
| 121 | header_value => "" | ||
| 122 | }; | ||
| 123 | my $theme_dark = { | ||
| 124 | host => YELLOW, | ||
| 125 | bundle_id => RED, | ||
| 126 | bundle_name => RED, | ||
| 127 | bundle_active => GREEN, | ||
| 128 | bundle_installed => RED, | ||
| 129 | bundle_resolved => YELLOW, | ||
| 130 | bundle_fragment => CYAN, | ||
| 131 | bundle_referenced => YELLOW, | ||
| 132 | bundle_version => CYAN, | ||
| 133 | service_id => GREEN, | ||
| 134 | service_interface => undef, | ||
| 135 | service_using => YELLOW, | ||
| 136 | service_registered => YELLOW, | ||
| 137 | prompt_context => CYAN, | ||
| 138 | prompt_empty => RED, | ||
| 139 | upload_installed => GREEN, | ||
| 140 | upload_uninstalled => RED, | ||
| 141 | package_resolved => GREEN, | ||
| 142 | package_optional => YELLOW, | ||
| 143 | package_version => CYAN, | ||
| 144 | package_imported_from => RED, | ||
| 145 | package_exported_to => RED, | ||
| 146 | header_name => YELLOW, | ||
| 147 | header_value => "" | ||
| 148 | }; | ||
| 149 | |||
| 150 | $self->{color_theme} = $theme_dark; | ||
| 151 | |||
| 152 | # Force pipe, quit if less than a screen-full. | ||
| 153 | my @args = ( | ||
| 154 | '-f', # force, needed for color output | ||
| 155 | # '-E', # Exit automatically at end of output | ||
| 156 | '-X' # no init | ||
| 157 | ); | ||
| 158 | if ($self->use_color) { | ||
| 159 | # Raw characters | ||
| 160 | push @args,'-r'; | ||
| 161 | } | ||
| 162 | if ($ENV{LESS}) { | ||
| 163 | my $l = ""; | ||
| 164 | for my $a (@args) { | ||
| 165 | $l .= $a . " " unless $ENV{LESS} =~ /$a/; | ||
| 166 | } | ||
| 167 | if (length($l)) { | ||
| 168 | chop $l; | ||
| 169 | $ENV{LESS} .= " " . $l; | ||
| 170 | } | ||
| 171 | } else { | ||
| 172 | $ENV{LESS} = join " ",@args; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | sub readline { | ||
| 177 | my $self = shift; | ||
| 178 | my $term = $self->term; | ||
| 179 | return $term->{term}->ReadLine; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | =head1 LICENSE | ||
| 184 | |||
| 185 | This file is part of osgish. | ||
| 186 | |||
| 187 | Osgish is free software: you can redistribute it and/or modify | ||
| 188 | it under the terms of the GNU General Public License as published by | ||
| 189 | the Free Software Foundation, either version 2 of the License, or | ||
| 190 | (at your option) any later version. | ||
| 191 | |||
| 192 | osgish is distributed in the hope that it will be useful, | ||
| 193 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 194 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 195 | GNU General Public License for more details. | ||
| 196 | |||
| 197 | You should have received a copy of the GNU General Public License | ||
| 198 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 199 | |||
| 200 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 201 | further details. | ||
| 202 | |||
| 203 | =head1 PROFESSIONAL SERVICES | ||
| 204 | |||
| 205 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 206 | general), you might want to have a look at www.consol.com Contact | ||
| 207 | roland.huss@consol.de for further information (or use the contact form at | ||
| 208 | http://www.consol.com/contact/) | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | roland@cpan.org | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | 1; | ||
| 217 |