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