blib/lib/Getopt/EX/termcolor/XTerm.pm

filter: Statementstmt 48.3% 45 / 93 Branchbran 2.9% 1 / 34 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 59.0% 13 / 22 Podpod 0.0% 0 / 9 totaltotal 37.2% 60 / 161 SCAR 46.2
CC
18
Cov
36%
ask
42.0
osc_answer
20.0
color_rgb
20.0
CRAP 101.2
« blib/lib/Getopt/EX/termcolor.pm ↑ Summary blib/lib/Getopt/EX/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 sub get_colors {
37 map {
38 0 ▶ my @rgb = get_color($_);
Subroutine get_colors: not called CC=2 SCAR=17.9
Pod undocumented
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 sub get_color {
49 0 ▶ my $res = lc shift;
Subroutine get_color: not called CC=2 SCAR=17.9
Pod undocumented
50 0 ▶ $res = $alias{$res} if $alias{$res};
Branch: 1 branch
BranchTrueFalseTotal
if $alias{$res} 0 0 0
51 0 return 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 use constant {
58 1 CSI => "\e[", # Control Sequence Introducer
59 OSC => "\e]", # Operating System Command
60 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 sub osc_command {
63 0 ▶ my($Ps, $Pt) = @_;
Subroutine osc_command: not called CC=1 SCAR=6.9
Pod undocumented
64 0 OSC . "$Ps;$Pt" . "\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 sub uncntrl {
85 0 ▶ $_[0] =~ s/([^\040-\176])/sprintf "\\%03o", ord $1/gear;
Subroutine uncntrl: not called CC=1 SCAR=6.9
Pod undocumented
86 }
87  
88 # OSC Set Text Parameter
89 sub osc_stp {
90 0 ▶ my $name = shift;
Subroutine osc_stp: not called CC=3 SCAR=24.8
Pod undocumented
91 0 ▶ my $color = @_ ? shift : '?';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
92 0 ▶ my $Ps = $oscPs{$name} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $Ps = $oscPs{$name} or croak() 0 0 0
93 0 osc_command $Ps, $color;
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 sub osc_answer {
100 0 ▶ @_ or return;
Subroutine osc_answer: not called CC=4 SCAR=30.0
Pod undocumented
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 sub ask {
106 0 ▶ my $request = shift;
Subroutine ask: not called CC=6 SCAR=37.4
Pod undocumented
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 sub color_rgb {
138 0 ▶ my $name = shift;
Subroutine color_rgb: not called CC=4 SCAR=30.0
Pod undocumented
139 0 ▶ my $rgb = osc_answer ask osc_stp $name or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rgb = osc_answer(ask(osc_stp($name))) or return 0 0 0
140 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
141 0 my $max = (2 ** (length($1) * 4)) - 1;
142 0 ▶ my @opt = $max == 255 ? () : ( { max => $max } );
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
143 0 ( @opt, map { hex } @rgb );
144 }
145  
146 1 ▶do { test() } if __FILE__ eq $0;
Branch: 1 branch
BranchTrueFalseTotal
if (__FILE__ eq $0) 0 1 1
147  
148 sub test {
149 0 ▶ local $Data::Dumper::Indent = 1;
Subroutine test: not called CC=3 SCAR=24.8
Pod undocumented
150 0 local $Data::Dumper::Terse = 1;
151 0 my $max = max map { length } @oscPs_names;
152 0 for my $name (@oscPs_names) {
153 0 my @rgb = color_rgb($name);
154 0 ▶ printf "%*s: %s",
Branch: 1 branch
BranchTrueFalseTotal
@rgb ? : 0 0 0
155 $max, $name,
156 @rgb ? Dumper(\@rgb)=~s/\n(?!\z)\s*/ /gr : "n/a\n";
157 }
158 }
159  
160 1 1;
« blib/lib/Getopt/EX/termcolor.pm ↑ Summary blib/lib/Getopt/EX/termcolor/iTerm.pm »