File: StripControlChars.pm

package info (click to toggle)
libnet-cli-interact-perl 2.300003-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 448 kB
  • sloc: perl: 1,951; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 1,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Net::CLI::Interact::Transport::Role::StripControlChars;
{ $Net::CLI::Interact::Transport::Role::StripControlChars::VERSION = '2.300003' }

use strict;
use warnings FATAL => 'all';

use Moo::Role;

my %ansi_codes = (
    1  => q/\x1b\[\d+;\d+H/, # code_position_cursor
    3  => q/\x1b\[\?25h/, #code_show_cursor
    4  => q/\x1b\x45/, #code_next_line
    5  => q/\x1b\[2K/, #code_erase_line
    6  => q/\x1b\[K/, #code_erase_start_line
    7  => q/\x1b\[\d+;\d+r/, #code_enable_scroll
    68 => q/\e\[\??\d+(;\d+)*[A-Za-z]/, #VLZ addon from ytti/oxidized
);

# https://github.com/ollyg/Net-CLI-Interact/issues/22
around 'buffer' => sub {
    my $orig = shift;
    my $buffer = ($orig->(@_) || '');

    # remove control characters
    $buffer =~ s/[\000-\010\013\014\016-\032\034-\037]//g;

    # strip ANSI terminal codes
    foreach my $code (sort keys %ansi_codes) {
        my $to = '';
        $to = "\n" if ($code == 4); # CODE_NEXT_LINE must substitute with '\n'
        $buffer =~ s/$ansi_codes{$code}/$to/g;
    }

    return $buffer;
};

1;