« lib/Devel/Trepan/CmdProcessor/Command/Condition.pm
↑ Summary
blib/lib/Devel/Trepan/CmdProcessor/Command/Show_Subcmd/Aliases.pm »
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | use Exporter; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp (); | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | use rlib '../../..'; | ||
| 10 | use if !defined Devel::Trepan::CmdProcessor, Devel::Trepan::CmdProcessor; | ||
| 11 | use strict; | ||
| 12 | |||
| 13 | package Devel::Trepan::CmdProcessor::Command; | ||
| 14 | no warnings 'redefine'; | ||
| 15 | use Devel::Trepan::Util qw(hash_merge); | ||
| 16 | |||
| 17 | # BEGIN { | ||
| 18 | # if ($] < 5.022) { | ||
| 19 | # # Because we use Exporter we want to silence: | ||
| 20 | # # Use of inherited AUTOLOAD for non-method ... is deprecated | ||
| 21 | # sub AUTOLOAD | ||
| 22 | # { | ||
| 23 | # my $name = our $AUTOLOAD; | ||
| 24 | # $name =~ s/.*:://; # lose package name | ||
| 25 | # my $target = "DynaLoader::$name"; | ||
| 26 | # goto &$target; | ||
| 27 | # } | ||
| 28 | # } | ||
| 29 | # } | ||
| 30 | |||
| 31 | sub DESTROY {} | ||
| 32 | |||
| 33 | use Array::Columnize; | ||
| 34 | sub declared ($) { | ||
| 35 | use constant 1.01; # don't omit this! | ||
| 36 | my $name = shift; | ||
| 37 | $name =~ s/^::/main::/; | ||
| 38 | my $pkg = caller; | ||
| 39 | my $full_name = $name =~ /::/ ? $name : "${pkg}::$name"; | ||
| 40 | $constant::declared{$full_name}; | ||
| 41 | } | ||
| 42 | |||
| 43 | use vars qw(@CMD_VARS @EXPORT @ISA @CMD_ISA @ALIASES $HELP); | ||
| 44 | BEGIN { | ||
| 45 | @CMD_VARS = qw($HELP $NAME $NEED_RUNNING $NEED_STACK @CMD_VARS); | ||
| 46 | } | ||
| 47 | use vars @CMD_VARS; | ||
| 48 | @ISA = qw(Exporter); | ||
| 49 | |||
| 50 | @CMD_ISA = qw(Devel::Trepan::CmdProcessor::Command); | ||
| 51 | @EXPORT = qw(&set_name @CMD_ISA $NEED_RUNNING | ||
| 52 | $NEED_STACK @CMD_VARS declared); | ||
| 53 | |||
| 54 | |||
| 55 | use constant NEED_STACK => 0; # We'll say that commands which need a stack | ||
| 56 | # to run have to declare that and those that | ||
| 57 | # don't don't have to mention it. | ||
| 58 | unless (@ISA) { | ||
| 59 | eval <<'EOE'; | ||
| 60 | use constant CATEGORY => 'Each command should set a category'; | ||
| 61 | EOE | ||
| 62 | } | ||
| 63 | |||
| 64 | # use constant NEED_RUNNING = 0; # We'll say that commands which need a a currently | ||
| 65 | # # running program. It's possible we have a stack even though | ||
| 66 | # # the program isn't running, e.g. there was an exception. | ||
| 67 | # # and we've faked the stack. (If this is not so, we can | ||
| 68 | # # don't need this and can simple use $NEED_STACK. | ||
| 69 | |||
| 70 | $HELP = 'Each command should set help text text'; | ||
| 71 | |||
| 72 | sub set_name() { | ||
| 73 | my ($pkg, $file, $line) = caller; | ||
| 74 | lc(File::Basename::basename($file, '.pm')); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub new($$) { | ||
| 78 | my($class, $proc) = @_; | ||
| 79 | my $self = { | ||
| 80 | proc => $proc, | ||
| 81 | class => $class, | ||
| 82 | dbgr => $proc->{dbgr} | ||
| 83 | }; | ||
| 84 | my $base_prefix="Devel::Trepan::CmdProcessor::Command::"; | ||
| 85 | for my $field (@CMD_VARS) { | ||
| 86 | my $sigil = substr($field, 0, 1); | ||
| 87 | my $new_field = index('$@', $sigil) >= 0 ? substr($field, 1) : $field; | ||
| 88 | if ($sigil eq '$') { | ||
| 89 | $self->{lc $new_field} = | ||
| 90 | eval "\$${class}::${new_field} || \$${base_prefix}${new_field}"; | ||
| 91 | } elsif ($sigil eq '@') { | ||
| 92 | $self->{lc $new_field} = eval "[\@${class}::${new_field}]"; | ||
| 93 | } else { | ||
| 94 | die "Woah - bad sigil in variable $field: $sigil "; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | no warnings; | ||
| 98 | my @ary = eval "${class}::ALIASES()"; | ||
| 99 | $self->{aliases} = @ary ? [@ary] : []; | ||
| 100 | no strict 'refs'; | ||
| 101 | *{"${class}::Category"} = eval "sub { ${class}::CATEGORY() }"; | ||
| 102 | *{"${class}::name"} = eval "sub { \$${class}::NAME }"; | ||
| 103 | my $short_help = eval "${class}::SHORT_HELP()"; | ||
| 104 | $self->{short_help} = $short_help if $short_help; | ||
| 105 | bless $self, $class; | ||
| 106 | $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | # List command names aligned in columns | ||
| 110 | sub columnize_commands($$$) { | ||
| 111 | my ($self, $commands, $opts) = @_; | ||
| 112 | my $width = $self->{settings}{maxwidth}; | ||
| 113 | $opts = {} unless $opts; | ||
| 114 | $opts = hash_merge($opts, {displaywidth => $width, | ||
| 115 | colsep => ' ', | ||
| 116 | ljust => 1, | ||
| 117 | lineprefix => ' '}); | ||
| 118 | my $r = Array::Columnize::columnize($commands, $opts); | ||
| 119 | chomp $r; | ||
| 120 | return $r; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub columnize_numbers($$) { | ||
| 124 | my ($self, $commands) = @_; | ||
| 125 | my $width = $self->settings->{maxwidth}; | ||
| 126 | my $r = Array::Columnize::columnize($commands, | ||
| 127 | {displaywidth => $width, | ||
| 128 | colsep => ', ', | ||
| 129 | ljust => 0, | ||
| 130 | lineprefix => ' '}); | ||
| 131 | chomp $r; | ||
| 132 | return $r; | ||
| 133 | } | ||
| 134 | |||
| 135 | # FIXME: probably there is a way to do the delegation to proc methods | ||
| 136 | # without having type it all out. | ||
| 137 | |||
| 138 | sub confirm($$$) { | ||
| 139 | my ($self, $message, $default) = @_; | ||
| 140 | $self->{proc}->confirm($message, $default); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub errmsg($$;$) { | ||
| 144 | my ($self, $message, $opts) = @_; | ||
| 145 | $opts ||= {}; | ||
| 146 | $self->{proc}->errmsg([$message], $opts); | ||
| 147 | } | ||
| 148 | |||
| 149 | # sub obj_const($$$) { | ||
| 150 | # my ($self, $obj, $name) = @_; | ||
| 151 | # $obj->class.const_get($name) | ||
| 152 | # } | ||
| 153 | |||
| 154 | # Convenience short-hand for $self->{proc}->msg | ||
| 155 | sub msg($$;$) { | ||
| 156 | my ($self, $message, $opts) = @_; | ||
| 157 | $opts ||= {}; | ||
| 158 | $self->{proc}->msg($message, $opts); | ||
| 159 | } | ||
| 160 | |||
| 161 | # Convenience short-hand for $self->{proc}->msg_nocr | ||
| 162 | sub msg_nocr($$;$) { | ||
| 163 | my ($self, $message, $opts) = @_; | ||
| 164 | $opts ||= {}; | ||
| 165 | $self->{proc}->msg_nocr($message, $opts); | ||
| 166 | } | ||
| 167 | |||
| 168 | # The method that implements the debugger command. | ||
| 169 | sub run { | ||
| 170 | Carp::croak "RuntimeError: You need to define this method elsewhere"; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub section($$;$) { | ||
| 174 | my ($self, $message, $opts) = @_; | ||
| 175 | $opts ||={}; | ||
| 176 | $self->{proc}->section($message, $opts); | ||
| 177 | } | ||
| 178 | |||
| 179 | sub settings($) { | ||
| 180 | my ($self) = @_; | ||
| 181 | $self->{proc}{settings}; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub short_help($) { | ||
| 185 | my ($self) = @_; | ||
| 186 | return $self->{short_help} if defined $self->{short_help}; | ||
| 187 | my @ary = split("\n", $self->{help}); | ||
| 188 | $self->{short_help} = $ary[0]; | ||
| 189 | } | ||
| 190 | |||
| 191 | unless (caller) { | ||
| 192 | require Devel::Trepan::CmdProcessor::Mock; | ||
| 193 | my $proc = Devel::Trepan::CmdProcessor::Mock::setup(); | ||
| 194 | my $cmd = Devel::Trepan::CmdProcessor::Command->new($proc); | ||
| 195 | # print $cmd->short_help, "\n"; | ||
| 196 | # print $cmd, "\n"; | ||
| 197 | # print $cmd->Category, "\n"; | ||
| 198 | # print $cmd->{name}, "\n"; | ||
| 199 | # print $cmd->MIN_ARGS, "\n"; | ||
| 200 | # p cmd.complete('aa'); | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |