output.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #
  2. # Copyright 1999, 2000, 2001 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. package output;
  19. use strict;
  20. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  21. require Exporter;
  22. @ISA = qw(Exporter);
  23. @EXPORT = qw();
  24. @EXPORT_OK = qw($output);
  25. use vars qw($output);
  26. $output = '_output'->new;
  27. package _output;
  28. use strict;
  29. my $stdout_isatty = -t STDOUT;
  30. my $stderr_isatty = -t STDERR;
  31. sub new($) {
  32. my $proto = shift;
  33. my $class = ref($proto) || $proto;
  34. my $self = {};
  35. bless ($self, $class);
  36. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  37. my $progress = \${$self->{PROGRESS}};
  38. my $last_progress = \${$self->{LAST_PROGRESS}};
  39. my $last_time = \${$self->{LAST_TIME}};
  40. my $progress_count = \${$self->{PROGRESS_COUNT}};
  41. my $prefix = \${$self->{PREFIX}};
  42. my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
  43. $$progress_enabled = 1;
  44. $$progress = "";
  45. $$last_progress = "";
  46. $$last_time = 0;
  47. $$progress_count = 0;
  48. $$prefix = undef;
  49. $$prefix_callback = undef;
  50. return $self;
  51. }
  52. sub DESTROY {
  53. my $self = shift;
  54. $self->hide_progress;
  55. }
  56. sub enable_progress($) {
  57. my $self = shift;
  58. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  59. $$progress_enabled = 1;
  60. }
  61. sub disable_progress($) {
  62. my $self = shift;
  63. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  64. $$progress_enabled = 0;
  65. }
  66. sub show_progress($) {
  67. my $self = shift;
  68. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  69. my $progress = ${$self->{PROGRESS}};
  70. my $last_progress = \${$self->{LAST_PROGRESS}};
  71. my $progress_count = \${$self->{PROGRESS_COUNT}};
  72. $$progress_count++;
  73. if($$progress_enabled) {
  74. if($$progress_count > 0 && $$progress && $stderr_isatty) {
  75. # If progress has more than $columns characters the xterm will
  76. # scroll to the next line and our ^H characters will fail to
  77. # erase it.
  78. my $columns=$ENV{COLUMNS} || 80;
  79. $progress = substr $progress,0,($columns-1);
  80. print STDERR $progress;
  81. $$last_progress = $progress;
  82. }
  83. }
  84. }
  85. sub hide_progress($) {
  86. my $self = shift;
  87. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  88. my $progress = \${$self->{PROGRESS}};
  89. my $last_progress = \${$self->{LAST_PROGRESS}};
  90. my $progress_count = \${$self->{PROGRESS_COUNT}};
  91. $$progress_count--;
  92. if($$progress_enabled) {
  93. if($$last_progress && $stderr_isatty) {
  94. my $message=" " x length($$last_progress);
  95. print STDERR $message;
  96. undef $$last_progress;
  97. }
  98. }
  99. }
  100. sub update_progress($) {
  101. my $self = shift;
  102. my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
  103. my $progress = ${$self->{PROGRESS}};
  104. my $last_progress = \${$self->{LAST_PROGRESS}};
  105. if($$progress_enabled) {
  106. # If progress has more than $columns characters the xterm will
  107. # scroll to the next line and our ^H characters will fail to
  108. # erase it.
  109. my $columns=$ENV{COLUMNS} || 80;
  110. $progress = substr $progress,0,($columns-1);
  111. my $prefix = "";
  112. my $suffix = "";
  113. if($$last_progress) {
  114. $prefix = "" x length($$last_progress);
  115. my $diff = length($$last_progress)-length($progress);
  116. if($diff > 0) {
  117. $suffix = (" " x $diff) . ("" x $diff);
  118. }
  119. }
  120. print STDERR $prefix, $progress, $suffix;
  121. $$last_progress = $progress;
  122. }
  123. }
  124. sub progress($$) {
  125. my $self = shift;
  126. my $progress = \${$self->{PROGRESS}};
  127. my $last_time = \${$self->{LAST_TIME}};
  128. my $new_progress = shift;
  129. if(defined($new_progress)) {
  130. if(!defined($$progress) || $new_progress ne $$progress) {
  131. $$progress = $new_progress;
  132. $self->update_progress;
  133. $$last_time = 0;
  134. }
  135. } else {
  136. return $$progress;
  137. }
  138. }
  139. sub lazy_progress($$) {
  140. my $self = shift;
  141. my $progress = \${$self->{PROGRESS}};
  142. my $last_time = \${$self->{LAST_TIME}};
  143. $$progress = shift;
  144. my $time = time();
  145. if($time - $$last_time > 0) {
  146. $self->update_progress;
  147. $$last_time = $time;
  148. }
  149. }
  150. sub prefix($$) {
  151. my $self = shift;
  152. my $prefix = \${$self->{PREFIX}};
  153. my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
  154. my $new_prefix = shift;
  155. if(defined($new_prefix)) {
  156. if(!defined($$prefix) || $new_prefix ne $$prefix) {
  157. $$prefix = $new_prefix;
  158. $$prefix_callback = undef;
  159. }
  160. } else {
  161. return $$prefix;
  162. }
  163. }
  164. sub prefix_callback($) {
  165. my $self = shift;
  166. my $prefix = \${$self->{PREFIX}};
  167. my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
  168. $$prefix = undef;
  169. $$prefix_callback = shift;
  170. }
  171. sub write($$) {
  172. my $self = shift;
  173. my $message = shift;
  174. my $prefix = \${$self->{PREFIX}};
  175. my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
  176. $self->hide_progress if $stdout_isatty;
  177. if(defined($$prefix)) {
  178. print $$prefix . $message;
  179. } elsif(defined($$prefix_callback)) {
  180. print &{$$prefix_callback}() . $message;
  181. } else {
  182. print $message;
  183. }
  184. $self->show_progress if $stdout_isatty;
  185. }
  186. 1;