termcolor/XTerm.pm

filter: Statementstmt 47.0% 48 / 102 Branchbran 2.5% 1 / 40 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 58.3% 14 / 24 Podpod 0.0% 0 / 10 totaltotal 35.7% 64 / 179 CC 22 SCAR 30.7
CC
22
Cov
35%
ask
72.0
test
30.0
osc_answer
20.0
CRAP 21.5
« 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 1 ▶use POSIX qw(tcgetpgrp);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use Getopt::EX::termcolor;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 0 ▶sub get_colors {
Subroutine get_colors: not called CC=2 SCAR=17.9
Pod undocumented
38 map {
39 0 my @rgb = get_color($_);
40 0 ▶ @rgb ? undef : [ @rgb ];
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
41 } @_;
42 }
43  
44 1 my %alias = qw(
45 foreground text_foreground
46 background text_background
47 );
48  
49 0 ▶sub get_color {
Subroutine get_color: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my $res = lc shift;
51 0 ▶ $res = $alias{$res} if $alias{$res};
Branch: 1 branch
BranchTrueFalseTotal
if $alias{$res} 0 0 0
52 0 return ask_color_rgb($res);
53 }
54  
55 1 our $debug = $ENV{DEBUG_GETOPTEX};
56 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
57  
58 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 CSI => "\e[", # Control Sequence Introducer
60 OSC => "\e]", # Operating System Command
61 1 };
62  
63 0 ▶sub osc_command {
Subroutine osc_command: not called CC=1 SCAR=6.9
Pod undocumented
64 0 OSC . join(';', @_) . "\a";
65 }
66  
67 1 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 my @oscPs_map = qw(
70 10 text_foreground
71 11 text_background
72 12 text_cursor
73 13 mouse_foreground
74 14 mouse_background
75 15 Tektronix_foreground
76 16 Tektronix_background
77 17 highlight_background
78 18 Tektronix_cursor
79 19 highlight_foreground
80 );
81 10 ▶my %oscPs = pairmap { $b => $a, lc $b => $a } @oscPs_map;
Subroutine __ANON__: called CC=1 SCAR=0.0
82 10 ▶my @oscPs_names = pairmap { $b } @oscPs_map;
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84 0 ▶sub uncntrl {
Subroutine uncntrl: not called CC=1 SCAR=6.9
Pod undocumented
85 0 $_[0] =~ s/([^\040-\176])/sprintf "\\%03o", ord $1/gear;
86 }
87  
88 # OSC Set Text Parameter
89 0 ▶sub osc_stp {
Subroutine osc_stp: not called CC=3 SCAR=24.8
Pod undocumented
90 0 my $Ps = shift;
91 0 ▶ if ($Ps !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Ps =~ /^\d+$/u) 0 0 0
92 0 ▶ $Ps = $oscPs{$Ps} or die "$Ps: invalid";
Branch: 1 branch
BranchTrueFalseTotal
$Ps = $oscPs{$Ps} or die "${Ps}: invalid" 0 0 0
93 }
94 0 osc_command $Ps, @_;
95 }
96  
97 1 my $osc_st_re = qr/[\a\x9c]|\e\\/;
98 1 my $osc_answer_re = qr/\e\]\d+;(?<answer>[\x08-\x13\x20-\x7d]*)$osc_st_re/;
99  
100 0 ▶sub osc_answer {
Subroutine osc_answer: not called CC=4 SCAR=30.0
Pod undocumented
101 0 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 0 0 0
102 0 ▶ defined $_[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or return 0 0 0
103 0 ▶ $_[0] =~ $osc_answer_re and $+{answer};
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$osc_answer_re/u and $+{"answer"} 0 0 0
104 }
105  
106 0 ▶sub ask {
Subroutine ask: not called CC=8 SCAR=42.8
Pod undocumented
107 0 my $request = shift;
108 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Getopt::EX::termcolor::XTerm::debug) 0 0 0
109 0 printf STDERR "[%s] Request: %s\n",
110 __PACKAGE__,
111 uncntrl $request;
112 }
113 0 ▶ open my $tty, "+<", "/dev/tty" or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $tty, "+<", "/dev/tty" or return 0 0 0
114 # A process outside the foreground process group must not touch
115 # the terminal: tcsetattr(3) and read(2) would stop it with
116 # SIGTTOU/SIGTTIN, as happens inside a process substitution.
117 # Ignore the signals as well, so that a race with the shell
118 # handing over the terminal ends in a failed read, not a stop.
119 0 ▶ if (tcgetpgrp(fileno $tty) != getpgrp()) {
Branch: 1 branch
BranchTrueFalseTotal
if (tcgetpgrp(fileno $tty) != getpgrp()) 0 0 0
120 0 ▶ warn sprintf("[%s] Not in foreground: skip\n", __PACKAGE__) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::EX::termcolor::XTerm::debug 0 0 0
121 0 return;
122 }
123 0 local $SIG{TTOU} = local $SIG{TTIN} = q(IGNORE);
124 0 ReadMode "cbreak", $tty;
125 0 printflush $tty $request;
126 0 my $timeout = $TIMEOUT;
127 0 my $answer = '';
128 0 ▶ while (defined (my $key = ReadKey $timeout, $tty)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = ReadKey($timeout, $tty))) 0 0 0
129 0 if (0 and $debug) {
130 printf STDERR "[%s] ReadKey: \"%s\"\n",
131 __PACKAGE__,
132 $key =~ /\P{Cc}/ ? $key : uncntrl $key;
133 }
134 0 $answer .= $key;
135 0 ▶ last if $answer =~ /$osc_st_re\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $answer =~ /$osc_st_re\z/u 0 0 0
136 }
137 0 ReadMode "restore", $tty;
138 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Getopt::EX::termcolor::XTerm::debug) 0 0 0
139 0 printf STDERR "[%s] Answer: %s\n",
140 __PACKAGE__,
141 uncntrl $answer;
142 }
143 0 return $answer;
144 }
145  
146 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 0 ▶sub set_color_rgb {
Subroutine set_color_rgb: not called CC=4 SCAR=30.0
Pod undocumented
149 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
150 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
151 0 my $max = (2 ** (length($1) * 4)) - 1;
152 0 ▶ my @opt = $max == 255 ? () : ( { max => $max } );
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
153 0 ( @opt, map { hex } @rgb );
154 }
155  
156 0 ▶sub ask_color_rgb {
Subroutine ask_color_rgb: not called CC=1 SCAR=6.9
Pod undocumented
157 0 set_color_rgb @_, '?';
158 }
159  
160 0 ▶do { test() } if __FILE__ eq $0;
Branch: 1 branch
BranchTrueFalseTotal
if (__FILE__ eq $0) 0 1 1
161  
162 0 ▶sub test {
Subroutine test: not called CC=5 SCAR=34.0
Pod undocumented
163 0 local $Data::Dumper::Indent = 1;
164 0 local $Data::Dumper::Terse = 1;
165 0 my $max = max map { length } @oscPs_names;
166 0 for my $name (@oscPs_names) {
167 0 my @rgb = ask_color_rgb $name;
168 0 ▶ printf "%*s: %s",
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
169 $max, $name,
170 @rgb ? Dumper(\@rgb)=~s/\n(?!\z)\s*/ /gr : "n/a\n";
171 }
172 0 for my $number (0 .. 255) {
173 0 my @rgb = ask_color_rgb(4, $number);
174 0 ▶ printf "%*d: %s",
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
175 $max, $number,
176 @rgb ? Dumper(\@rgb)=~s/\n(?!\z)\s*/ /gr : "n/a\n";
177 }
178 }
179  
180 1 1;
« termcolor.pm ↑ Summary termcolor/iTerm.pm »