File Coverage

blib/lib/Net/CLI/Interact/Transport/Role/StripControlChars.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Net::CLI::Interact::Transport::Role::StripControlChars;
2             $Net::CLI::Interact::Transport::Role::StripControlChars::VERSION = '2.400002';
3 1     1   570 use strict;
  1         3  
  1         52  
4 1     1   8 use warnings FATAL => 'all';
  1         3  
  1         43  
5              
6 1     1   5 use Moo::Role;
  1         3  
  1         8  
7              
8             my %ansi_codes = (
9             1 => q/\x1b\[\d+;\d+H/, # code_position_cursor
10             3 => q/\x1b\[\?25h/, #code_show_cursor
11             4 => q/\x1b\x45/, #code_next_line
12             5 => q/\x1b\[2K/, #code_erase_line
13             6 => q/\x1b\[K/, #code_erase_start_line
14             7 => q/\x1b\[\d+;\d+r/, #code_enable_scroll
15             68 => q/\e\[\??\d+(;\d+)*[A-Za-z]/, #VLZ addon from ytti/oxidized
16             );
17              
18             # https://github.com/ollyg/Net-CLI-Interact/issues/22
19             around 'buffer' => sub {
20             my $orig = shift;
21             my $buffer = ($orig->(@_) || '');
22              
23             # remove control characters
24             $buffer =~ s/[\000-\010\013\014\016-\032\034-\037]//g;
25              
26             # strip ANSI terminal codes
27             foreach my $code (sort keys %ansi_codes) {
28             my $to = '';
29             $to = "\n" if ($code == 4); # CODE_NEXT_LINE must substitute with '\n'
30             $buffer =~ s/$ansi_codes{$code}/$to/g;
31             }
32              
33             return $buffer;
34             };
35              
36             =pod
37              
38             =cut
39              
40             1;