Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Using Tab Completion on Windows in cmd.exe

by Stamm (Acolyte)
on Nov 24, 2011 at 16:22 UTC ( #939918=perlquestion: print w/ replies, xml ) Need Help??
Stamm has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I tried all the examples in Term::Shell, Term::ShellUI, Term::Complete and others. Tab completion never works. It just displays a tab character. Is it possible to have this facility on Windows in cmd.exe or is it a Unix privilege?

Thanks in advance.

Stamm

Comment on Using Tab Completion on Windows in cmd.exe
Re: Using Tab Completion on Windows in cmd.exe
by cavac (Chaplain) on Nov 24, 2011 at 18:16 UTC

    Can't really help you about tab completion. You could read in the keypresses chararcter by character and write your own parser. But i guess this should already be a solved problem.

    A workaround would be to use Term::Readline. You could - for example - prepolulate the history. Something like this:

    #!/usr/bin/perl use strict; use warnings; use Term::ReadLine; my $term = Term::ReadLine->new('Tabtest'); my $prompt = "Test your tabs, guv: "; if ($^O eq "MSWin32" || $^O eq "dos") { my $INPUT; open($INPUT, "<con") and $term->newTTY($INPUT, $term->OUT); } foreach my $command (qw[print hello echo foo bar baz]) { $term->addhistory($command); } while(defined(my $line = $term->readline($prompt))) { chomp $line; exit if($line eq "exit"); print "I don't know how to '$line'\n"; $term->addhistory($line); }

    Not quite what you wanted, i know, but i'm also fresh out of ideas.

    Don't use '#ff0000':
    use Acme::AutoColor; my $redcolor = RED();
    All colors subject to change without notice.
      Thanks for your answer. And yes, I thought it would have been solved too.

      As for your code, I'm not sure I understand. Command history already works.

      Maybe I should have Term::ReadLine::Gnu. But I can't install it. It says libtermcap.a or libcurses.a is missing. Anybody know what I should be doing?

        libtermcap.a or libcurses.a is missing. Anybody know what I should be doing?

        Install libetrmcap or libcurses, obviously :)

        Or try Term::ReadLine::Perl

Re: Using Tab Completion on Windows in cmd.exe
by Anonymous Monk on Nov 24, 2011 at 18:33 UTC
    Term::ReadKey ought to work, and anything using ReadKey(-1) for completion

    http://deps.cpantesters.org/depended-on-by.pl?dist=TermReadKey-2.30

      Here is a proof of concept, uses Data::Dump::Streamer and cpan STSI/TermReadKey-2.30.02.tar.gz (or cpanp i STSI/TermReadKey-2.30.02.tar.gz)

      use Term::ReadKey; use strict; use warnings; my @completion_list = qw[ ro sham bo foo bar baz ]; { my $orig_prompt = "Hi: "; my $prompt = $orig_prompt; my $prev_prompt = $orig_prompt; my $tab = do { my $ix = -1; my $xx = @completion_list - 1; sub { $ix++; return $completion_list[ $ix % $xx ]; }; }; $|=1; print $prompt; my $key ; while( 1 ){ $key = ReadKey(-1); next unless defined $key; if( $key eq "\r"){ print "\n"; last; } elsif( $key eq "\t" ){ my $completion = $tab->(); $prompt = $orig_prompt . $completion; #~ FAIL print "\b" x ( 1 + length $prev_prompt ), $prompt; #~ FAIL print "\b" x ( 100 ), $prompt; #~ FAIL print "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\ +b\b\b\b$prompt"; print "\r" , " " x ( length $prev_prompt ); print "\r", $prompt; $prev_prompt = $prompt; } elsif( $key eq "\e") { print "\r" , " " x ( length $prev_prompt ); $prompt = $orig_prompt ; print "\r", $prompt; $prev_prompt = $prompt; } else { use DDS; warn Dump $key ; } } use DDS; warn Dump [ $key, $orig_prompt, $prompt, $prev_prompt ] ; }

      It could make a great addition to Term::Interact/example

        Thanks a lot for your script. It works.

        So it means I have to reinvent the wheel and come up with my own readline function with tab completion support. I'll do that then, with the help of your script.

        Thanks again. :-)
Re: Using Tab Completion on Windows in cmd.exe
by Not_a_Number (Parson) on Nov 24, 2011 at 18:55 UTC

    Please forgive me (and feel free to downvote me) if this is a silly question - I know next to zero about Unix tab completion - but in what way does cmd.exe's own tab completion fail to meet your needs?

      tab completion on cmd.exe does pwd readdir completion, or C:\WIN*TAB* becomes C:\WINDOWS

      That is great if that is what you want, but if you want to provide your own keywords, not so much

      cmd.exe can complete file and directory names. Well, most of the time anyway. And it has a (not very smart) history of the commands you previously typed. But thats just about it.

      Modern unix shells are way smarter and more flexible than that. Take this introduction for bash for example (part 2 is here): You can configure it more or less any way you like. A look at the bash manual reveals a ton of other possibilities as well.

      And there's a bunch of other smart shells out there as well. On Linux/Unix just pick what you like.

      On Windows, you have to install Cygwin... To be fair, Windows was not designed to be used from the command line while Unix was. Windows was actually designed to move the User away from the command line - which didn't work out so well. In the last few years Microsoft reversed direction and started projects like the PowerShell. And now they are moving to JavaScript ;-)

      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.
Re: Using Tab Completion on Windows in cmd.exe
by ikegami (Pope) on Nov 24, 2011 at 20:23 UTC
    I don't follow. What do Perl modules have to do with name completion in cmd?

      Well, Stamm wants to write a console Perl script that features tab completion. It seems most (all?) Perl modules tested so far seem to rely on some unix feature or other, or just don't support tab completion.

      Tab completion does not necessarily means name completion. For example, an interactive Eliza program might use it as well. So if the user types

      # I like tab c(tab)
      
      tab completion might turn it into
      # I like tab completion.
      
      to which the correct response would be of course
      Tell me more.
      

      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.
        Ok, so not in cmd, but at his own prompt.
Re: Using Tab Completion on Windows in cmd.exe
by Stamm (Acolyte) on Nov 25, 2011 at 22:40 UTC
    I noticed that, by default, the environment variable TERM is set to DUMB on Windows. If I unset it, tab completion works in all packages. It's actually Term::ReadLine::Perl who is called behind the scenes.

    Unfortunately, I can't use arrow keys anymore to edit the line. I can only use Ctrl-B & Ctrl-F like in vi.

    Is there a way to have the arrow key comfort? By looking at the source, I suspect it can be done with Keymaps in the Term::ReadLine::Perl package, but there's no documentation. Anybody knows how to do that?

      Hmm, unsetting term=dumb doesn't work for me on winxp, which windows do you have?

        I'm using Windows 7. I think you must have Term::ReadLine::Perl installed. Otherwise, Term::ReadLine::Stub will be used, which doesn't do word completion.
Re: Using Tab Completion on Windows in cmd.exe
by Discipulus (Curate) on Nov 25, 2014 at 09:58 UTC
    For future reference: does not use the readline::rl_basic function directly! instead take a look at this post to implement something reliable.
    HtH
    L*


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939918]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-27 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls