| 1 | package ProgressMonitor::Stringify::Fields::Dynamic; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use ProgressMonitor::Exceptions; | ||
| 7 | require ProgressMonitor::Stringify::Fields::AbstractDynamicField if 0; | ||
| 8 | |||
| 9 | use classes | ||
| 10 | extends => 'ProgressMonitor::Stringify::Fields::AbstractDynamicField', | ||
| 11 | new => 'new', | ||
| 12 | attrs_pr => ['rendering',], | ||
| 13 | ; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my $cfg = shift; | ||
| 19 | |||
| 20 | my $self = $class->SUPER::_new($cfg, $CLASS); | ||
| 21 | |||
| 22 | $cfg = $self->_get_cfg; | ||
| 23 | |||
| 24 | $self->_set_width(length($cfg->get_text)); | ||
| 25 | |||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub widthChange | ||
| 30 | { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | my $cfg = $self->_get_cfg; | ||
| 34 | |||
| 35 | # recompute some vars | ||
| 36 | # | ||
| 37 | my $w = $self->get_width; | ||
| 38 | my $txt = $cfg->get_text; | ||
| 39 | $self->{$ATTR_rendering} = $txt . ($cfg->get_fillCharacter x ($w - length($txt))); | ||
| 40 | |||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub render | ||
| 45 | { | ||
| 46 | my $self = shift; | ||
| 47 | # arguments not used... | ||
| 48 | # my $state = shift; | ||
| 49 | # my $tick = shift; | ||
| 50 | # my $totalTicks = shift; | ||
| 51 | # my $clean = shift; | ||
| 52 | |||
| 53 | return $self->{$ATTR_rendering}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub change_text | ||
| 57 | { | ||
| 58 | my $self = shift; | ||
| 59 | my $newText = shift; | ||
| 60 | |||
| 61 | my $w = $self->get_width; | ||
| 62 | my $cfg = $self->_get_cfg; | ||
| 63 | |||
| 64 | my $oldText = $self->{$ATTR_rendering}; | ||
| 65 | |||
| 66 | $self->{$ATTR_rendering} = substr($newText . ($cfg->get_fillCharacter x $w), 0, $w); | ||
| 67 | |||
| 68 | return $oldText; | ||
| 69 | } | ||
| 70 | |||
| 71 | ### | ||
| 72 | |||
| 73 | package ProgressMonitor::Stringify::Fields::DynamicConfiguration; | ||
| 74 | |||
| 75 | use strict; | ||
| 76 | use warnings; | ||
| 77 | |||
| 78 | # Attributes | ||
| 79 | # text | ||
| 80 | # Set to any text that should be rendered | ||
| 81 | # fillCharacter | ||
| 82 | # Set to the filler used to fill out the dynamic width | ||
| 83 | # | ||
| 84 | use classes | ||
| 85 | extends => 'ProgressMonitor::Stringify::Fields::AbstractDynamicFieldConfiguration', | ||
| 86 | attrs => ['text', 'fillCharacter'], | ||
| 87 | ; | ||
| 88 | |||
| 89 | sub defaultAttributeValues | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | return {%{$self->SUPER::defaultAttributeValues()}, text => '', fillCharacter => ' '}; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub checkAttributeValues | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | $self->SUPER::checkAttributeValues; | ||
| 101 | |||
| 102 | X::Usage->throw("length of fillCharacter must have length 1") if length($self->get_fillCharacter) != 1; | ||
| 103 | |||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | ############################ | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | ProgressMonitor::Stringify::Field::Dynamic - a field implementation that renders a | ||
| 112 | fixed value in a dynamic space, filling out remaining places. | ||
| 113 | |||
| 114 | =head1 SYNOPSIS | ||
| 115 | |||
| 116 | TBW | ||
| 117 | |||
| 118 | =head1 DESCRIPTION | ||
| 119 | |||
| 120 | This is a dynamic size field rendering a fixed value, but filling out to adapt to | ||
| 121 | dynamically allocated space. Intended for use together with | ||
| 122 | other fields in order to provide explanatory text or similar, while also being good overlaid | ||
| 123 | messages and give as much space as possible. | ||
| 124 | |||
| 125 | Inherits from ProgressMonitor::Stringify::Fields::AbstractField. | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =over 2 | ||
| 130 | |||
| 131 | =item new( $hashRef ) | ||
| 132 | |||
| 133 | Configuration data: | ||
| 134 | |||
| 135 | =over 2 | ||
| 136 | |||
| 137 | =item text (default => '') | ||
| 138 | |||
| 139 | The text to display. | ||
| 140 | |||
| 141 | =item filler (default => ' ') | ||
| 142 | |||
| 143 | The filler. | ||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =over 2 | ||
| 150 | |||
| 151 | =item change_text( $newText) | ||
| 152 | |||
| 153 | Change the text to display and returns the old text. | ||
| 154 | |||
| 155 | The passed in text will be padded to the assigned width of the field if its shorter using the 'fillCharacter'. | ||
| 156 | If it's too long, it will be cut. | ||
| 157 | |||
| 158 | =back | ||
| 159 | |||
| 160 | =head1 AUTHOR | ||
| 161 | |||
| 162 | Kenneth Olwing, C<< <knth at cpan.org> >> | ||
| 163 | |||
| 164 | =head1 BUGS | ||
| 165 | |||
| 166 | I wouldn't be surprised! If you can come up with a minimal test that shows the | ||
| 167 | problem I might be able to take a look. Even better, send me a patch. | ||
| 168 | |||
| 169 | Please report any bugs or feature requests to | ||
| 170 | C<bug-progressmonitor at rt.cpan.org>, or through the web interface at | ||
| 171 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>. | ||
| 172 | I will be notified, and then you'll automatically be notified of progress on | ||
| 173 | your bug as I make changes. | ||
| 174 | |||
| 175 | =head1 SUPPORT | ||
| 176 | |||
| 177 | You can find general documentation for this module with the perldoc command: | ||
| 178 | |||
| 179 | perldoc ProgressMonitor | ||
| 180 | |||
| 181 | =head1 ACKNOWLEDGEMENTS | ||
| 182 | |||
| 183 | Thanks to my family. I'm deeply grateful for you! | ||
| 184 | |||
| 185 | =head1 COPYRIGHT & LICENSE | ||
| 186 | |||
| 187 | Copyright 2006,2007 Kenneth Olwing, all rights reserved. | ||
| 188 | |||
| 189 | This program is free software; you can redistribute it and/or modify it | ||
| 190 | under the same terms as Perl itself. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | 1; # End of ProgressMonitor::Stringify::Fields::Dynamic |