]> git.sur5r.net Git - i3/i3/blob - testcases/lib/StatusLine.pm
823c6713df080808134aa17685db046faf8a10a2
[i3/i3] / testcases / lib / StatusLine.pm
1 package StatusLine;
2 use strict; use warnings;
3
4 # enable autoflush on STDOUT.
5 # this is essential, because we print our statuslines without a newline
6 $| = 1;
7
8 use Exporter 'import';
9 our @EXPORT = qw/status_init status status_completed/;
10
11 my $ansi_clear_line = "\033[2K";
12 my $ansi_save_cursor = "\0337";
13 my $ansi_restore_cursor = "\0338";
14 my %ansi_line_upwards;
15
16 my $tests_total;
17
18 # setup %ansi_line_upwards to map all working displays to the
19 # specific movement commands and initialize all status lines
20 sub status_init {
21     my %args = @_;
22     my $displays = $args{displays};
23     $tests_total = $args{tests};
24
25     for my $n (1 .. @$displays) {
26         # since we are moving upwards, get $display in reverse order
27         my $display = $displays->[-$n];
28
29         $ansi_line_upwards{$display} = "\033[$n\101";
30
31         # print an empty line for this status line
32         print "\n";
33     }
34
35     status_completed(0);
36 }
37
38 # generates the status text, prints it in the appropiate line
39 # and returns it, so it can be used in conjuction with C<Log()>
40 sub status {
41     my ($display, $msg) = @_;
42     my $status = "[$display] $msg";
43
44     print
45         $ansi_save_cursor,
46         $ansi_line_upwards{$display},
47         $ansi_clear_line,
48         $status,
49         $ansi_restore_cursor;
50
51     return $status;
52 }
53
54 sub status_completed {
55     my $num = shift;
56     print
57         $ansi_save_cursor,
58         $ansi_clear_line,
59         "completed $num of $tests_total tests",
60         $ansi_restore_cursor;
61 }
62
63
64 __PACKAGE__ __END__