Curses-UI

 view release on metacpan or  search on metacpan

lib/Curses/UI/Searchable.pm  view on Meta::CPAN

#
$INC{'Curses/UI/SearchEntry.pm'} = $INC{'Curses/UI/Searchable.pm'};


# ----------------------------------------------------------------------
# Searchable package
# ----------------------------------------------------------------------

package Curses::UI::Searchable;

use strict;
use Curses;
use Curses::UI::Common;
require Exporter;

use vars qw(
    $VERSION 
    @ISA 
    @EXPORT
);

$VERSION = '1.10';

@ISA = qw(
    Exporter
);

@EXPORT = qw(
    search_forward
    search_backward
    search
    search_next
);

sub search_forward()
{
    my $this = shift;
    $this->search("/", +1);
}

sub search_backward()
{
    my $this = shift;
    $this->search("?", -1);
}

sub search()
{
    my $this   = shift;
    my $prompt = shift || ':';
    my $direction   = shift || +1; 

    $this->change_canvasheight(-1);
    $this->draw;

    my $querybox = new Curses::UI::SearchEntry(
        -parent   => $this,
        -prompt   => $prompt,
    );

    my $old_cursor_mode = $this->root->cursor_mode;
    $this->root->cursor_mode(1);
    $querybox->getobj('entry')->{-focus} = 1;
    $querybox->draw;
    $querybox->modalfocus();
    $querybox->getobj('entry')->{-focus} = 0;

    my $query = $querybox->get;
    $querybox->prompt(':');
    $querybox->draw;
    
    my $key;
    if ($query ne '')
    {
        my ($newidx, $wrapped) = 
        $this->search_next($query, $direction);

        KEY: for (;;)
        {
            unless (defined $newidx) {
                $querybox->text('Not found');
            } else {
                $querybox->text($wrapped ? 'Wrapped' : '');
            }
	    $querybox->pos(0);
            $querybox->draw;

            $querybox->{-key} = '-1';
            while ($querybox->{-key} eq '-1') {
	       $this->root->do_one_event($querybox);      
            }

            if ($querybox->{-key} eq 'n') { 
                ($newidx, $wrapped) = 
                    $this->search_next($query, $direction);
            } elsif ($querybox->{-key} eq 'N') {
                ($newidx, $wrapped) = 
                    $this->search_next($query, -$direction);
            } else {
                last KEY;
            }
        }
    }

    # Restore the screen.
    $this->root->cursor_mode($old_cursor_mode);
    $this->change_canvasheight(+1);
    $this->draw;

    $this->root->feedkey($querybox->{-key});
    return $this;
}

sub search_next($$;)
{
    my $this = shift;
    my $query = shift;
    my $direction = shift;
    $direction = ($direction > 0 ? +1 : -1);
    $this->search_get($query, $direction);
}

sub change_canvasheight($;)
{
    my $this = shift;
    my $change = shift;

    if ($change < 0)
    {
	# Change the canvasheight, so we can fit in the searchline.
	$this->{-sh}--;
	$this->{-yscrpos}++
	    if ($this->{-ypos}-$this->{-yscrpos} == $this->canvasheight);
    }
    elsif ($change > 0)
    {
	# Restore the canvasheight.
	$this->{-sh}++;
	my $inscreen = ($this->canvasheight 
                     - ($this->number_of_lines 
                     - $this->{-yscrpos}));
	while ($this->{-yscrpos} > 0 and 
	       $inscreen < $this->canvasheight)
        {
	    $this->{-yscrpos}--;
	    $inscreen = ($this->canvasheight 
                      - ($this->number_of_lines 
                      - $this->{-yscrpos}));
	}
    }

    $this->{-search_highlight} = undef;
    $this->layout_content();
}

sub search_get($$;)
{
    my $this      = shift;
    my $query     = shift;
    my $direction = shift || +1;

    my $startpos = $this->{-ypos};
    my $offset = 0;
    my $wrapped = 0;
    for (;;)
    {



( run in 0.525 second using v1.01-cache-2.11-cpan-39bf76dae61 )