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