Interactive Console Program with Tab-Completion 
Author Message
 Interactive Console Program with Tab-Completion

Hello,

I am running into a lot of trouble trying to find a good way to
implement my interactive console program. Here is the idea. You are
given a prompt (let's get creative and use "prompt> "). There are,
lets say, two commands. "add" and "delete". When a user types "add",
enters a space, and then hit tabs, the tab completion should return a
list of files in the current directory. When the user types delete,
space, and then tab, however, the user should get a list of
directories that have already been selected with add.

The problem with using the readline extension for this, as some as you
may know, is it does not allow you to get any text before the last
space (which is crucial, since that is where my command is). Someone
did submit a patch to ruby-talk to fix  this problem, but it breaks
backwards compatability (well, that and for some reason it wasn't
working quite right for me :P)

I was wondering if anyone had a good solution to this. I guess the
least desirable option would be writing a new readline-like interface.
If someone has a better idea, though, or has alreayd done this, please
let me know. Thanks.



Tue, 29 Nov 2005 08:38:16 GMT  
 Interactive Console Program with Tab-Completion
bash (and I belive tcsh) have programmable completion that does exactly
what you are asking for.  Maybe a look into their source code helps.

see here for a description of the bash feature
http://www.gnu.org/manual/bash/html_mono/bashref.html#SEC110

Regards

    robert



Quote:
> Hello,

> I am running into a lot of trouble trying to find a good way to
> implement my interactive console program. Here is the idea. You are
> given a prompt (let's get creative and use "prompt> "). There are,
> lets say, two commands. "add" and "delete". When a user types "add",
> enters a space, and then hit tabs, the tab completion should return a
> list of files in the current directory. When the user types delete,
> space, and then tab, however, the user should get a list of
> directories that have already been selected with add.

> The problem with using the readline extension for this, as some as you
> may know, is it does not allow you to get any text before the last
> space (which is crucial, since that is where my command is). Someone
> did submit a patch to ruby-talk to fix  this problem, but it breaks
> backwards compatability (well, that and for some reason it wasn't
> working quite right for me :P)

> I was wondering if anyone had a good solution to this. I guess the
> least desirable option would be writing a new readline-like interface.
> If someone has a better idea, though, or has alreayd done this, please
> let me know. Thanks.



Tue, 29 Nov 2005 18:23:30 GMT  
 Interactive Console Program with Tab-Completion

Quote:

> bash (and I belive tcsh) have programmable completion that does exactly
> what you are asking for.  Maybe a look into their source code helps.

But bash (and probably tcsh too) uses readline...

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Linux: The OS people choose without $200,000,000 of persuasion.
        -- Mike Coleman



Tue, 29 Nov 2005 18:54:35 GMT  
 Interactive Console Program with Tab-Completion



Quote:

> > bash (and I belive tcsh) have programmable completion that does
exactly
> > what you are asking for.  Maybe a look into their source code helps.

> But bash (and probably tcsh too) uses readline...

That's why I suggested to look into their source code. :-)

    robert



Tue, 29 Nov 2005 19:57:58 GMT  
 Interactive Console Program with Tab-Completion


Quote:
> But bash (and probably tcsh too) uses readline...

Nope, tcsh being a BSD licensed program doesn't use the GPL readline. It
has its own readline-like code (which existed before readline BTW).

Zsh has also its own code for this.

To stay in line with the original post, readline is very easy to use in
Ruby:

-=-=-=-
#! /usr/bin/env ruby
#
# $Id: //depot/src/ruby/dices.rb#1 $

require "readline"

class Fixnum
  def dice(n = 6)
    res = 0
    print "  "
    self.times {
      dice = rand(n) + 1
      print dice.to_s + " "
      res += dice
    }
    puts ""
    return res
  end
end

class String
  def has_mod?
    if self =~ /[-+](\d+)$/ then
      mod = $1.to_i
      puts "  Bonus => #{mod}"
      return mod
    else
      return 0
    end
  end
end

if $0 == __FILE__ then
  srand
  while true do
    input = Readline::readline("Dice> ", true)
    if input then
      input.chomp!
      input.gsub!(/\s*/, '')
      bonus = 0
      case input
      when /q/i
        break
      when /^(\d+)d(\d+)/
        nb = $1.to_i
        size = $2.to_i
        res = nb.dice(size)
        mod = input.has_mod?
        puts "  #{nb} x D#{size} => #{res + mod} (#{res})"
      when /^d(\d+)/
        size = $1.to_i
        res = 1.dice(size)
        if res == 1 then
          puts "  **FUMBLE**"
        elsif res == size
          puts " **Natural #{size}**"
        else
          mod = input.has_mod?
          puts "  D#{size} => #{res + mod} (#{res})"
        end
      end
    else
      puts ""
      break
    end
  end
end
-=-=-=-
--

Usenet C{*filter*}Historique                   FreeBSD: The Power to Serve!



Tue, 29 Nov 2005 20:13:38 GMT  
 Interactive Console Program with Tab-Completion

Quote:
> To stay in line with the original post, readline is very easy to use in
> Ruby:

I'm not having a problem using the features that ruby's readline
provides. The problem is the ruby implementation does not deal well
with spaces. The text returned by the completion function is anything
from the last space onwards, instead of the entire line.


Wed, 30 Nov 2005 01:26:15 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. rlcompleter2 <TAB> interactive completion

2. rlcompleter2 <TAB> interactive completion

3. ANN: rlcompleter2 0.95 Interactive Completion

4. function/variable name completion in interactive mode of the interpreter

5. ANN: rlcompleter2 0.95 Interactive Completion

6. TAB completion

7. tab completion in cmd

8. completion on tab?

9. CLISP tab completion problem

10. tab completion on clisp

11. Q Porting HP Console-Based FORTRAN Program to Win32 Console

12. Spawning console programs with I/O redirection and (preferably no) visible console

 

 
Powered by phpBB® Forum Software