| 1 | package Psh::Builtins::Strategy; | ||
| 2 | |||
| 3 | require Psh::Strategy; | ||
| 4 | |||
| 5 | =item * C<strategy list> shows the list of currently used strategies | ||
| 6 | |||
| 7 | =item * C<strategy available> shows the list of available strategies | ||
| 8 | |||
| 9 | =item * C<strategy add "name"> adds a strategy before the consume-all eval strategy | ||
| 10 | |||
| 11 | =item * C<strategy add "name" before "name"> inserts a strategy before the other one | ||
| 12 | |||
| 13 | =item * C<strategy add "name" after "name"> inserts a strategy after the other one | ||
| 14 | |||
| 15 | =item * C<strategy del "name"> removes a strategy | ||
| 16 | |||
| 17 | =item * C<strategy help "name"> shows help about a strategy | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub bi_strategy | ||
| 22 | { | ||
| 23 | my ($line, $words)= @_; | ||
| 24 | if( ! $words->[0]) { | ||
| 25 | require Psh::Builtins::Help; | ||
| 26 | Psh::Builtins::Help::bi_help('strategy'); | ||
| 27 | return (0,undef); | ||
| 28 | } elsif( $words->[0] eq 'add') { | ||
| 29 | my $strat= lc($words->[1]); | ||
| 30 | my $obj= Psh::Strategy::get($strat); | ||
| 31 | my $pos; | ||
| 32 | unless ($obj) { | ||
| 33 | Psh::Util::print_error_i18n('bi_strategy_notfound',$strat); | ||
| 34 | return (0,undef); | ||
| 35 | } | ||
| 36 | |||
| 37 | if( @{$words}>3) { | ||
| 38 | $pos= $words->[3]; | ||
| 39 | if( $pos !~ /^\d+$/) { | ||
| 40 | $pos= Psh::Strategy::find($pos); | ||
| 41 | if( $pos<0) { | ||
| 42 | Psh::Util::print_error_i18n('bi_strategy_notfound',$words->[3]); | ||
| 43 | return (0,undef); | ||
| 44 | } | ||
| 45 | } else { | ||
| 46 | $pos--; | ||
| 47 | } | ||
| 48 | if( $words->[2] eq 'after') { | ||
| 49 | $pos++; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | Psh::Strategy::add($obj,$pos) if $obj; | ||
| 53 | } elsif( $words->[0] eq 'del' || | ||
| 54 | $words->[0] eq 'remove') { | ||
| 55 | Psh::Strategy::remove($words->[1]); | ||
| 56 | } elsif( $words->[0] eq 'show' || | ||
| 57 | $words->[0] eq 'list') { | ||
| 58 | Psh::Util::print_out_i18n('bi_strategy_list'); | ||
| 59 | my @list= Psh::Strategy::list(); | ||
| 60 | for( my $i=0; $i<@list; $i++) { | ||
| 61 | Psh::Util::print_out(($i+1).") ".$list[$i]->name. | ||
| 62 | ($list[$i]->consumes == Psh::Strategy::CONSUME_LINE()?' (line)':''). | ||
| 63 | "\n"); | ||
| 64 | } | ||
| 65 | } elsif( $words->[0] eq 'help') { | ||
| 66 | require Psh::Builtins::Help; | ||
| 67 | if( @{$words}<2) { | ||
| 68 | Psh::Builtins::Help::bi_help('strategy'); | ||
| 69 | } else { | ||
| 70 | my $tmp=''; | ||
| 71 | foreach my $line (@INC) { | ||
| 72 | my $tmpfile= Psh::OS::catfile( | ||
| 73 | Psh::OS::catdir($line,'Psh','Strategy'), | ||
| 74 | ucfirst($words->[1]).'.pm'); | ||
| 75 | $tmp= Psh::Builtins::Help::get_pod_from_file($tmpfile, | ||
| 76 | $words->[1]); | ||
| 77 | last if $tmp; | ||
| 78 | } | ||
| 79 | if( $tmp ) { | ||
| 80 | Psh::OS::display_pod("=over 4\n".$tmp."\n=back\n"); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } elsif( $words->[0] eq 'available') { | ||
| 84 | my @list= Psh::Strategy::available_list(); | ||
| 85 | Psh::Util::print_list(@list); | ||
| 86 | } else { | ||
| 87 | Psh::Util::print_error_i18n('bi_strategy_wrong_arg'); | ||
| 88 | return (0,undef); | ||
| 89 | } | ||
| 90 | return (1,undef); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub cmpl_strategy { | ||
| 94 | my( $text, $dummy, $starttext) = @_; | ||
| 95 | my @words= split ' ',$starttext; | ||
| 96 | |||
| 97 | $Psh::Completion::ac=' '; | ||
| 98 | |||
| 99 | if( @words >= 4) { | ||
| 100 | return (1,grep { Psh::Util::starts_with($_,$text) } | ||
| 101 | @Psh::strategies); | ||
| 102 | } elsif( @words >= 3) { | ||
| 103 | return (1,grep { Psh::Util::starts_with($_,$text) } | ||
| 104 | qw(before after)); | ||
| 105 | } elsif( @words >= 2) { | ||
| 106 | if( $words[1] eq 'del' || $words[1] eq 'remove') { | ||
| 107 | return (1,grep { Psh::Util::starts_with($_,$text) } | ||
| 108 | @Psh::strategies); | ||
| 109 | } elsif( $words[1] eq 'help' || $words[1] eq 'add') { | ||
| 110 | return (1, grep { Psh::Util::starts_with($_,$text) } | ||
| 111 | _generate_strategy_list()); | ||
| 112 | } | ||
| 113 | } else { | ||
| 114 | return (1,grep { Psh::Util::starts_with($_,$text) } | ||
| 115 | qw(show list del help remove add available)); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | 1; |