| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2017 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of PerlIO-via-EscStatus. | ||
| 4 | # | ||
| 5 | # PerlIO-via-EscStatus is free software; you can redistribute it and/or | ||
| 6 | # modify it under the terms of the GNU General Public License as published | ||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||
| 8 | # later version. | ||
| 9 | # | ||
| 10 | # PerlIO-via-EscStatus is distributed in the hope that it will be useful, | ||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 13 | # Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with PerlIO-via-EscStatus. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | package ProgressMonitor::Stringify::ToEscStatus; | ||
| 20 | use 5.008; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use ProgressMonitor::State qw(STATE_DONE); | ||
| 24 | use PerlIO::via::EscStatus; | ||
| 25 | |||
| 26 | our $VERSION = 12; | ||
| 27 | |||
| 28 | use classes | ||
| 29 | extends => 'ProgressMonitor::Stringify::AbstractMonitor', | ||
| 30 | new => 'new'; | ||
| 31 | |||
| 32 | # no private instance data to initialize | ||
| 33 | sub new { | ||
| 34 | my ($class, $cfg) = @_; | ||
| 35 | return $class->SUPER::_new($cfg, $CLASS); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub render { | ||
| 39 | my ($self) = @_; | ||
| 40 | my $cfg = $self->_get_cfg; | ||
| 41 | my $fh = $cfg->get_stream; | ||
| 42 | |||
| 43 | my $str; | ||
| 44 | if ($self->_get_state == STATE_DONE) { | ||
| 45 | $str = ''; | ||
| 46 | |||
| 47 | } else { | ||
| 48 | $str = $self->_toString (1); # with messages | ||
| 49 | if (my $pos = rindex ($str, "\n")) { # message part as ordinary print | ||
| 50 | print $fh substr ($str, 0, $pos+1); | ||
| 51 | $str = substr ($str, $pos+1); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | print $fh PerlIO::via::EscStatus::make_status ($str); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub setErrorMessage { | ||
| 58 | my ($self, $msg) = @_; | ||
| 59 | $msg = $self->SUPER::setErrorMessage ($msg); | ||
| 60 | if ($msg) { | ||
| 61 | my $fh = $self->_get_cfg->get_stream; | ||
| 62 | print $fh $msg,"\n"; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | package ProgressMonitor::Stringify::ToEscStatusConfiguration; | ||
| 68 | use strict; | ||
| 69 | use warnings; | ||
| 70 | use Scalar::Util; | ||
| 71 | |||
| 72 | use classes | ||
| 73 | extends => 'ProgressMonitor::Stringify::AbstractMonitorConfiguration', | ||
| 74 | attrs => ['stream']; | ||
| 75 | |||
| 76 | sub defaultAttributeValues { | ||
| 77 | my ($self) = @_; | ||
| 78 | my $hashref = $self->SUPER::defaultAttributeValues; | ||
| 79 | $hashref = { stream => \*STDOUT, | ||
| 80 | %$hashref }; | ||
| 81 | |||
| 82 | # override superclass default, since EscStatus truncates itself | ||
| 83 | $hashref->{'maxWidth'} = 999_999; | ||
| 84 | |||
| 85 | return $hashref; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub checkAttributeValues { | ||
| 89 | my ($self) = @_; | ||
| 90 | $self->SUPER::checkAttributeValues; | ||
| 91 | |||
| 92 | my $fh = $self->get_stream; | ||
| 93 | if (! Scalar::Util::openhandle ($fh)) { | ||
| 94 | X::Usage->throw ('not an open handle'); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | __END__ |