#!/usr/pkg/bin/perl

# simple filter to remove colour codes, xterm titles and 'el' 
# (clear to end-of-line) sequences from prompt

use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;

my $filter = new RlwrapFilter;
my $name = $filter -> name;

$filter -> help_text("Usage: rlwrap -z '$name [-o]' <command>\n".
		     "removes all junk from prompt\n"
);



$filter -> prompt_handler(\&scrub);
$filter -> run;

sub scrub {
    my ($dirty) = @_;
    my $clean = $dirty;
    $clean =~ s/^.*\r//; 
    $clean =~ s/\x1b]0;.*?\x07//g; # remove xterm title control sequence
    $clean =~ s/\x1b.*?m//g; # remove colour codes
    $clean =~ s/\x1b\[K//g; # clear to end of line in xterm
    $clean =~ s/[\x01-\x1f]//g;
    $clean =~ /[[control:]]/ and die "couldn't clean promt: still dirty\n"; 
    while ($clean =~ s/.\x08//g) {}; # let backspaces eat the chars before them
    return $clean;   
}


