[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); |
---|
[a3a9eb7] | 9 | use BarnOwl::Complete::Filter qw(complete_filter_name complete_filter_expr); |
---|
[f80ada8] | 10 | |
---|
[d4ecc78] | 11 | my @all_colors = qw(default |
---|
| 12 | black |
---|
| 13 | blue |
---|
| 14 | cyan |
---|
| 15 | green |
---|
| 16 | magenta |
---|
| 17 | red |
---|
| 18 | white |
---|
| 19 | yellow); |
---|
| 20 | |
---|
[1610e5b] | 21 | my %show = ( |
---|
| 22 | information => undef, |
---|
| 23 | colors => undef, |
---|
| 24 | commands => undef, |
---|
| 25 | command => \&complete_command, |
---|
| 26 | filters => undef, |
---|
| 27 | filter => \&complete_filter_name, |
---|
| 28 | license => undef, |
---|
| 29 | quickstart => undef, |
---|
| 30 | startup => undef, |
---|
| 31 | status => undef, |
---|
| 32 | styles => undef, |
---|
| 33 | subscriptions => undef, |
---|
| 34 | subs => undef, |
---|
| 35 | terminal => undef, |
---|
| 36 | variables => undef, |
---|
| 37 | variable => \&complete_variable, |
---|
| 38 | version => undef, |
---|
| 39 | view => undef, |
---|
| 40 | zpunts => undef, |
---|
| 41 | ); |
---|
| 42 | |
---|
[f80ada8] | 43 | sub complete_command { return sort @BarnOwl::all_commands; } |
---|
[d4ecc78] | 44 | sub complete_color { return @all_colors; } |
---|
[3ff3d86] | 45 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
---|
| 46 | sub complete_style { return @{BarnOwl::all_styles()}; } |
---|
[f80ada8] | 47 | |
---|
| 48 | sub complete_help { |
---|
| 49 | my $ctx = shift; |
---|
[7940ac2] | 50 | if($ctx->word == 1) { |
---|
| 51 | return complete_command(); |
---|
| 52 | } |
---|
[f80ada8] | 53 | } |
---|
| 54 | |
---|
[1610e5b] | 55 | sub complete_show { |
---|
| 56 | my $ctx = shift; |
---|
| 57 | if($ctx->word == 1) { |
---|
| 58 | return keys %show; |
---|
| 59 | } elsif ($ctx->word == 2) { |
---|
| 60 | my $cmd = $show{$ctx->words->[1]}; |
---|
| 61 | if($cmd) { |
---|
| 62 | return $cmd->($ctx); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
[d4ecc78] | 67 | sub complete_filter { |
---|
| 68 | my $ctx = shift; |
---|
[76e0e4a] | 69 | # Syntax: filter FILTERNAME FLAGS EXPR |
---|
| 70 | |
---|
| 71 | # FILTERNAME |
---|
| 72 | return complete_filter_name() if $ctx->word == 1; |
---|
| 73 | |
---|
| 74 | # FLAGS |
---|
| 75 | $ctx = $ctx->shift_words(1); # complete_flags starts at the second word |
---|
[d4ecc78] | 76 | return complete_flags($ctx, |
---|
| 77 | [qw()], |
---|
| 78 | { |
---|
| 79 | "-c" => \&complete_color, |
---|
| 80 | "-b" => \&complete_color, |
---|
| 81 | }, |
---|
[76e0e4a] | 82 | # EXPR |
---|
| 83 | sub { |
---|
| 84 | my $ctx = shift; |
---|
| 85 | my $arg = shift; |
---|
| 86 | |
---|
| 87 | # We pass stop_at_nonflag, so we can rewind to the start |
---|
| 88 | my $idx = $ctx->word - $arg; |
---|
[6035008] | 89 | $ctx = $ctx->shift_words($idx); |
---|
| 90 | return complete_filter_expr($ctx); |
---|
[76e0e4a] | 91 | }, |
---|
| 92 | stop_at_nonflag => 1 |
---|
[d4ecc78] | 93 | ); |
---|
| 94 | } |
---|
| 95 | |
---|
[bd25f30] | 96 | sub complete_view { |
---|
| 97 | my $ctx = shift; |
---|
| 98 | if ($ctx->word == 1) { |
---|
[6bc35b4] | 99 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
---|
[bd25f30] | 100 | } |
---|
| 101 | if ($ctx->words->[1] eq "--home") { |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | if ($ctx->words->[1] eq "-d") { |
---|
[6035008] | 105 | $ctx = $ctx->shift_words(2); |
---|
| 106 | return complete_filter_expr($ctx); |
---|
[bd25f30] | 107 | } |
---|
[6bc35b4] | 108 | if ($ctx->words->[1] eq "-s") { |
---|
| 109 | return complete_style(); |
---|
| 110 | } |
---|
[bd25f30] | 111 | return; |
---|
| 112 | } |
---|
| 113 | |
---|
[28cf94b] | 114 | sub complete_getvar { |
---|
| 115 | my $ctx = shift; |
---|
| 116 | return unless ($ctx->word == 1); |
---|
| 117 | return complete_variable(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | sub complete_set { |
---|
| 121 | my $ctx = shift; |
---|
| 122 | return complete_flags($ctx, |
---|
| 123 | [qw(-q)], |
---|
| 124 | { |
---|
| 125 | }, |
---|
| 126 | \&complete_set_args |
---|
| 127 | ); |
---|
| 128 | } |
---|
| 129 | sub complete_set_args { |
---|
| 130 | my $ctx = shift; |
---|
| 131 | my $arg = shift; |
---|
| 132 | return if $arg; |
---|
| 133 | return complete_variable(); |
---|
| 134 | } |
---|
| 135 | |
---|
[b06a888] | 136 | sub complete_startup { |
---|
| 137 | my $ctx = shift; |
---|
| 138 | my $new_ctx = $ctx->shift_words(1); |
---|
| 139 | return BarnOwl::Completion::get_completions($new_ctx); |
---|
| 140 | } |
---|
| 141 | |
---|
[d4ecc78] | 142 | BarnOwl::Completion::register_completer(help => \&complete_help); |
---|
| 143 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
---|
[bd25f30] | 144 | BarnOwl::Completion::register_completer(view => \&complete_view); |
---|
[1610e5b] | 145 | BarnOwl::Completion::register_completer(show => \&complete_show); |
---|
[28cf94b] | 146 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
---|
| 147 | BarnOwl::Completion::register_completer(set => \&complete_set); |
---|
[d5ccf4e8] | 148 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
---|
[b06a888] | 149 | BarnOwl::Completion::register_completer(startup => \&complete_startup); |
---|
[f80ada8] | 150 | |
---|
| 151 | 1; |
---|