| 1 | |||
| 2 | package App::FQStat::Input; | ||
| 3 | # App::FQStat is (c) 2007-2009 Steffen Mueller | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Term::ANSIScreen qw/RESET locate :keyboard/; | ||
| 11 | use Term::ReadKey; | ||
| 12 | |||
| 13 | use App::FQStat::Drawing qw/printline update_display/; | ||
| 14 | require App::FQStat::Actions; | ||
| 15 | use App::FQStat::Debug; | ||
| 16 | use App::FQStat::Colors qw(get_color); | ||
| 17 | |||
| 18 | use base 'Exporter'; | ||
| 19 | our %EXPORT_TAGS = ( | ||
| 20 | 'all' => [qw( | ||
| 21 | poll_user | ||
| 22 | get_input_key | ||
| 23 | select_multiple_jobs | ||
| 24 | select_job | ||
| 25 | )], | ||
| 26 | ); | ||
| 27 | our @EXPORT_OK = @{$EXPORT_TAGS{'all'}}; | ||
| 28 | |||
| 29 | |||
| 30 | # Poll user for info | ||
| 31 | sub poll_user { | ||
| 32 | warnenter if ::DEBUG > 1; | ||
| 33 | my $query = shift; | ||
| 34 | locate(2,1); | ||
| 35 | print get_color("user_input"); | ||
| 36 | printline(""); | ||
| 37 | locate(2,1); | ||
| 38 | print $query; | ||
| 39 | ReadMode 1; | ||
| 40 | my $input = <STDIN>; | ||
| 41 | ReadMode 3; | ||
| 42 | print RESET; | ||
| 43 | update_display(); | ||
| 44 | chomp $input; | ||
| 45 | return $input; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | # get new input key, blocking for KEY_POLL_INTERVAL seconds | ||
| 50 | sub get_input_key { | ||
| 51 | warnenter if ::DEBUG > 3; | ||
| 52 | my $timeout = shift; | ||
| 53 | $timeout ||= ::KEY_POLL_INTERVAL(); | ||
| 54 | my $key = ReadKey $timeout; | ||
| 55 | return $key; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | # select many jobs | ||
| 61 | # returns reference to array of selected jobs | ||
| 62 | # and the key that triggered the end of selection | ||
| 63 | sub select_multiple_jobs { | ||
| 64 | warnenter if ::DEBUG; | ||
| 65 | my @args = ('multiple', @_); | ||
| 66 | return _select_jobs(@args); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | # select single job | ||
| 71 | # returns reference to array of selected jobs | ||
| 72 | # and the key that triggered the end of selection | ||
| 73 | sub select_job { | ||
| 74 | warnenter if ::DEBUG; | ||
| 75 | my @args = ('single', @_); | ||
| 76 | return _select_jobs(@args); | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | # This implements the whole job selection thingy | ||
| 81 | sub _select_jobs { | ||
| 82 | warnenter if ::DEBUG > 1; | ||
| 83 | my $mode = shift; # can be "single" or "multiple". Defaults to "multiple". | ||
| 84 | my $is_multiple = $mode eq 'single' ? 0 : 1; | ||
| 85 | my $keys_toggle_return = shift || ['q']; | ||
| 86 | my $select_color = shift || get_color("selected_job"); | ||
| 87 | my $cursor_color = shift || get_color('selected_cursor'); | ||
| 88 | my $selected_start = shift; | ||
| 89 | |||
| 90 | # if one of these keys is found, we return | ||
| 91 | my %return_keys = map {($_ => 1)} @$keys_toggle_return; | ||
| 92 | |||
| 93 | my $cursor_pos; | ||
| 94 | { | ||
| 95 | lock($::DisplayOffset); | ||
| 96 | $cursor_pos = $::DisplayOffset; | ||
| 97 | } | ||
| 98 | my %selected; # holds the indices of selected lines with a color (select_color) | ||
| 99 | my %selected_mark; # holds indices of marked lines with mark ("* ") | ||
| 100 | if ($selected_start) { | ||
| 101 | %selected = map {($_ => $select_color)} @$selected_start; | ||
| 102 | %selected_mark = map {($_ => "*")} @$selected_start; | ||
| 103 | } | ||
| 104 | my $selectspan_begin; # holds the start of the selection span | ||
| 105 | |||
| 106 | # control key mappings | ||
| 107 | my %ckeys = ( | ||
| 108 | 'A' => sub { # up | ||
| 109 | $cursor_pos--; | ||
| 110 | $cursor_pos = 0 if $cursor_pos < 0; | ||
| 111 | lock($::DisplayOffset); | ||
| 112 | App::FQStat::Actions::scroll_up(1) if $cursor_pos < $::DisplayOffset; | ||
| 113 | }, | ||
| 114 | 'B' => sub { # down | ||
| 115 | $cursor_pos++; | ||
| 116 | $cursor_pos = @{$::Records}-1 if $cursor_pos >= @{$::Records}; | ||
| 117 | lock($::DisplayOffset); | ||
| 118 | App::FQStat::Actions::scroll_down(1) if $cursor_pos >= $::DisplayOffset+$::Termsize[1]-5; | ||
| 119 | }, | ||
| 120 | '5' => sub { # pgup | ||
| 121 | $cursor_pos -= $::Termsize[1]-5; | ||
| 122 | $cursor_pos = 0 if $cursor_pos < 0; | ||
| 123 | lock($::DisplayOffset); | ||
| 124 | App::FQStat::Actions::scroll_up($::Termsize[1]-5) if $cursor_pos < $::DisplayOffset; | ||
| 125 | }, | ||
| 126 | '6' => sub { # pgdown | ||
| 127 | $cursor_pos += $::Termsize[1]-5; | ||
| 128 | $cursor_pos = @{$::Records}-1 if $cursor_pos >= @{$::Records}; | ||
| 129 | lock($::DisplayOffset); | ||
| 130 | App::FQStat::Actions::scroll_down($::Termsize[1]-5) if $cursor_pos >= $::DisplayOffset+$::Termsize[1]-5; | ||
| 131 | }, | ||
| 132 | 'H' => sub { # pos1 | ||
| 133 | $cursor_pos = 0; | ||
| 134 | App::FQStat::Actions::scroll_up(1e9); | ||
| 135 | }, | ||
| 136 | 'F' => sub { # end | ||
| 137 | $cursor_pos = @{$::Records}-1; | ||
| 138 | App::FQStat::Actions::scroll_down(1e9); | ||
| 139 | }, | ||
| 140 | ); | ||
| 141 | |||
| 142 | |||
| 143 | my $redraw = 1; | ||
| 144 | my @tsize = @::Termsize; | ||
| 145 | while (1) { | ||
| 146 | ::GetTermSize(); | ||
| 147 | if ($redraw or $tsize[0] != $::Termsize[0] or $tsize[1] != $::Termsize[1]) { | ||
| 148 | my %highlight = %selected; # merge cursor highlight and selection highlight | ||
| 149 | if (defined $selectspan_begin) { | ||
| 150 | # mark the whole selection span as cursor | ||
| 151 | my ($begin, $end) = ($selectspan_begin>$cursor_pos ? ($cursor_pos,$selectspan_begin) : ($selectspan_begin,$cursor_pos)); | ||
| 152 | $highlight{$_} = $cursor_color foreach ($begin..$end); | ||
| 153 | } | ||
| 154 | else { $highlight{$cursor_pos} = $cursor_color } | ||
| 155 | |||
| 156 | App::FQStat::Drawing::draw_header_line(); | ||
| 157 | App::FQStat::Drawing::draw_job_display(\%highlight, \%selected_mark); # highlight and mark | ||
| 158 | $redraw = 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | my $input = get_input_key(); | ||
| 162 | if (defined $input) { | ||
| 163 | if ($input =~ /\n/ or $input =~ /\r/ or $input eq ' ') { # select | ||
| 164 | if ($is_multiple and defined $selectspan_begin) { | ||
| 165 | _process_selectspan(\%selected, \%selected_mark, $selectspan_begin, $cursor_pos, $select_color); | ||
| 166 | $selectspan_begin = undef; # end span selection | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | # normal selection/deselection | ||
| 170 | _toggle_select_deselect(\%selected, \%selected_mark, $cursor_pos, $select_color); | ||
| 171 | |||
| 172 | # only allow a single selection if not in multi-mode | ||
| 173 | if (not $is_multiple) { | ||
| 174 | my $selected = [keys %selected]; | ||
| 175 | return($selected, $input); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | $redraw = 1; | ||
| 179 | } | ||
| 180 | elsif (exists $return_keys{$input}) { | ||
| 181 | # treat exit as selection in single-selection mode. | ||
| 182 | if (not $is_multiple) { | ||
| 183 | _toggle_select_deselect(\%selected, \%selected_mark, $cursor_pos, $select_color); | ||
| 184 | } | ||
| 185 | |||
| 186 | # wrap up and return | ||
| 187 | my $selected = [keys %selected]; | ||
| 188 | return($selected, $input); | ||
| 189 | } | ||
| 190 | elsif ($is_multiple and $input eq 's') { | ||
| 191 | if (defined $selectspan_begin) { | ||
| 192 | _process_selectspan(\%selected, \%selected_mark, $selectspan_begin, $cursor_pos, $select_color); | ||
| 193 | $selectspan_begin = undef; # end span selection | ||
| 194 | } | ||
| 195 | # start selection span | ||
| 196 | else { $selectspan_begin = $cursor_pos } | ||
| 197 | $redraw = 1; | ||
| 198 | } | ||
| 199 | elsif ($input eq '[') { # handle control keys | ||
| 200 | my $key = get_input_key(0.01); | ||
| 201 | if (defined $key and exists $ckeys{$key}) { | ||
| 202 | $ckeys{$key}->($key); | ||
| 203 | $redraw = 1; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } # end if defined input | ||
| 207 | } # end while | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | sub _toggle_select_deselect { | ||
| 212 | warnenter if ::DEBUG > 2; | ||
| 213 | my $select_hash = shift; | ||
| 214 | my $select_mark_hash = shift; | ||
| 215 | my $index = shift; | ||
| 216 | my $color = shift; | ||
| 217 | if (exists $select_hash->{$index}) { | ||
| 218 | delete $select_hash->{$index}; | ||
| 219 | delete $select_mark_hash->{$index}; | ||
| 220 | } | ||
| 221 | else { | ||
| 222 | $select_hash->{$index} = $color; | ||
| 223 | $select_mark_hash->{$index} = "*"; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | sub _process_selectspan { | ||
| 228 | warnenter if ::DEBUG > 2; | ||
| 229 | my ($selected, $selected_mark, $selectspan_begin, $cursor_pos, $select_color) = @_; | ||
| 230 | |||
| 231 | my ($begin, $end) = ( $selectspan_begin > $cursor_pos | ||
| 232 | ? ($cursor_pos, $selectspan_begin) | ||
| 233 | : ($selectspan_begin, $cursor_pos) ); | ||
| 234 | |||
| 235 | foreach my $index ($begin..$end) { | ||
| 236 | _toggle_select_deselect($selected, $selected_mark, $index, $select_color); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | 1; | ||
| 244 |