File: Progress.pm

package info (click to toggle)
sockperf 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,912 kB
  • sloc: cpp: 8,092; perl: 7,225; sh: 3,254; makefile: 114; awk: 93
file content (99 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
##
# @file Progress.pm
#
# @brief TE module for Progress object.
#
#

## @class
# Container for Progress object.
package TE::Progress; 

use strict;
use warnings;

# External modules

# Own modules


our $object = undef;

sub init 
{
    # Set default values
    $object->{_fh} = \*STDERR,
    $object->{_counter} = 0;
    $object->{_icons} = ['-', '\\', '|', '/'];
    $object->{_line} = undef;
    $object->{_fields} = ();
    $object->{_width} = 80;
}

sub update 
{ 	
	# Check if it is initialized
	return if (not defined($object));
	
    my $status_line = undef;
    my ($node_field, $status_field) = @_;
    my $icon_field = $object->{_icons}->[$object->{_counter} % scalar(@{$object->{_icons}})];
        
    $status_field = (defined($object->{_fields}->[2]) ? $object->{_fields}->[2] : '') if (not defined($status_field));
    $status_field = $1 if ($status_field =~ /([^\n\r]+)/); 
    $status_field = (defined($object->{_fields}->[2]) ? $object->{_fields}->[2] : '') if ($status_field =~ /^([\n\r]+)/);
    $status_field = (defined($object->{_fields}->[2]) ? $object->{_fields}->[2] : '') if ($status_field eq '');     

    $node_field = (defined($object->{_fields}->[1]) ? $object->{_fields}->[1] : '') if (not defined($node_field));
        
    $status_line = sprintf("%s(%-1.1s)%s %s[%-30.30s]%s %s%-40.40s%s\r",
                           ($^O =~ /Win/ ? "" : "\e[33m"),
                           $icon_field, 
                           ($^O =~ /Win/ ? "" : "\e[0m"),
                           ($^O =~ /Win/ ? "" : "\e[32m"),
                           $node_field,
                           ($^O =~ /Win/ ? "" : "\e[0m"),
                           ($^O =~ /Win/ ? "" : "\e[0m"),
                           $status_field,
                           ($^O =~ /Win/ ? "" : "\e[0m"));

    $object->{_counter} ++;
    $object->{_line} = $status_line;
    $object->{_fields}->[0] = $icon_field;
    $object->{_fields}->[1] = $node_field;
    $object->{_fields}->[2] = $status_field;
    
    printf ${$object->{_fh}} ("%s\r", $status_line) if defined($status_line);
}


sub clean 
{   
    # Check if it is initialized
    return if (not defined($object));
    
    # Clean status line
    my $status_line = (" " x $object->{_width}) . ("\b" x $object->{_width}) if defined($object->{_line});
        
    $object->{_line} = $status_line;
    
    printf ${$object->{_fh}} ("%s\r", $status_line) if defined($status_line);
}


sub end 
{
    # Check if it is initialized
    return if (not defined($object));
    
    # Clean status line
    my $status_line = (" " x $object->{_width}) . ("\b" x $object->{_width}) if defined($object->{_line});
    
    printf ${$object->{_fh}} ("%s\r", $status_line) if defined($status_line);
    
    $object = ();
    $object = undef;
}


1;