blib/lib/Term/Detect/Software.pm

filter: Statementstmt 10.0% 18 / 180 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 4 / 8 Podpod 100% 2 / 2 totaltotal 7.8% 24 / 307 CC 50 SCAR 67.7
CC
50
Cov
6%
detect_terminal
1332.0
_set_engine
156.0
__ANON__
12.0
CRAP 875.0
↑ Summary
1 package Term::Detect::Software;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2024-02-10'; # DATE
11 1 our $DIST = 'Term-Detect-Software'; # DIST
12 1 our $VERSION = '0.227'; # VERSION
13  
14 1 our @EXPORT_OK = qw(detect_terminal detect_terminal_cached);
15  
16 1 my $dt_cache;
17 0 ▶sub detect_terminal_cached {
Subroutine detect_terminal_cached: not called CC=2 SCAR=17.9
Pod documented
18 0 ▶ if (!$dt_cache) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt_cache) 0 0 0
19 0 $dt_cache = detect_terminal(@_);
20 }
21 0 $dt_cache;
22 }
23  
24 0 ▶sub _set_engine {
Subroutine _set_engine: not called CC=12 SCAR=50.5
25 0 my ($info, $engine) = @_;
26 0 ▶ if ($engine eq 'konsole') {
Branch: 11 branches
BranchTrueFalseTotal
if ($engine eq 'konsole') { } 0 0 0
elsif ($engine eq 'xterm') { } 0 0 0
elsif ($engine eq 'cygwin') { } 0 0 0
elsif ($engine eq 'linux') { } 0 0 0
elsif ($engine eq 'gnome-terminal') { } 0 0 0
elsif ($engine eq 'windows') { } 0 0 0
elsif ($engine eq 'dumb') { } 0 0 0
elsif ($engine eq 'rxvt') { } 0 0 0
elsif ($engine eq 'st') { } 0 0 0
elsif ($engine eq 'putty') { } 0 0 0
elsif ($engine eq 'xvt') { } 0 0 0
27 0 $info->{emulator_engine} = 'konsole';
28 0 $info->{color_depth} = 2**24;
29 0 $info->{default_bgcolor} = '000000';
30 0 $info->{unicode} = 1;
31 0 $info->{box_chars} = 1;
32 } elsif ($engine eq 'xterm') {
33 0 $info->{emulator_engine} = 'xterm';
34 0 $info->{color_depth} = 256;
35 0 $info->{default_bgcolor} = 'ffffff';
36 0 $info->{unicode} = 0;
37 0 $info->{box_chars} = 1;
38 } elsif ($engine eq 'cygwin') {
39 0 $info->{emulator_engine} = 'cygwin';
40 0 $info->{color_depth} = 16;
41 0 $info->{default_bgcolor} = '000000';
42 0 $info->{unicode} = 0; # CONFIRM?
43 0 $info->{box_chars} = 1;
44 } elsif ($engine eq 'linux') {
45 # Linux virtual console
46 0 $info->{emulator_engine} = 'linux';
47 0 $info->{color_depth} = 16;
48 0 $info->{default_bgcolor} = '000000';
49 # actually it can show a few Unicode characters like single borders
50 0 $info->{unicode} = 0;
51 0 $info->{box_chars} = 0;
52 } elsif ($engine eq 'gnome-terminal') {
53 0 $info->{emulator_engine} = 'gnome-terminal';
54 0 $info->{unicode} = 1;
55 0 $info->{box_chars} = 1;
56 # color_depth and default_bgcolor vary
57 } elsif ($engine eq 'windows') {
58 0 $info->{emulator_software} = 'windows';
59 0 $info->{emulator_engine} = 'windows';
60 0 $info->{color_depth} = 16;
61 0 $info->{unicode} = 0;
62 0 $info->{default_bgcolor} = '000000';
63 0 $info->{box_chars} = 0;
64 } elsif ($engine eq 'dumb') {
65 # run under CGI or something like that
66 0 $info->{emulator_software} = 'dumb';
67 0 $info->{emulator_engine} = 'dumb';
68 0 $info->{color_depth} = 0;
69 # XXX how to determine unicode support?
70 0 $info->{default_bgcolor} = '000000';
71 0 $info->{box_chars} = 0;
72 } elsif ($engine eq 'rxvt') {
73 0 $info->{emulator_engine} = 'rxvt';
74 0 $info->{color_depth} = 16;
75 0 $info->{unicode} = 0;
76 0 $info->{default_bgcolor} = 'd6d2d0';
77 0 $info->{box_chars} = 1;
78 } elsif ($engine eq 'st') {
79 0 $info->{emulator_software} = 'st';
80 0 $info->{emulator_engine} = 'st';
81 0 $info->{color_depth} = 256;
82 0 $info->{unicode} = 1;
83 0 $info->{default_bgcolor} = '000000';
84 0 $info->{box_chars} = 1; # some characters are currently flawed though as of 0.6
85 } elsif ($engine eq 'putty') {
86 0 $info->{emulator_engine} = 'putty';
87 0 $info->{color_depth} = 256;
88 0 $info->{unicode} = 0;
89 0 $info->{default_bgcolor} = '000000';
90 } elsif ($engine eq 'xvt') {
91 0 $info->{emulator_engine} = 'xvt';
92 0 $info->{color_depth} = 0; # only support bold
93 0 $info->{unicode} = 0;
94 0 $info->{default_bgcolor} = 'd6d2d0';
95 } else {
96 0 die "Unknown engine '$engine'";
97 }
98 }
99  
100 0 ▶sub detect_terminal {
Subroutine detect_terminal: not called CC=36 SCAR=71.9
Pod documented
101 0 my @dbg;
102 0 my $info = {_debug_info=>\@dbg};
103  
104 DETECT:
105 {
106 0 ▶ if (defined $ENV{PERL_TERM_DETECT_SOFTWARE_ENGINE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PERL_TERM_DETECT_SOFTWARE_ENGINE"}) 0 0 0
107 0 push @dbg, "skip detect, set to '$ENV{PERL_TERM_DETECT_SOFTWARE_ENGINE}' via PERL_TERM_DETECT_SOFTWARE_ENGINE env";
108 0 _set_engine($info, $ENV{PERL_TERM_DETECT_SOFTWARE_ENGINE});
109 0 last DETECT;
110 }
111  
112 0 ▶ unless (defined $ENV{TERM}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"TERM"}) 0 0 0
113 0 push @dbg, "skip: TERM env undefined";
114 0 $info->{emulator_engine} = '';
115 0 $info->{emulator_software} = '';
116 0 last DETECT;
117 }
118  
119 0 ▶ if ($ENV{KONSOLE_DBUS_SERVICE} || $ENV{KONSOLE_DBUS_SESSION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"KONSOLE_DBUS_SERVICE"} or $ENV{"KONSOLE_DBUS_SESSION"}) 0 0 0
Condition0%0/3
$ENV{"KONSOLE_DBUS_SERVICE"} or $ENV{"KONSOLE_DBUS_SESSION"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"KONSOLE_DBUS_SERVICE"} or $ENV{"KONSOLE_DBUS_SESSION"}
$ENV{"KONSOLE_DBUS_SERVICE"} $ENV{"KONSOLE_DBUS_SESSION"}
Truth table0%0/3
A or B
A= $ENV{"KONSOLE_DBUS_SERVICE"}B= $ENV{"KONSOLE_DBUS_SESSION"}
ABresult
1X1
011
000
120 0 push @dbg, "detect: konsole via KONSOLE_DBUS_{SERVICE,SESSION} env";
121 0 _set_engine($info, 'konsole');
122 0 last DETECT;
123 }
124  
125 0 ▶ if ($ENV{XTERM_VERSION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"XTERM_VERSION"}) 0 0 0
126 0 push @dbg, "detect: xterm via XTERM_VERSION env";
127 0 _set_engine($info, 'xterm');
128 0 last DETECT;
129 }
130  
131 0 ▶ if ($ENV{TERM} eq 'xterm' && ($ENV{OSTYPE} // '') eq 'cygwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TERM"} eq "xterm" and ($ENV{"OSTYPE"} // "") eq "cygwin") 0 0 0
Condition0%0/5
$ENV{"TERM"} eq "xterm" and ($ENV{"OSTYPE"} // "") eq "cygwin"
!ll&&!rl&&r
000
$ENV{"OSTYPE"} // ""
l!l
00
MC/DC0%0/2
$ENV{"TERM"} eq "xterm" and ($ENV{"OSTYPE"} // "") eq "cygwin"
$ENV{"TERM"} eq "xterm" $ENV{"OSTYPE"}
Truth table0%0/3
A and B
A= $ENV{"TERM"} eq "xterm"B= $ENV{"OSTYPE"}
ABresult
0X0
100
111
132 0 push @dbg, "detect: xterm via TERM env (cygwin)";
133 0 _set_engine($info, 'cygwin');
134 0 last DETECT;
135 }
136  
137 0 ▶ if ($ENV{TERM} eq 'linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TERM"} eq "linux") 0 0 0
138 0 push @dbg, "detect: linux via TERM env";
139 0 _set_engine($info, 'linux');
140 0 last DETECT;
141 }
142  
143 0 my $gnome_terminal_terms = [qw/gnome-terminal guake xfce4-terminal
144 mlterm lxterminal/];
145  
146 0 ▶ my $set_gnome_terminal_term = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
147 0 _set_engine($info, 'gnome-terminal');
148 0 $info->{emulator_software} = $_[0];
149  
150 # xfce4-terminal only shows 16 color, despite being
151 # gnome-terminal-based?
152 0 ▶ $info->{color_depth} = $_[0] =~ /xfce4/ ? 16 : 256;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /xfce4/ ? : 0 0 0
153  
154 0 ▶ if (grep { $_ eq $_[0] } (qw/mlterm/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $_[0];} 'mlterm') { } 0 0 0
155 0 $info->{default_bgcolor} = 'ffffff';
156 } else {
157 0 $info->{default_bgcolor} = '000000';
158 }
159 0 };
160  
161 0 ▶ if (grep { $_ eq ($ENV{COLORTERM} // '') } @$gnome_terminal_terms) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ($ENV{"COLORTERM"} // "");} @$gnome_terminal_terms) 0 0 0
Condition0%0/2
$ENV{"COLORTERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"COLORTERM"} // ""
$ENV{"COLORTERM"}
Truth table0%0/2
A
A= $ENV{"COLORTERM"}
Aresult
11
00
162 0 push @dbg, "detect: gnome-terminal via COLORTERM";
163 0 $set_gnome_terminal_term->($ENV{COLORTERM});
164 0 last DETECT;
165 }
166  
167 0 ▶ if ($ENV{TERM} eq 'dumb' && $ENV{windir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TERM"} eq "dumb" and $ENV{"windir"}) 0 0 0
Condition0%0/3
$ENV{"TERM"} eq "dumb" and $ENV{"windir"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"TERM"} eq "dumb" and $ENV{"windir"}
$ENV{"TERM"} eq "dumb" $ENV{"windir"}
Truth table0%0/3
A and B
A= $ENV{"TERM"} eq "dumb"B= $ENV{"windir"}
ABresult
0X0
100
111
168 0 push @dbg, "detect: windows via TERM & windir env";
169 0 _set_engine($info, 'windows');
170 0 last DETECT;
171 }
172  
173 0 ▶ if ($ENV{TERM} eq 'dumb') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TERM"} eq "dumb") 0 0 0
174 0 push @dbg, "detect: dumb via TERM env";
175 0 _set_engine($info, 'dumb');
176 0 last DETECT;
177 }
178  
179 DETECT_VIA_PS:
180 {
181 # TODO: check under windows, because Proc::ProcessTable also works
182 # under windows
183 0 ▶ last if $^O =~ /Win/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /Win/ 0 0 0
184  
185 0 ▶ last unless $ENV{PERL_TERM_DETECT_SOFTWARE_CHECK_PS};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"PERL_TERM_DETECT_SOFTWARE_CHECK_PS"} 0 0 0
186  
187 0 require Proc::Find::Parents;
188 0 my $ppids = Proc::Find::Parents::get_parent_processes();
189 0 ▶ unless (defined $ppids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ppids) 0 0 0
190 0 push @dbg, "skip: get_parent_processes returns undef";
191 0 last;
192 }
193  
194 # [0] is shell
195 0 ▶ my $proc = @$ppids >= 2 ? $ppids->[1]{name} : '';
Branch: 1 branch
BranchTrueFalseTotal
@$ppids >= 2 ? : 0 0 0
196 #say "D:proc=$proc";
197 0 ▶ if (grep { $_ eq $proc } @$gnome_terminal_terms) {
Branch: 5 branches
BranchTrueFalseTotal
if (grep {$_ eq $proc;} @$gnome_terminal_terms) { } 0 0 0
elsif (grep {$_ eq $proc;} 'rxvt', 'mrxvt') { } 0 0 0
elsif ($proc eq 'st' and $ENV{'TERM'} eq 'xterm-256color') { } 0 0 0
elsif (grep {$_ eq $proc;} 'pterm') { } 0 0 0
elsif (grep {$_ eq $proc;} 'xvt') { } 0 0 0
198 0 push @dbg, "detect: gnome-terminal via procname ($proc)";
199 0 $set_gnome_terminal_term->($proc);
200 0 last DETECT;
201 0 } elsif (grep { $_ eq $proc } (qw/rxvt mrxvt/)) {
202 0 push @dbg, "detect: rxvt via procname ($proc)";
203 0 $info->{emulator_software} = $proc;
204 0 _set_engine($info, 'rxvt');
205 0 last DETECT;
206 ▶ } elsif ($proc eq 'st' && $ENV{TERM} eq 'xterm-256color') {
Condition0%0/3
$proc eq "st" and $ENV{"TERM"} eq "xterm-256color"
!ll&&!rl&&r
000
MC/DC0%0/2
$proc eq "st" and $ENV{"TERM"} eq "xterm-256color"
$proc eq "st" $ENV{"TERM"} eq "xterm-256color"
Truth table0%0/3
A and B
A= $proc eq "st"B= $ENV{"TERM"} eq "xterm-256color"
ABresult
0X0
100
111
207 0 push @dbg, "detect: st via procname";
208 0 _set_engine($info, 'st');
209 0 last DETECT;
210 0 } elsif (grep { $_ eq $proc } (qw/pterm/)) {
211 0 push @dbg, "detect: pterm via procname ($proc)";
212 0 $info->{emulator_software} = $proc;
213 0 _set_engine($info, 'putty');
214 0 last DETECT;
215 0 } elsif (grep { $_ eq $proc } (qw/xvt/)) {
216 0 push @dbg, "detect: xvt via procname ($proc)";
217 0 $info->{emulator_software} = $proc;
218 0 _set_engine($info, 'xvt');
219 0 last DETECT;
220 }
221 }
222  
223 DETECT_GENERIC:
224 {
225 0 ▶ unless (exists $info->{color_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $info->{"color_depth"}) 0 0 0
226 0 ▶ if ($ENV{TERM} =~ /256color/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'TERM'} =~ /256color/) { } 0 0 0
227 0 push @dbg, "detect color_depth: 256 via TERM env";
228 0 $info->{color_depth} = 256;
229 } else {
230 0 require File::Which;
231 0 ▶ if (File::Which::which("tput")) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Which::which("tput")) 0 0 0
232 0 my $res = `tput colors` + 0;
233 0 push @dbg, "detect color_depth: $res via tput";
234 0 ▶ $res = 16 if $res == 8; # 8 is basically 16 (8 low-intensity + 8 high-intensity)
Branch: 1 branch
BranchTrueFalseTotal
if $res == 8 0 0 0
235 0 $info->{color_depth} = $res;
236 }
237 }
238 }
239  
240 0 ▶ $info->{emulator_software} //= '(generic)';
Condition0%0/2
$info->{"emulator_software"} //= "(generic)"
l!l
00
MC/DC0%0/1
$info->{"emulator_software"} //= "(generic)"
$info->{"emulator_software"}
Truth table0%0/2
A
A= $info->{"emulator_software"}
Aresult
11
00
241 0 ▶ $info->{emulator_engine} //= '(generic)';
Condition0%0/2
$info->{"emulator_engine"} //= "(generic)"
l!l
00
MC/DC0%0/1
$info->{"emulator_engine"} //= "(generic)"
$info->{"emulator_engine"}
Truth table0%0/2
A
A= $info->{"emulator_engine"}
Aresult
11
00
242 0 ▶ $info->{unicode} //= 0;
Condition0%0/2
$info->{"unicode"} //= 0
l!l
00
MC/DC0%0/1
$info->{"unicode"} //= 0
$info->{"unicode"}
Truth table0%0/2
A
A= $info->{"unicode"}
Aresult
11
00
243 0 ▶ $info->{color_depth} //= 0;
Condition0%0/2
$info->{"color_depth"} //= 0
l!l
00
MC/DC0%0/1
$info->{"color_depth"} //= 0
$info->{"color_depth"}
Truth table0%0/2
A
A= $info->{"color_depth"}
Aresult
11
00
244 0 ▶ $info->{box_chars} //= 0;
Condition0%0/2
$info->{"box_chars"} //= 0
l!l
00
MC/DC0%0/1
$info->{"box_chars"} //= 0
$info->{"box_chars"}
Truth table0%0/2
A
A= $info->{"box_chars"}
Aresult
11
00
245 0 ▶ $info->{default_bgcolor} //= '000000';
Condition0%0/2
$info->{"default_bgcolor"} //= "000000"
l!l
00
MC/DC0%0/1
$info->{"default_bgcolor"} //= "000000"
$info->{"default_bgcolor"}
Truth table0%0/2
A
A= $info->{"default_bgcolor"}
Aresult
11
00
246 }
247  
248 } # DETECT
249  
250 # some additional detections
251  
252 # we're running under emacs, it doesn't support box chars
253 0 ▶ if ($ENV{INSIDE_EMACS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"INSIDE_EMACS"}) 0 0 0
254 0 $info->{inside_emacs} = 1;
255 0 $info->{box_chars} = 0;
256 }
257  
258 0 $info;
259 }
260  
261 1 1;
262 # ABSTRACT: Detect terminal (emulator) software and its capabilities
263  
264 __END__
↑ Summary