| 1 | package Term::Output::List::Win32; | ||
| 2 | use 5.020; | ||
| 3 | use Moo 2; | ||
| 4 | use Scalar::Util 'weaken'; | ||
| 5 | use Win32::Console; | ||
| 6 | use experimental 'signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.06'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Term::Output::List::Win32 - output an updateable list of ongoing jobs to a Win32 console | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my $printer = Term::Output::List->new( | ||
| 17 | hook_warnings => 1, | ||
| 18 | ); | ||
| 19 | my @ongoing_tasks = ('file1: frobnicating', 'file2: bamboozling', 'file3: frobnicating'); | ||
| 20 | $printer->output_list(@ongoing_tasks); | ||
| 21 | |||
| 22 | $printer->output_permanent("Frobnicated gizmos"); # appears above the list | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | has '_last_lines' => ( | ||
| 27 | is => 'rw', | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 MEMBERS | ||
| 31 | |||
| 32 | =head2 C<< fh >> | ||
| 33 | |||
| 34 | Filehandle used for output. Default is C<< STDOUT >>. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | has 'fh' => ( | ||
| 39 | is => 'lazy', | ||
| 40 | default => sub { \*STDOUT }, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has 'ellipsis' => ( | ||
| 44 | is => 'lazy', | ||
| 45 | default => sub { "..." }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has 'console' => ( | ||
| 49 | is => 'lazy', | ||
| 50 | default => sub($s) { Win32::Console->new(STD_OUTPUT_HANDLE) }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | with 'Term::Output::List::Role'; | ||
| 54 | |||
| 55 | =head2 C<< interactive >> | ||
| 56 | |||
| 57 | Whether the script is run interactively and should output intermittent | ||
| 58 | updateable information | ||
| 59 | |||
| 60 | =head2 C<< hook_warnings >> | ||
| 61 | |||
| 62 | Install a hook for sending warnings to C<< ->output_permanent >>. This | ||
| 63 | prevents ugly tearing/overwriting when your code outputs warnings. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | =head1 METHODS | ||
| 68 | |||
| 69 | =head2 C<< Term::Output::List::Win32->new() >> | ||
| 70 | |||
| 71 | my $output = Term::Output::List::Win32->new( | ||
| 72 | hook_warnings => 1, | ||
| 73 | ) | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item C<< hook_warnings >> | ||
| 78 | |||
| 79 | Install a hook for sending warnings to C<< ->output_permanent >>. This | ||
| 80 | prevents ugly tearing/overwriting when your code outputs warnings. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | =head2 C<< width >> | ||
| 87 | |||
| 88 | Width of the terminal. This is initialized at first use. You may (or may not) | ||
| 89 | want to set up a C<< $SIG{WINCH} >> handler to set the terminal width when | ||
| 90 | the terminal size changes. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub width($self) { | ||
| 95 | my ($w,$h) = $self->console->Size(); | ||
| 96 | return $w | ||
| 97 | }; | ||
| 98 | |||
| 99 | =head2 C<< ->scroll_up >> | ||
| 100 | |||
| 101 | Helper method to place the cursor at the top of the updateable list. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub scroll_up( $self, $count=$self->_last_lines ) { | ||
| 106 | if( !$count) { | ||
| 107 | } else { | ||
| 108 | # Overwrite the number of lines we printed last time | ||
| 109 | my ($x,$y) = $self->console->Cursor; | ||
| 110 | $y //= 0; | ||
| 111 | my $diff = $y-$count; | ||
| 112 | $self->console->Title($count); | ||
| 113 | my $line = $diff < 0 ? 0 : $diff; | ||
| 114 | $self->console->Cursor( 0, $line ); | ||
| 115 | }; | ||
| 116 | } | ||
| 117 | |||
| 118 | =head2 C<<->output_permanent>> | ||
| 119 | |||
| 120 | $o->output_permanent("Frobnicated 3 items for job 2"); | ||
| 121 | $o->output_list("Frobnicating 9 items for job 1", | ||
| 122 | "Frobnicating 2 items for job 3", | ||
| 123 | ); | ||
| 124 | |||
| 125 | Outputs items that should go on the permanent record. It is expected to | ||
| 126 | output the (remaining) list of ongoing jobs after that. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub output_permanent( $self, @items ) { | ||
| 131 | my $total = $self->_last_lines // 0; | ||
| 132 | if( !$self->interactive ) { | ||
| 133 | print { $self->fh } join("\n", @items) . "\n"; | ||
| 134 | |||
| 135 | } else { | ||
| 136 | $self->scroll_up($total); | ||
| 137 | my $w = $self->width; | ||
| 138 | |||
| 139 | if( @items ) { | ||
| 140 | $self->do_clear_eol(scalar @items); | ||
| 141 | print { $self->fh } | ||
| 142 | join("\n", | ||
| 143 | map { s/\s*\z//r } | ||
| 144 | map { $self->_trim( $_, $w ) } | ||
| 145 | @items)."\n"; | ||
| 146 | }; | ||
| 147 | |||
| 148 | my $blank = $total - @items; | ||
| 149 | if( $blank > 0 ) { | ||
| 150 | $self->do_clear_eol($blank); | ||
| 151 | } | ||
| 152 | $self->fresh_output(); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 C<<->output_list @items>> | ||
| 157 | |||
| 158 | $o->output_list("Frobnicating 9 items for job 1", | ||
| 159 | "Frobnicating 2 items for job 3", | ||
| 160 | ); | ||
| 161 | |||
| 162 | Outputs items that can be updated later, as long as no intervening output | ||
| 163 | (like from C<print>, C<say> or C<warn>) has happened. If you want to output | ||
| 164 | lines that should not be overwritten later, see C<</->output_permanent>> | ||
| 165 | |||
| 166 | =head2 C<<->fresh_output >> | ||
| 167 | |||
| 168 | $o->fresh_output(); | ||
| 169 | |||
| 170 | Helper subroutine to make all items from the last output list remain as is. | ||
| 171 | |||
| 172 | For compatibility between output to a terminal and output without a terminal, | ||
| 173 | you should use C<< ->output_permanent >> for things that should be permanent | ||
| 174 | instead. | ||
| 175 | |||
| 176 | =head2 C<< ->do_clear_eol >> | ||
| 177 | |||
| 178 | $o->do_clear_eol(10); # clear 10 lines from the cursor down | ||
| 179 | |||
| 180 | Helper method to clear lines on the terminal. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub do_clear_eol( $self, $count=$self->_last_lines ) { | ||
| 185 | if( !$count) { | ||
| 186 | } else { | ||
| 187 | # Overwrite the number of lines we printed last time | ||
| 188 | my $c = $self->console; | ||
| 189 | my $w = $self->width; | ||
| 190 | my ($x,$y) = $c->Cursor; | ||
| 191 | $c->FillChar(' ',$w*$count, 0, $y ); | ||
| 192 | }; | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |