line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=pod |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Cli::Human - A collection of utility functions to make things more human friendly |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 METHODS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
10
|
|
|
|
|
|
|
package Wireguard::WGmeta::Cli::Human; |
11
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
12
|
1
|
|
|
1
|
|
4
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
13
|
1
|
|
|
1
|
|
5
|
use experimental 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
14
|
1
|
|
|
1
|
|
86
|
use Scalar::Util qw(looks_like_number); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
60
|
|
15
|
1
|
|
|
1
|
|
6
|
use base 'Exporter'; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
424
|
|
16
|
|
|
|
|
|
|
our @EXPORT = qw(disabled2human bits2human return_self timestamp2human); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
0
|
0
|
|
sub disabled2human($state) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
if ($state == 1) { |
21
|
0
|
|
|
|
|
|
return "no"; |
22
|
|
|
|
|
|
|
} |
23
|
0
|
|
|
|
|
|
return "yes"; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head3 bits2human($n_bits) |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Takes a number of bits and coverts it to a human readable amount of MiB |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
B |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=over 1 |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=item |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
C<$n_bits> A number of bits |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=back |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
B |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$n_bits * 1_000_000 . "MiB" |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
0
|
|
|
0
|
1
|
|
sub bits2human($n_bits) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
if (looks_like_number($n_bits)){ |
47
|
|
|
|
|
|
|
# this calculation is probably not correct, however, I found no reference on what is actually the unit of the wg show dump... |
48
|
0
|
|
|
|
|
|
return sprintf("%.2f %s", $n_bits / 1_000_000, "MiB"); |
49
|
|
|
|
|
|
|
} |
50
|
0
|
|
|
|
|
|
return "0.0 MiB"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head3 timestamp2human($timestamp) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Takes a unix timestamp and puts it in a human relatable form (delta from now) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
B |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=over 1 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
C<$timestamp> Int or string containing a unix timestamp |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=back |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
B |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
A string describing how long ago this timestamp was |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
0
|
|
|
0
|
1
|
|
sub timestamp2human($timestamp) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
74
|
0
|
0
|
0
|
|
|
|
if (not looks_like_number($timestamp) or $timestamp == 0) { |
75
|
0
|
|
|
|
|
|
return "never" |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
|
my $int_timestamp = int($timestamp); |
78
|
0
|
|
|
|
|
|
my $delta = time - $int_timestamp; |
79
|
0
|
0
|
|
|
|
|
if ($delta > 2592000) { |
80
|
0
|
|
|
|
|
|
return ">month ago"; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
0
|
|
|
|
|
if ($delta > 604800) { |
83
|
0
|
|
|
|
|
|
return ">week ago"; |
84
|
|
|
|
|
|
|
} |
85
|
0
|
0
|
|
|
|
|
if ($delta > 86400) { |
86
|
0
|
|
|
|
|
|
return ">day ago"; |
87
|
|
|
|
|
|
|
} |
88
|
0
|
0
|
|
|
|
|
if ($delta < 86400) { |
89
|
0
|
|
|
|
|
|
return sprintf("%.2f mins ago", $delta / 60); |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
|
return $delta; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head3 return_self($x) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The famous C function |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
B |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over 1 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
C<$x> Some value or object |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=back |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
B |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
C<$x> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
0
|
|
|
0
|
1
|
|
sub return_self($x) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
return $x; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |