termcolor/XTerm.pm

filter: Statementstmt 47.3% 45 / 95 Branchbran 2.7% 1 / 36 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 56.5% 13 / 23 Podpod 0.0% 0 / 10 totaltotal 35.9% 60 / 167 CC 20 SCAR 27.3
CC
20
Cov
35%
ask
42.0
test
30.0
osc_answer
20.0
CRAP 15.3
« termcolor.pm ↑ Summary termcolor/iTerm.pm »
1 =head1 NAME
2  
3 Getopt::EX::termcolor::XTerm
4  
5 =head1 SYNOPSIS
6  
7 use Getopt::EX::termcolor::XTerm;
8  
9 =head1 DESCRIPTION
10  
11 This is a L<Getopt::EX::termcolor> module for XTerm.
12  
13 =head1 SEE ALSO
14  
15 L<Getopt::EX::termcolor>
16  
17 L<https://www.xfree86.org/current/ctlseqs.html>
18  
19 =cut
20  
21 package Getopt::EX::termcolor::XTerm;
22  
23 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 our @EXPORT_OK = qw(test);
28  
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use Getopt::EX::termcolor;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 0 ▶sub get_colors {
Subroutine get_colors: not called CC=2 SCAR=17.9
Pod undocumented
37 map {
38 0 my @rgb = get_color($_);
39 0 ▶ @rgb ? undef : [ @rgb ];
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
40 } @_;
41 }
42  
43 1 my %alias = qw(
44 foreground text_foreground
45 background text_background
46 );
47  
48 0 ▶sub get_color {
Subroutine get_color: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my $res = lc shift;
50 0 ▶ $res = $alias{$res} if $alias{$res};
Branch: 1 branch
BranchTrueFalseTotal
if $alias{$res} 0 0 0
51 0 return ask_color_rgb($res);
52 }
53  
54 1 our $debug = $ENV{DEBUG_GETOPTEX};
55 1 ▶our $TIMEOUT = $ENV{TERMCOLOR_XTERM_TIMEOUT} || 0.1;
Condition50%1/2
$ENV{'TERMCOLOR_XTERM_TIMEOUT'} || 0.1
l!l
01
MC/DC0%0/1
$ENV{'TERMCOLOR_XTERM_TIMEOUT'} || 0.1
$ENV{'TERMCOLOR_XTERM_TIMEOUT'}
Truth table50%1/2
A
A= $ENV{'TERMCOLOR_XTERM_TIMEOUT'}
Aresult
11
00
56  
57 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 CSI => "\e[", # Control Sequence Introducer
59 OSC => "\e]", # Operating System Command
60 1 };
61  
62 0 ▶sub osc_command {
Subroutine osc_command: not called CC=1 SCAR=6.9
Pod undocumented
63 0 OSC . join(';', @_) . "\a";
64 }
65  
66 1 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 my @oscPs_map = qw(
69 10 text_foreground
70 11 text_background
71 12 text_cursor
72 13 mouse_foreground
73 14 mouse_background
74 15 Tektronix_foreground
75 16 Tektronix_background
76 17 highlight_background
77 18 Tektronix_cursor
78 19 highlight_foreground
79 );
80 10 ▶my %oscPs = pairmap { $b => $a, lc $b => $a } @oscPs_map;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 10 ▶my @oscPs_names = pairmap { $b } @oscPs_map;
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 0 ▶sub uncntrl {
Subroutine uncntrl: not called CC=1 SCAR=6.9
Pod undocumented
84 0 $_[0] =~ s/([^\040-\176])/sprintf "\\%03o", ord $1/gear;
85 }
86  
87 # OSC Set Text Parameter
88 0 ▶sub osc_stp {
Subroutine osc_stp: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my $Ps = shift;
90 0 ▶ if ($Ps !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Ps =~ /^\d+$/u) 0 0 0
91 0 ▶ $Ps = $oscPs{$Ps} or die "$Ps: invalid";
Branch: 1 branch
BranchTrueFalseTotal
$Ps = $oscPs{$Ps} or die "${Ps}: invalid" 0 0 0
92 }
93 0 osc_command $Ps, @_;
94 }
95  
96 1 my $osc_st_re = qr/[\a\x9c]|\e\\/;
97 1 my $osc_answer_re = qr/\e\]\d+;(?<answer>[\x08-\x13\x20-\x7d]*)$osc_st_re/;
98  
99 0 ▶sub osc_answer {
Subroutine osc_answer: not called CC=4 SCAR=30.0
Pod undocumented
100 0 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 0 0 0
101 0 ▶ defined $_[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or return 0 0 0
102 0 ▶ $_[0] =~ $osc_answer_re and $+{answer};
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$osc_answer_re/u and $+{"answer"} 0 0 0
103 }
104  
105 0 ▶sub ask {
Subroutine ask: not called CC=6 SCAR=37.4
Pod undocumented
106 0 my $request = shift;
107 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Getopt::EX::termcolor::XTerm::debug) 0 0 0
108 0 printf STDERR "[%s] Request: %s\n",
109 __PACKAGE__,
110 uncntrl $request;
111 }
112 0 ▶ open my $tty, "+<", "/dev/tty" or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $tty, "+<", "/dev/tty" or return 0 0 0
113 0 ReadMode "cbreak", $tty;
114 0 printflush $tty $request;
115 0 my $timeout = $TIMEOUT;
116 0 my $answer = '';
117 0 ▶ while (defined (my $key = ReadKey $timeout, $tty)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = ReadKey($timeout, $tty))) 0 0 0
118 0 if (0 and $debug) {
119 printf STDERR "[%s] ReadKey: \"%s\"\n",
120 __PACKAGE__,
121 $key =~ /\P{Cc}/ ? $key : uncntrl $key;
122 }
123 0 $answer .= $key;
124 0 ▶ last if $answer =~ /$osc_st_re\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $answer =~ /$osc_st_re\z/u 0 0 0
125 }
126 0 ReadMode "restore", $tty;
127 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Getopt::EX::termcolor::XTerm::debug) 0 0 0
128 0 printf STDERR "[%s] Answer: %s\n",
129 __PACKAGE__,
130 uncntrl $answer;
131 }
132 0 return $answer;
133 }
134  
135 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 0 ▶sub set_color_rgb {
Subroutine set_color_rgb: not called CC=4 SCAR=30.0
Pod undocumented
138 0 ▶ my $rgb = osc_answer ask osc_stp @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rgb = osc_answer(ask(osc_stp(@_))) or return 0 0 0
139 0 ▶ my @rgb = $rgb =~ m{rgb:([\da-f]+)/([\da-f]+)/([\da-f]+)}i or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@rgb) = $rgb =~ m[rgb:([\da-f]+)/([\da-f]+)/([\da-f]+)]iu or return 0 0 0
140 0 my $max = (2 ** (length($1) * 4)) - 1;
141 0 ▶ my @opt = $max == 255 ? () : ( { max => $max } );
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
142 0 ( @opt, map { hex } @rgb );
143 }
144  
145 0 ▶sub ask_color_rgb {
Subroutine ask_color_rgb: not called CC=1 SCAR=6.9
Pod undocumented
146 0 set_color_rgb @_, '?';
147 }
148  
149 1 ▶do { test() } if __FILE__ eq $0;
Branch: 1 branch
BranchTrueFalseTotal
if (__FILE__ eq $0) 0 1 1
150  
151 0 ▶sub test {
Subroutine test: not called CC=5 SCAR=34.0
Pod undocumented
152 0 local $Data::Dumper::Indent = 1;
153 0 local $Data::Dumper::Terse = 1;
154 0 my $max = max map { length } @oscPs_names;
155 0 for my $name (@oscPs_names) {
156 0 my @rgb = ask_color_rgb $name;
157 0 ▶ printf "%*s: %s",
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
158 $max, $name,
159 @rgb ? Dumper(\@rgb)=~s/\n(?!\z)\s*/ /gr : "n/a\n";
160 }
161 0 for my $number (0 .. 255) {
162 0 my @rgb = ask_color_rgb(4, $number);
163 0 ▶ printf "%*d: %s",
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
164 $max, $number,
165 @rgb ? Dumper(\@rgb)=~s/\n(?!\z)\s*/ /gr : "n/a\n";
166 }
167 }
168  
169 1 1;
« termcolor.pm ↑ Summary termcolor/iTerm.pm »