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.
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.
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 =~ /-+$/ 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
-=-=-=-
Ollivier ROBERT -=- Eurocontrol EEC/ITM -=- roberto@eurocontrol.fr
Usenet Canal Historique FreeBSD: The Power to Serve!
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.