File Coverage

blib/lib/Term/Visual/StatusBar.pm
Criterion Covered Total %
statement 12 73 16.4
branch 0 4 0.0
condition n/a
subroutine 4 8 50.0
pod 0 4 0.0
total 16 89 17.9


line stmt bran cond sub pod time code
1             # $Id: StatusBar.pm,v 1.1 2003/04/06 12:39:02 lunartear Exp $
2             ### Manage the status bar.
3              
4             package Term::Visual::StatusBar;
5              
6 1     1   4 use strict;
  1         1  
  1         25  
7 1     1   4 use warnings;
  1         1  
  1         20  
8              
9 1     1   4 use vars qw($VERSION);
  1         1  
  1         41  
10             $VERSION = (qw($Revision: 1.1 $ ))[1];
11              
12 1     1   4 use Carp qw(croak);
  1         2  
  1         871  
13              
14              
15             sub DEBUG () { 0 }
16             if (DEBUG) { open ERRS, ">status_error_file"; }
17              
18             sub FIELD_TO_LINE () { 0 } # A hash mapping field names to their lines.
19             sub STATUS_LINES () { 1 } # A list of status line definitions.
20              
21             sub SL_FORMAT () { 0 } # The sprintf format for a status line.
22             sub SL_VALUES () { 1 } # The values for fields in a line.
23             sub SL_OFFSETS () { 2 } # A hash mapping field names to array offsets.
24              
25             sub new {
26 0     0 0   my $package = shift;
27 0           my $self =
28             bless [ { }, # FIELD_TO_LINE
29             [ ], # STATUS_LINES
30             ], $package;
31 0           return $self;
32             }
33              
34             sub set_format {
35 0     0 0   my $self = shift;
36 0           my %hash = @_;
37 0           if (DEBUG) { print ERRS %hash, " <-in statusbar\n"; }
38 0           for my $line (keys %hash) {
39 0           if (DEBUG) { print ERRS "in for loop of statusbar set_format\n"; }
40 0           if (DEBUG) { print ERRS "$hash{$line}{format} <-format value\n"; }
41 0           if (DEBUG) { print ERRS "@{$hash{$line}{fields}} <-fields values\n"; }
42 0           my $format = $hash{$line}{format};
43 0           my @fields = @{$hash{$line}{fields}};
  0            
44 0           if (DEBUG) {
45             print ERRS $line,"<-line\n", $format,"<-format\n", @fields,"<-fields\n";
46             }
47              
48             # Build a list of values for each field. Also build a hash to map
49             # the field name to its offset in the list of values.
50              
51 0           my (@values, %offsets);
52 0           for my $index (0..$#fields) {
53 0           push @values, '';
54 0           $offsets{$fields[$index]} = $index;
55             }
56 0           if (DEBUG) { print ERRS "built list of values and hash to map of field\n"; }
57              
58             # If the line is being redefined, then remove the old line's
59             # definitions.
60              
61 0 0         if (defined $self->[STATUS_LINES]->[$line]) {
62 0           if (DEBUG) { print ERRS "the line is being redefined\n"; }
63 0           for my $field (keys %{$self->[STATUS_LINES]->[$line]->[SL_OFFSETS]}) {
  0            
64 0           delete $self->[FIELD_TO_LINE]->{$field};
65             }
66             }
67              
68             # Store the fields to lines.
69              
70 0           for my $field (@fields) {
71 0           $self->[FIELD_TO_LINE]->{$field} = $line;
72             }
73              
74 0           if (DEBUG) { print ERRS "stored the fields to lines\n"; }
75              
76             # Store the status line definition in the object.
77              
78 0           $self->[STATUS_LINES]->[$line] =
79             [ $format, # SL_FORMAT
80             \@values, # SL_VALUES
81             \%offsets, # SL_OFFSETS
82             ];
83 0           if (DEBUG) { print ERRS "stored the status line in the object\n"; }
84             }
85              
86 0           if (DEBUG) { print ERRS "left main for loop in set_format\n"; }
87             #TODO return the status_lines to eliminate having to call get();
88 0           return;
89             }
90              
91             # Set a status line value.
92              
93             sub set {
94 0     0 0   if (DEBUG) { print ERRS "set called\n"; }
95 0           my $self = shift;
96             # my ($status_line, $line);
97 0           while (@_) {
98 0           if (DEBUG) { print ERRS "entered while loop of set()\n"; }
99 0           my ($status_line, $line);
100 0           my ($field, $value) = (shift, shift);
101              
102             # Find out which line the field is in.
103              
104 0           $line = $self->[FIELD_TO_LINE]->{$field};
105 0 0         croak "unknown field \"$field\"" unless defined $line;
106              
107             # Store the value in SL_VALUES based on its offset from SL_OFFSETS.
108              
109 0           $status_line = $self->[STATUS_LINES]->[$line];
110 0           my $offset = $status_line->[SL_OFFSETS]->{$field};
111 0           $status_line->[SL_VALUES]->[$offset] = $value;
112             }
113              
114             # Create a formatted line based on SL_FORMAT, and give it to the
115             # terminal to display.
116 0           my @status_lines;
117 0           for my $line (0..$#{$self->[STATUS_LINES]}) {
  0            
118 0           my $status_line = $self->[STATUS_LINES]->[$line];
119 0           my $formatted_status_line = sprintf( $status_line->[SL_FORMAT],
120 0           @{$status_line->[SL_VALUES]} );
121 0           push(@status_lines, $line, $formatted_status_line);
122             }
123 0           if (DEBUG) { print ERRS join(' ', @status_lines), " <-status_lines in set\n";}
124 0           return \@status_lines;
125             }
126              
127             sub get {
128 0     0 0   if (DEBUG) { print ERRS "StatusBar->get called\n"; }
129 0           my $self = shift;
130 0           my @status_lines;
131 0           for my $line (0..$#{$self->[STATUS_LINES]}) {
  0            
132 0           my $status_line = $self->[STATUS_LINES]->[$line];
133 0           my $formatted_status_line = sprintf( $status_line->[SL_FORMAT],
134 0           @{$status_line->[SL_VALUES]} );
135 0           push(@status_lines, $line, $formatted_status_line);
136             }
137 0           return \@status_lines;
138             }
139              
140              
141             ### End.
142              
143             1;