« blib/lib/Devel/Trepan/CmdProcessor/Command/Subcmd/SubsubMgr.pm
↑ Summary
lib/Devel/Trepan/Interface/User.pm »
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2015, 2017-2018 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | use warnings; no warnings 'redefine'; no warnings 'once'; | ||
| 4 | use rlib '../../../../..'; | ||
| 5 | |||
| 6 | package Devel::Trepan::CmdProcessor::Command::Set::Highlight; | ||
| 7 | |||
| 8 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 9 | use Devel::Trepan::DB::LineCache; | ||
| 10 | |||
| 11 | @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd); | ||
| 12 | # Values inherited from parent | ||
| 13 | use vars @Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS; | ||
| 14 | |||
| 15 | our $SHORT_HELP = 'Set whether we use terminal highlighting'; | ||
| 16 | our $MIN_ABBREV = length('hi'); | ||
| 17 | =pod | ||
| 18 | |||
| 19 | =head2 Synopsis: | ||
| 20 | |||
| 21 | =cut | ||
| 22 | our $HELP = <<'HELP'; | ||
| 23 | =pod | ||
| 24 | |||
| 25 | B<set highlight> [reset | plain | lightbg | darkbg | off] | ||
| 26 | |||
| 27 | Set whether we use terminal highlighting; Permissable values are: | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item B<plain> | ||
| 32 | |||
| 33 | no terminal highlighting | ||
| 34 | |||
| 35 | =item B<off> | ||
| 36 | |||
| 37 | same as B<plain> | ||
| 38 | |||
| 39 | =item B<lightbg> | ||
| 40 | |||
| 41 | terminal background is light; this is the default | ||
| 42 | |||
| 43 | =item B<darkbg> | ||
| 44 | |||
| 45 | terminal background is dark or light foreground text | ||
| 46 | |||
| 47 | =back | ||
| 48 | |||
| 49 | If the first argument is B<reset>, we clear any existing color formatting | ||
| 50 | and recolor all source code output. | ||
| 51 | |||
| 52 | =head2 Examples: | ||
| 53 | |||
| 54 | set highlight off # no highlight | ||
| 55 | set highlight plain # same as above | ||
| 56 | set highlight darkbg # terminal has dark background | ||
| 57 | set highlight lightbg # terminal has light background | ||
| 58 | set highlight # same as above | ||
| 59 | set highlight reset lightbg # clear source-code cache and | ||
| 60 | # set for light background | ||
| 61 | set highlight reset # clear source-code cache | ||
| 62 | |||
| 63 | =head2 See also: | ||
| 64 | |||
| 65 | L<C<show highlight>|Devel::Trepan::CmdProcessor::Command::Show::Highlight> | ||
| 66 | |||
| 67 | =cut | ||
| 68 | HELP | ||
| 69 | |||
| 70 | my @choices = qw(reset plain lightbg darkbg off); | ||
| 71 | sub complete($$) | ||
| 72 | { | ||
| 73 | my ($self, $prefix) = @_; | ||
| 74 | Devel::Trepan::Complete::complete_token(\@choices, $prefix); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub get_highlight_type($$) | ||
| 78 | { | ||
| 79 | my ($self, $arg) = @_; | ||
| 80 | return 'lightbg' unless $arg; | ||
| 81 | if (grep {$arg eq $_} @choices) { | ||
| 82 | return $arg; | ||
| 83 | } else { | ||
| 84 | my $proc = $self->{proc}; | ||
| 85 | my $msg = sprintf('Expecting one of: %s; got "%s"', | ||
| 86 | join(', ', @choices), $arg); | ||
| 87 | $proc->errmsg($msg); | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub run($$) | ||
| 93 | { | ||
| 94 | my ($self, $args) = @_; | ||
| 95 | |||
| 96 | my $proc = $self->{proc}; | ||
| 97 | my $highlight_type = 'lightbg'; | ||
| 98 | if ((scalar @$args >= 3) && ('reset' eq $args->[2])) { | ||
| 99 | if (scalar @$args > 3) { | ||
| 100 | $highlight_type = $self->get_highlight_type($args->[3]); | ||
| 101 | } else { | ||
| 102 | $highlight_type = $proc->{settings}{'highlight'}; | ||
| 103 | return unless $highlight_type; | ||
| 104 | clear_file_format_cache(); | ||
| 105 | } | ||
| 106 | } elsif (scalar @$args == 2) { | ||
| 107 | $highlight_type = 'off'; | ||
| 108 | } else { | ||
| 109 | $highlight_type = $self->get_highlight_type($args->[2]); | ||
| 110 | return unless $highlight_type; | ||
| 111 | } | ||
| 112 | if ($highlight_type eq 'plain' || $highlight_type eq 'off') { | ||
| 113 | $highlight_type = undef ; | ||
| 114 | } else { | ||
| 115 | Devel::Trepan::DB::LineCache::color_setup($highlight_type); | ||
| 116 | } | ||
| 117 | $proc->{settings}{'highlight'} = $highlight_type; | ||
| 118 | $proc->set_prompt(); | ||
| 119 | my $show_cmd = $proc->{commands}->{'show'}; | ||
| 120 | $show_cmd->run(['show', 'highlight']); | ||
| 121 | } | ||
| 122 | |||
| 123 | unless (caller) { | ||
| 124 | # Demo it. | ||
| 125 | require Devel::Trepan::CmdProcessor::Mock; | ||
| 126 | my ($proc, $cmd) = | ||
| 127 | Devel::Trepan::CmdProcessor::Mock::subcmd_setup(); | ||
| 128 | Devel::Trepan::CmdProcessor::Mock::subcmd_demo_info($proc, $cmd); | ||
| 129 | |||
| 130 | $cmd->run($cmd->{prefix}, 'off'); | ||
| 131 | for my $arg ('', 're', 'foo') { | ||
| 132 | # use Enbugger('trepan'); Enbugger->stop(); | ||
| 133 | my @aref = $cmd->complete($arg); | ||
| 134 | printf "complete '%s': %s\n", $arg, join(', ', @aref); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; |