App-AltSQL
view release on metacpan or search on metacpan
lib/App/AltSQL/Term.pm view on Meta::CPAN
package App::AltSQL::Term;
use Moose;
use Term::ReadLine::Zoid;
use Data::Dumper;
use JSON qw(encode_json decode_json);
use Term::ANSIColor;
with 'App::AltSQL::Role';
with 'MooseX::Object::Pluggable';
has 'term' => (
is => 'ro',
lazy_build => 1,
);
has 'prompt' => (
is => 'rw',
default => 'altsql> ',
);
has 'history_fn' => ( is => 'ro' );
has 'autocomplete_entries' => ( is => 'rw' );
sub args_spec {
return (
history_fn => {
cli => 'history=s',
default => $ENV{HOME} . '/.altsql_history.js',
help => '--history FILENAME',
},
);
}
sub BUILD {
my $self = shift;
$self->log_info("Ctrl-C to reset the line; Ctrl-D to exit");
}
sub setup {
my $self = shift;
if (my $custom_prompt = $self->app->config->{prompt}) {
$self->prompt($custom_prompt);
}
}
sub _build_term {
my $self = shift;
my $term = Term::ReadLine::Zoid->new("altsql-shell");
$self->{term} = $term;
# Require the tab key to be hit twice before showing the list of autocomplete items
$term->Attribs->{autolist} = 0;
$term->Attribs->{completion_function} = sub {
$self->completion_function(@_);
};
$term->Attribs->{beat} = sub {
# Check on things in the background here; called every second there is no input from the user
};
$term->bindkey('^Z', sub {
# The Term::ReadLine::Zoid uses Term::ReadKey in 'raw' mode which disables all signals
# If we can find a way to background ourselves at this point, that's the only option that I can see
$self->log_info("Backgrounding is not currently possible");
});
$term->bindkey('^D', sub {
print "\n";
$self->app->shutdown();
});
$term->bindkey('return', sub { $self->return_key });
$self->read_history();
return $term;
}
sub return_key {
my $self = shift;
## The user has pressed the 'enter' key. If the buffer ends in ';' or '\G', or if they've typed the bare word 'quit' or 'exit', accept the buffer
my $input = join ' ', @{ $self->term->{lines} };
if ($input =~ m{(;|\\G|\\c)\s*$} || $input =~ m{^\s*(quit|exit)\s*$} || $input =~ m{^\s*$}) {
$self->term->accept_line();
}
else {
$self->term->insert_line();
}
}
sub readline {
my $self = shift;
return $self->term->readline($self->render_prompt());
}
sub completion_function {
my ($self, $word, $buffer, $start) = @_;
#$self->log_debug("\ncompletion_function: '$word', '$buffer', '$start'");
my $hash = $self->autocomplete_entries;
return () unless $hash;
my @matches;
foreach my $key (sort keys %$hash) {
push @matches, $key if $key =~ m/^$word/i;
}
return @matches;
}
sub write_history {
my ($self, $fn) = @_;
$fn ||= $self->history_fn;
if (! $fn) {
return;
}
open my $out, '>', $fn or die "Can't open $fn for writing: $!";
print $out encode_json({ history => [ $self->term->GetHistory ] });
close $out;
( run in 0.662 second using v1.01-cache-2.11-cpan-fe3c2283af0 )