| 1 | # SPDX-FileCopyrightText: SUSE LLC | ||||||||||||
| 2 | # SPDX-License-Identifier: GPL-2.0-or-later | ||||||||||||
| 3 | |||||||||||||
| 4 | package Cavil::CLI::Progress; | ||||||||||||
| 5 | 7 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 7 | ▶ | use Mojo::Util qw(encode); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | # A single self-erasing status line on STDERR, so a long-running check always shows what it is doing without | ||||||||||||
| 10 | # touching STDOUT (which carries the report or a JSON pipe). Disabled unless STDERR is a terminal, so CI logs | ||||||||||||
| 11 | # stay clean. A phase leads with an animated spinner and a label; set the label to name what is happening and | ||||||||||||
| 12 | # call spin() to keep it alive while waiting. | ||||||||||||
| 13 | 7 | my @SPINNER = ("\x{2839}", "\x{2838}", "\x{283c}", "\x{2834}", "\x{2826}", "\x{2827}", "\x{2807}", "\x{280f}"); | |||||||||||
| 14 | |||||||||||||
| 15 | 7 | has enabled => 0; | |||||||||||
| 16 | 7 | has 'label'; | |||||||||||
| 17 | 7 | has _frame => 0; | |||||||||||
| 18 | |||||||||||||
| 19 | # Begin a phase with the given label. | ||||||||||||
| 20 | 29 | ▶ | sub start ($self, $label) { | ||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 21 | 29 | $self->label($label); | |||||||||||
| 22 | 29 | $self->_draw; | |||||||||||
| 23 | 29 | return $self; | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | # Advance the spinner one frame; called on a timer while a request is in flight so the line shows life. | ||||||||||||
| 27 | 21 | ▶ | sub spin ($self) { | ||||||||||
|
Subroutine
spin: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 28 | 21 | ▶ | return unless $self->enabled; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 5 | $self->_frame($self->_frame + 1); | |||||||||||
| 30 | 5 | $self->_draw; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | # Erase the status line, so it never bleeds into the report that follows. | ||||||||||||
| 34 | 11 | ▶ | sub finish ($self) { | ||||||||||
|
Subroutine
finish: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 35 | 11 | ▶ | return unless $self->enabled; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 1 | $self->_write(''); | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 34 | ▶ | sub _draw ($self) { | ||||||||||
|
Subroutine
_draw: called CC=3 SCAR=11.1
| |||||||||||||
| 40 | 34 | ▶ | return unless $self->enabled; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 9 | ▶ | $self->_write($SPINNER[$self->_frame % @SPINNER] . ' ' . ($self->label // '')); | ||||||||||
|
Condition50%1/2
$self->label // ""
MC/DC0%0/1
$self->label // ""
$self->label
Truth table50%1/2
A
A= $self->label
| |||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 10 | ▶ | sub _write ($self, $text) { | ||||||||||
|
Subroutine
_write: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 10 | print STDERR encode('UTF-8', "\r\e[K$text"); | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 7 | 1; | |||||||||||