[f80ada8] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | # Completers for client state |
---|
| 5 | |
---|
| 6 | package BarnOwl::Complete::Client; |
---|
| 7 | |
---|
| 8 | use BarnOwl::Completion::Util qw(complete_flags); |
---|
| 9 | |
---|
[d4ecc78] | 10 | my @all_colors = qw(default |
---|
| 11 | black |
---|
| 12 | blue |
---|
| 13 | cyan |
---|
| 14 | green |
---|
| 15 | magenta |
---|
| 16 | red |
---|
| 17 | white |
---|
| 18 | yellow); |
---|
| 19 | |
---|
[1610e5b] | 20 | my %show = ( |
---|
| 21 | information => undef, |
---|
| 22 | colors => undef, |
---|
| 23 | commands => undef, |
---|
| 24 | command => \&complete_command, |
---|
| 25 | filters => undef, |
---|
| 26 | filter => \&complete_filter_name, |
---|
| 27 | license => undef, |
---|
| 28 | quickstart => undef, |
---|
| 29 | startup => undef, |
---|
| 30 | status => undef, |
---|
| 31 | styles => undef, |
---|
| 32 | subscriptions => undef, |
---|
| 33 | subs => undef, |
---|
| 34 | terminal => undef, |
---|
| 35 | variables => undef, |
---|
| 36 | variable => \&complete_variable, |
---|
| 37 | version => undef, |
---|
| 38 | view => undef, |
---|
| 39 | zpunts => undef, |
---|
| 40 | ); |
---|
| 41 | |
---|
[f80ada8] | 42 | sub complete_command { return sort @BarnOwl::all_commands; } |
---|
[d4ecc78] | 43 | sub complete_color { return @all_colors; } |
---|
[1610e5b] | 44 | sub complete_filter_name { return @{BarnOwl::all_filters()}; } |
---|
| 45 | sub complete_variable {} # stub |
---|
[f80ada8] | 46 | |
---|
[bc5e856] | 47 | my %filter_cmds = ( |
---|
| 48 | sender => \&BarnOwl::Complete::Zephyr::complete_user, |
---|
| 49 | recipient => \&BarnOwl::Complete::Zephyr::complete_user, |
---|
| 50 | class => \&BarnOwl::Complete::Zephyr::complete_class, |
---|
| 51 | instance => \&BarnOwl::Complete::Zephyr::complete_instance, |
---|
| 52 | opcode => \&BarnOwl::Complete::Zephyr::complete_opcode, |
---|
| 53 | realm => \&BarnOwl::Complete::Zephyr::complete_realm, |
---|
| 54 | body => undef, |
---|
| 55 | hostname => undef, |
---|
| 56 | type => sub { qw(zephyr aim admin); }, |
---|
| 57 | direction => sub { qw(in out none); }, |
---|
| 58 | login => sub { qw(login logout none); }, |
---|
| 59 | filter => \&complete_filter_name, |
---|
| 60 | perl => undef, |
---|
| 61 | ); |
---|
| 62 | |
---|
| 63 | # Returns: |
---|
| 64 | # - where to look next after pulling out an expression |
---|
| 65 | # - $INCOMPLETE if this cannot form a complete expression (or w/e) |
---|
| 66 | # - pushes to completion list as it finds valid completions |
---|
| 67 | |
---|
| 68 | my $INCOMPLETE = -1; |
---|
| 69 | sub _complete_filter_expr { |
---|
| 70 | # Takes as arguments context and the index into $ctx->words where the |
---|
| 71 | # filter expression starts |
---|
| 72 | my $ctx = shift; |
---|
| 73 | my $start = shift; |
---|
| 74 | my $o_comp = shift; |
---|
| 75 | my $end = $ctx->word; |
---|
| 76 | |
---|
| 77 | # Grab an expression; we don't allow empty |
---|
| 78 | my $i = $start; |
---|
| 79 | $i = _complete_filter_primitive_expr($ctx, $start, $o_comp); |
---|
| 80 | return $INCOMPLETE if $start == $INCOMPLETE; |
---|
| 81 | |
---|
| 82 | while ($i <= $end) { |
---|
| 83 | if ($i == $end) { |
---|
| 84 | # We could and/or another clause |
---|
| 85 | push @$o_comp, qw(and or); |
---|
| 86 | return $end; # Or we could let the parent do his thing |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if ($ctx->words->[$i] ne 'and' && $ctx->words->[$i] ne 'or') { |
---|
| 90 | return $i; # We're done. Let the parent do his thing |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | # Eat the and/or |
---|
| 94 | $i++; |
---|
| 95 | |
---|
| 96 | # Grab an expression |
---|
| 97 | $i = _complete_filter_primitive_expr($ctx, $i, $o_comp); |
---|
| 98 | return $INCOMPLETE if $i == $INCOMPLETE; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return $i; # Well, it looks like we're happy |
---|
| 102 | # (Actually, I'm pretty sure this never happens...) |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | sub _complete_filter_primitive_expr { |
---|
| 106 | my $ctx = shift; |
---|
| 107 | my $start = shift; |
---|
| 108 | my $o_comp = shift; |
---|
| 109 | my $end = $ctx->word; |
---|
| 110 | |
---|
| 111 | if ($start >= $end) { |
---|
| 112 | push @$o_comp, "("; |
---|
| 113 | push @$o_comp, qw(true false not); |
---|
| 114 | push @$o_comp, keys %filter_cmds; |
---|
| 115 | return $INCOMPLETE; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | my $word = $ctx->words->[$start]; |
---|
| 119 | if ($word eq "(") { |
---|
| 120 | $start = _complete_filter_expr($ctx, $start+1, $o_comp); |
---|
| 121 | return $INCOMPLETE if $start == $INCOMPLETE; |
---|
| 122 | |
---|
| 123 | # Now, we expect a ")" |
---|
| 124 | if ($start >= $end) { |
---|
| 125 | push @$o_comp, ")"; |
---|
| 126 | return $INCOMPLETE; |
---|
| 127 | } |
---|
| 128 | if ($ctx->words->[$start] ne ')') { |
---|
| 129 | # User is being confusing. Give up. |
---|
| 130 | return $INCOMPLETE; |
---|
| 131 | } |
---|
| 132 | return $start+1; # Eat the ) |
---|
| 133 | } elsif ($word eq "not") { |
---|
| 134 | # We just want another primitive expression |
---|
| 135 | return _complete_filter_primitive_expr($ctx, $start+1, $o_comp); |
---|
| 136 | } elsif ($word eq "true" || $word eq "false") { |
---|
| 137 | # No arguments |
---|
| 138 | return $start+1; # Eat the boolean. Mmmm, tasty. |
---|
| 139 | } else { |
---|
| 140 | # It's of the form 'CMD ARG' |
---|
| 141 | return $start+2 if ($start+1 < $end); # The user supplied the argument |
---|
| 142 | |
---|
| 143 | # complete the argument |
---|
| 144 | my $arg_func = $filter_cmds{$word}; |
---|
| 145 | push @$o_comp, ($arg_func ? ($arg_func->()) : ()); |
---|
| 146 | return $INCOMPLETE; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | sub complete_filter_expr { |
---|
| 151 | my $ctx = shift; |
---|
| 152 | my $start = shift; |
---|
| 153 | my @completions = (); |
---|
| 154 | _complete_filter_expr($ctx, $start, \@completions); |
---|
| 155 | # Get rid of duplicates and sort |
---|
| 156 | my %hash = (); |
---|
| 157 | @hash{@completions} = (); |
---|
| 158 | @completions = sort keys %hash; |
---|
| 159 | return @completions; |
---|
| 160 | } |
---|
| 161 | |
---|
[4fde21c] | 162 | sub complete_filter_args { |
---|
| 163 | my $ctx = shift; |
---|
| 164 | my $arg = shift; |
---|
[bc5e856] | 165 | return complete_filter_name() unless $arg; |
---|
| 166 | my $idx = 2; # skip the filter name |
---|
| 167 | while ($idx < $ctx->word) { |
---|
| 168 | last unless ($ctx->words->[$idx] =~ m{^-}); |
---|
| 169 | $idx += 2; # skip the flag and the argument |
---|
| 170 | } |
---|
| 171 | return complete_filter_expr($ctx, $idx); |
---|
[4fde21c] | 172 | } |
---|
| 173 | |
---|
[f80ada8] | 174 | sub complete_help { |
---|
| 175 | my $ctx = shift; |
---|
[7940ac2] | 176 | if($ctx->word == 1) { |
---|
| 177 | return complete_command(); |
---|
| 178 | } |
---|
[f80ada8] | 179 | } |
---|
| 180 | |
---|
[1610e5b] | 181 | sub complete_show { |
---|
| 182 | my $ctx = shift; |
---|
| 183 | if($ctx->word == 1) { |
---|
| 184 | return keys %show; |
---|
| 185 | } elsif ($ctx->word == 2) { |
---|
| 186 | my $cmd = $show{$ctx->words->[1]}; |
---|
| 187 | if($cmd) { |
---|
| 188 | return $cmd->($ctx); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
[d4ecc78] | 193 | sub complete_filter { |
---|
| 194 | my $ctx = shift; |
---|
| 195 | return complete_flags($ctx, |
---|
| 196 | [qw()], |
---|
| 197 | { |
---|
| 198 | "-c" => \&complete_color, |
---|
| 199 | "-b" => \&complete_color, |
---|
| 200 | }, |
---|
[4fde21c] | 201 | \&complete_filter_args |
---|
[d4ecc78] | 202 | ); |
---|
| 203 | } |
---|
| 204 | |
---|
[bd25f30] | 205 | sub complete_view { |
---|
| 206 | my $ctx = shift; |
---|
| 207 | if ($ctx->word == 1) { |
---|
| 208 | return ("--home", "-d", complete_filter_name()); |
---|
| 209 | } |
---|
| 210 | if ($ctx->words->[1] eq "--home") { |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | if ($ctx->words->[1] eq "-d") { |
---|
| 214 | return complete_filter_expr($ctx, 2); |
---|
| 215 | } |
---|
| 216 | return; |
---|
| 217 | } |
---|
| 218 | |
---|
[28cf94b] | 219 | sub complete_getvar { |
---|
| 220 | my $ctx = shift; |
---|
| 221 | return unless ($ctx->word == 1); |
---|
| 222 | return complete_variable(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | sub complete_set { |
---|
| 226 | my $ctx = shift; |
---|
| 227 | return complete_flags($ctx, |
---|
| 228 | [qw(-q)], |
---|
| 229 | { |
---|
| 230 | }, |
---|
| 231 | \&complete_set_args |
---|
| 232 | ); |
---|
| 233 | } |
---|
| 234 | sub complete_set_args { |
---|
| 235 | my $ctx = shift; |
---|
| 236 | my $arg = shift; |
---|
| 237 | return if $arg; |
---|
| 238 | return complete_variable(); |
---|
| 239 | } |
---|
| 240 | |
---|
[d4ecc78] | 241 | BarnOwl::Completion::register_completer(help => \&complete_help); |
---|
| 242 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
---|
[bd25f30] | 243 | BarnOwl::Completion::register_completer(view => \&complete_view); |
---|
[1610e5b] | 244 | BarnOwl::Completion::register_completer(show => \&complete_show); |
---|
[28cf94b] | 245 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
---|
| 246 | BarnOwl::Completion::register_completer(set => \&complete_set); |
---|
[f80ada8] | 247 | |
---|
| 248 | 1; |
---|