| 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 | use BarnOwl::Complete::Filter qw(complete_filter_name complete_filter_expr); |
|---|
| 10 | |
|---|
| 11 | my @all_colors = qw(default |
|---|
| 12 | black |
|---|
| 13 | blue |
|---|
| 14 | cyan |
|---|
| 15 | green |
|---|
| 16 | magenta |
|---|
| 17 | red |
|---|
| 18 | white |
|---|
| 19 | yellow); |
|---|
| 20 | |
|---|
| 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 | |
|---|
| 43 | sub complete_command { return sort @BarnOwl::all_commands; } |
|---|
| 44 | sub complete_color { return @all_colors; } |
|---|
| 45 | sub complete_variable { return @{BarnOwl::all_variables()}; } |
|---|
| 46 | sub complete_style { return @{BarnOwl::all_styles()}; } |
|---|
| 47 | |
|---|
| 48 | sub complete_help { |
|---|
| 49 | my $ctx = shift; |
|---|
| 50 | if($ctx->word == 1) { |
|---|
| 51 | return complete_command(); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 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 | |
|---|
| 67 | sub complete_filter { |
|---|
| 68 | my $ctx = shift; |
|---|
| 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 |
|---|
| 76 | return complete_flags($ctx, |
|---|
| 77 | [qw()], |
|---|
| 78 | { |
|---|
| 79 | "-c" => \&complete_color, |
|---|
| 80 | "-b" => \&complete_color, |
|---|
| 81 | }, |
|---|
| 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; |
|---|
| 89 | $ctx = $ctx->shift_words($idx); |
|---|
| 90 | return complete_filter_expr($ctx); |
|---|
| 91 | }, |
|---|
| 92 | stop_at_nonflag => 1 |
|---|
| 93 | ); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | sub complete_view { |
|---|
| 97 | my $ctx = shift; |
|---|
| 98 | if ($ctx->word == 1) { |
|---|
| 99 | return ("--home", "-d", "-r", "-s", complete_filter_name()); |
|---|
| 100 | } |
|---|
| 101 | if ($ctx->words->[1] eq "--home") { |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | if ($ctx->words->[1] eq "-d") { |
|---|
| 105 | $ctx = $ctx->shift_words(2); |
|---|
| 106 | return complete_filter_expr($ctx); |
|---|
| 107 | } |
|---|
| 108 | if ($ctx->words->[1] eq "-s") { |
|---|
| 109 | return complete_style(); |
|---|
| 110 | } |
|---|
| 111 | return; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 142 | BarnOwl::Completion::register_completer(help => \&complete_help); |
|---|
| 143 | BarnOwl::Completion::register_completer(filter => \&complete_filter); |
|---|
| 144 | BarnOwl::Completion::register_completer(view => \&complete_view); |
|---|
| 145 | BarnOwl::Completion::register_completer(show => \&complete_show); |
|---|
| 146 | BarnOwl::Completion::register_completer(getvar => \&complete_getvar); |
|---|
| 147 | BarnOwl::Completion::register_completer(set => \&complete_set); |
|---|
| 148 | BarnOwl::Completion::register_completer(unset => \&complete_set); |
|---|
| 149 | BarnOwl::Completion::register_completer(startup => \&complete_startup); |
|---|
| 150 | |
|---|
| 151 | 1; |
|---|