Sandor Szücs
7/18/2008 7:15:00 PM
Hi,
Sorry for my first post, it wasn't what you not already knew.
I should better read then write, but I hope on the bottom I found
something that help.
On 17.07.2008, at 18:44, Marc Heiler wrote:
> is there a way to "find out" what a user already typed in readline?
>
> Reason is, i am fetching user input like so:
>
>
> user_input =3D Readline.readline(' Input name of a dir, then press
> <TAB>'+"\n",true)
>
> And with Readline.completion_proc i have code that will return only =20=
> dirs
> in this directory. But I would like to invoke this specific code part
> only
> when a user typed in i.e. "cd "
>
> So if i have a directory called "foobar" it should work here:
>
>
> "cd foo<TAB>" # completed to foobar
>
> but if the user would type
>
> "blabalblabla foo<TAB>" # there should be no result
I tested a bit your scenario and I think you have to set
Readline.completer_word_break_characters , but not to " \t\n\"\=20
\'`><=3D;|&{("
If you set it to "\t\n\"\\'`><=3D;|&{(", without whitespace, you will =20=
get as
input in your CompletionProc the whole line.
Then you can do:
when /^cd/
typed =3D input.sub("cd","").strip
...
$ tabcompletion_test.rb
cd bl|cd bl| # tab called after 'cd bl'
$ cat tabcompletion_test.rb
require 'readline'
module MYCMD
ReservedWords =3D [
"BEGIN", "END", "yield",
]
CompletionProc =3D proc { |input|
puts("|#{input}|")
case input
when /^cd/
typed =3D input.sub("cd","")
if typed=3D=3D""
candidates =3D Dir.entries(".")
elsif typed =3D=3D " "
candidates =3D Dir.entries(".")
else
candidates =3D ["no"]
end
else
(candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
end
}
def self.select_message(receiver, message, candidates)
candidates.grep(/^#{message}/).collect do |e|
puts("e:#{e}")
case e
when /^[a-zA-Z_]/
receiver + "." + e
end
end
end
end
## no ' '
Readline.completer_word_break_characters=3D "\t\n\"\\'`><=3D;|&{("
Readline.completion_append_character =3D nil
Readline.completion_proc =3D MYCMD::CompletionProc=09
loop do
cmd =3D Readline.readline
eval(cmd.chomp)
end
regards, Sandor Sz=FCcs
--