]> git.sur5r.net Git - i3/i3/blob - testcases/lib/StatusLine.pm
379fd3d6c133bb6f1a0524fd61639795a98199f0
[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 sub noninteractive {
19     # CONTINUOUS_INTEGRATION gets set when running under Travis, see
20     # http://docs.travis-ci.com/user/ci-environment/ and
21     # https://github.com/travis-ci/travis-ci/issues/1337
22     return (! -t STDOUT) || $ENV{CONTINUOUS_INTEGRATION} eq 'true';
23 }
24
25 # setup %ansi_line_upwards to map all working displays to the
26 # specific movement commands and initialize all status lines
27 sub status_init {
28     my %args = @_;
29     my $displays = $args{displays};
30     $tests_total = $args{tests};
31
32     return if noninteractive();
33
34     for my $n (1 .. @$displays) {
35         # since we are moving upwards, get $display in reverse order
36         my $display = $displays->[-$n];
37
38         $ansi_line_upwards{$display} = "\033[$n\101";
39
40         # print an empty line for this status line
41         print "\n";
42     }
43
44     status_completed(0);
45 }
46
47 # generates the status text, prints it in the appropiate line
48 # and returns it, so it can be used in conjuction with C<Log()>
49 sub status {
50     my ($display, $msg) = @_;
51     my $status = "[$display] $msg";
52
53     return $status if noninteractive();
54
55     print
56         $ansi_save_cursor,
57         $ansi_line_upwards{$display},
58         $ansi_clear_line,
59         $status,
60         $ansi_restore_cursor;
61
62     return $status;
63 }
64
65 sub status_completed {
66     my $num = shift;
67
68     return if noninteractive();
69
70     print
71         $ansi_save_cursor,
72         $ansi_clear_line,
73         "completed $num of $tests_total tests",
74         $ansi_restore_cursor;
75 }
76
77
78 __PACKAGE__ __END__