Skip to content

Commit

Permalink
Merge pull request namecoin#192 from mnp/namecoinq
Browse files Browse the repository at this point in the history
Add contrib script nmcsh - simple namecoind readline wrapper
  • Loading branch information
phelixbtc committed Jan 2, 2015
2 parents a00c33d + 923422b commit 6273022
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions contrib/nmcsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/perl
#
# nmcsh - simple namecoind readline wrapper
#
# This provides history (Ctrl-p, Ctrl-n) command completion (TAB on an
# incomplete command), argument help (TAB after a finished command),
# and the other usual readline keys. See
# http://tiswww.case.edu/php/chet/readline/rluserman.html for
# details.
#
# Before running this, make sure namecoind is in your $PATH and the
# dameon is running.
#
# Copyright (c) 2014 Mitchell Perilstein
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING.

use strict;
use warnings;
use Term::ReadLine;

my $NMCD = 'namecoind'; # must be in your $PATH
my $PROMPT = 'nmc> ';
my $PAGER = defined $ENV{PAGER} ? "| $ENV{PAGER}" : '';

my $term = Term::ReadLine->new('namecoind wrapper');
my $OUT = $term->OUT || \*STDOUT;

# grok the help for command completion strings
my %cmds;
my $r = open NMC, "$NMCD help|" or die $!;
while (<NMC>) {
my ($cmd, @rest) = split;
$cmds{$cmd} = join " ", @rest;
}
close NMC or die $!;

# complete on partial commands or show help for finished ones
sub my_complete {
my ($text, $line, $start) = @_;

return grep(/^$text/, keys %cmds)
if $start == 0;

my $cmd = (split(/\s+/, $line))[0];
if ($cmd) {
print "($cmd)";
return "$cmd $cmds{$cmd}";
}

return '';
}

$readline::rl_completion_function = undef;
$readline::rl_completion_function = "main::my_complete";

# main loop: prompt, exec command, show result
while (defined ($_ = $term->readline($PROMPT)) ) {
if ('quit' eq $_ || 'exit' eq $_ || 'q' eq $_) {
print $OUT "bye\n";
exit;
}

next unless $_;

# you could set up a bidirectional child but exec each time is fast
# enough and much simpler
my $res = `$NMCD $_ $PAGER`;
if ($@) {
warn $@;
}
else {
print $OUT "[$res]" unless $@;
}

$term->addhistory($_) if /\S/;
}

0 comments on commit 6273022

Please sign in to comment.