Several additions and bug fixes.
[wine] / tools / winapi / output.pm
1 package output;
2
3 use strict;
4
5 my $_output;
6
7 sub new {
8     my $self = shift;
9     $_output = _output->new(@_);
10     return $_output;
11 }
12
13 sub AUTOLOAD {
14     my $self = shift;
15
16     my $name = $output::AUTOLOAD;
17     $name =~ s/^.*::(.[^:]*)$/$1/;
18
19     return $_output->$name(@_);
20 }
21
22 package _output;
23
24 use strict;
25
26 my $stdout_isatty = -t STDOUT;
27 my $stderr_isatty = -t STDERR;
28
29 sub new {
30     my $proto = shift;
31     my $class = ref($proto) || $proto;
32     my $self  = {};
33     bless ($self, $class);
34
35     my $progress = \${$self->{PROGRESS}};
36     my $last_progress = \${$self->{LAST_PROGRESS}};
37     my $progress_count = \${$self->{PROGRESS_COUNT}};
38     my $prefix = \${$self->{PREFIX}};
39
40     $$progress = "";
41     $$last_progress = "";
42     $$progress_count = 0;
43     $$prefix = "";
44
45     return $self;
46 }
47
48
49 sub show_progress {
50     my $self = shift;
51     my $progress = \${$self->{PROGRESS}};
52     my $last_progress = \${$self->{LAST_PROGRESS}};
53     my $progress_count = \${$self->{PROGRESS_COUNT}};
54
55     $$progress_count++;
56
57     if($$progress_count > 0 && $$progress && $stderr_isatty) {
58         print STDERR $$progress;
59         $$last_progress = $$progress;
60     }
61 }
62
63 sub hide_progress  {
64     my $self = shift;
65     my $progress = \${$self->{PROGRESS}};
66     my $last_progress = \${$self->{LAST_PROGRESS}};
67     my $progress_count = \${$self->{PROGRESS_COUNT}};
68
69     $$progress_count--;
70
71     if($$last_progress && $stderr_isatty) {
72         my $message;
73         for (1..length($$last_progress)) {
74             $message .= "\b \b";
75         }
76         print STDERR $message;
77         undef $$last_progress;
78     }
79 }
80
81 sub update_progress {
82     my $self = shift;
83     my $progress = \${$self->{PROGRESS}};
84     my $last_progress = \${$self->{LAST_PROGRESS}};
85     
86     my $prefix = "";
87     my $suffix = "";
88     if($$last_progress) {
89         for (1..length($$last_progress)) {
90             $prefix .= "\b";
91         }
92         
93         my $diff = length($$last_progress)-length($$progress);
94         if($diff > 0) {
95             for (1..$diff) {
96                 $suffix .= " ";
97             }
98             for (1..$diff) {
99                 $suffix .= "\b";
100             }
101         }
102     }
103     print STDERR $prefix . $$progress . $suffix;
104     $$last_progress = $$progress;
105 }
106
107 sub progress {
108     my $self = shift;
109     my $progress = \${$self->{PROGRESS}};
110
111     $$progress = shift;
112
113     $self->update_progress;
114 }
115
116 sub prefix {
117     my $self = shift;
118     my $prefix = \${$self->{PREFIX}};
119
120     $$prefix = shift;
121 }
122
123 sub write {
124     my $self = shift;
125
126     my $message = shift;
127
128     my $prefix = \${$self->{PREFIX}};
129
130     $self->hide_progress if $stdout_isatty;
131     print $$prefix . $message;
132     $self->show_progress if $stdout_isatty;
133 }
134
135 1;