#!/usr/bin/env perl

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



my $filter = new RlwrapFilter;


$filter -> help_text("Usage: rlwrap [-options] -z null <command>\n".
		     "a filter that does nothing, but can test the effect of (slow) filters\n".
                     "(only if debug flags 'filtering' and 'random delay' are set" );

$filter -> minimal_rlwrap_version("0.44");

my $process_all_messages = $ENV{RLWRAP_DEBUG} & DEBUG_FILTERING;
my $delay_all_responses  = $ENV{RLWRAP_DEBUG} & DEBUG_RANDOM_DELAY;


sub wait_a_little_and_return {
    my($retval) = @_;
    if ($delay_all_responses) { 
        select(undef, undef, undef, 0.1*rand()); # Emulate a filter that takes a lot of time to respond
    }
    return($retval);
}


if ($process_all_messages) {
    # make this filter "interested" in all message types (important for debugging)
    $filter -> send_output_oob("debugging filtering: all message types will be processed" . ($delay_all_responses ? " and delayed.\n" : ".\n"));
    #$filter -> output_handler(sub {wait_a_little_and_return($_)});
    $filter -> echo_handler(sub {wait_a_little_and_return($_)});
    $filter -> input_handler(sub {wait_a_little_and_return($_)});
    $filter -> signal_handler(sub {wait_a_little_and_return($_)});
    $filter -> prompt_handler(sub {wait_a_little_and_return($_)});
    $filter -> history_handler(sub {wait_a_little_and_return($_)});
    $filter -> completion_handler(sub {shift;shift; wait_a_little_and_return(), return @_});
} else {
    # just keep the unused handlers undefined
}

$filter -> run;
