Highline - question with multiple choices

S

szimek

Hi,

I've got an array of user logins and need to provide a way to select
one or more of them with auto-completion. Currently I've got code
straight from readline example:

users = ask("Select users: ", user_logins) do |q|
q.readline = true
end

The problem is that it says "You must choose one of...." if I select
more than one login. How to allow to select multiple choices?
 
J

James Gray

Hi,
Hello.

I've got an array of user logins and need to provide a way to select
one or more of them with auto-completion. Currently I've got code
straight from readline example:

users = ask("Select users: ", user_logins) do |q|
q.readline = true
end

The problem is that it says "You must choose one of...." if I select
more than one login. How to allow to select multiple choices?

Is it okay to ask the user to enter one login per line with a blank
line to end? If so, this code should work:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "highline/import"

LOGINS = %w[bob joe dave alice hal]

selected = ask("Select Users:", LOGINS + [""]) do |q|
q.readline = true
q.gather = ""
end
p selected

__END__

Hope that helps.

James Edward Gray II
 
S

szimek

Hi,
Hello.

I've got an array of user logins and need to provide a way to select
one or more of them with auto-completion. Currently I've got code
straight from readline example:
users =3D ask("Select users: ", user_logins) do |q|
=A0 =A0q.readline =3D true
end
The problem is that it says "You must choose one of...." if I select
more than one login. How to allow to select multiple choices?

Is it okay to ask the user to enter one login per line with a blank =A0
line to end? =A0If so, this code should work:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "highline/import"

LOGINS =3D %w[bob joe dave alice hal]

selected =3D ask("Select Users:", LOGINS + [""]) do |q|
=A0 =A0q.readline =3D true
=A0 =A0q.gather =A0 =3D ""
end
p selected

__END__

Hope that helps.

James Edward Gray II

Thanks, I'll probably use this solution for now, but is it possible at
all with highline to make it in a single line?

Another question: I'd like user to be able to select a ticket number
from a list with auto-complete. But if user presses tab, I'd like to
display not only possible ticket numbers, but their descriptions as
well - one entry (i.e. "1234 - something doesn't work") per line, if
it's possible.
 
J

James Gray

Hi,
Hello.

I've got an array of user logins and need to provide a way to select
one or more of them with auto-completion. Currently I've got code
straight from readline example:
users = ask("Select users: ", user_logins) do |q|
q.readline = true
end
The problem is that it says "You must choose one of...." if I select
more than one login. How to allow to select multiple choices?

Is it okay to ask the user to enter one login per line with a blank
line to end? If so, this code should work:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "highline/import"

LOGINS = %w[bob joe dave alice hal]

selected = ask("Select Users:", LOGINS + [""]) do |q|
q.readline = true
q.gather = ""
end
p selected

__END__

Hope that helps.

James Edward Gray II

Thanks, I'll probably use this solution for now, but is it possible at
all with highline to make it in a single line?

Not with autocompletion, no. HighLine doesn't allow you to manually
set the autocompletion Proc used by Readline. Perhaps we should, but
then if you do that you probably aren't gaining anything from HighLine
and you should probably just use Readline manually.
Another question: I'd like user to be able to select a ticket number
from a list with auto-complete. But if user presses tab, I'd like to
display not only possible ticket numbers, but their descriptions as
well - one entry (i.e. "1234 - something doesn't work") per line, if
it's possible.

Sure, just set an Array of Strings with the number and description as
you show above for the values. They will auto complete fine, and you
can strip the description after you have their answer.

James Edward Gray II
 
T

Tomasz Krakowski

Hello all, I'm new here and i have a problem...
Can you tell me what I'm doing wrong?
Here is fragment of my code:
choose do |menu|
menu.prompt = "Please choose your test: "
for j in 0..ip.length-1
menu.choice:)"#{ip[j][0]}\t\t\t\t#{ip[j][1]} : #{port}",
"blahblah#{j}") do
system "cls"
ParsePING(ip[j][0], ip[j][1], port)
end
end#for
end

All options prints great. But when I choose any option, always executes
only last one... It looks like every menu option have implemented
ParsePING function with last j parameter.
Can someone help?

Kraku

Happy new year for everyone!
 
J

James Gray

Hello all, I'm new here and i have a problem...
Welcome.

Can you tell me what I'm doing wrong?

I'll try to help.
Here is fragment of my code:
choose do |menu|
menu.prompt = "Please choose your test: "
for j in 0..ip.length-1
menu.choice:)"#{ip[j][0]}\t\t\t\t#{ip[j][1]} : #{port}",
"blahblah#{j}") do
system "cls"
ParsePING(ip[j][0], ip[j][1], port)
end
end#for
end

All options prints great. But when I choose any option, always
executes
only last one... It looks like every menu option have implemented
ParsePING function with last j parameter.
Can someone help?

I don't see an obvious problem with the code, so I suggest we start by
validating ParsePING(). Without using HighLine at all, can you just
manually call it with a few different parameters and make sure it is
doing what you expect?

James Edward Gray II
 
T

Tomasz Krakowski

That works as I expect it to. Is it the same for you?

Yes it is ok but I'm in need to create that menu dynamically. I've done
already menu with 'static' menu items and that works. Now I have
problem,as you can see, with dynamic one.

Anyway, I recommend making sure that variable holds what you think it
does.

Yes I'm sure, by now it is included from other source file and looks
like that:
$ip = [
["tvp1", "238.269.2.1"],
["tvp2", "219.249.2.2"],
["tvp info", "219.269.2.3"],
["polsat", "219.269.2.4"], blahblah etc. one hundred more of them...In
the future it will be loaded from txt file in runtime, but now if
dynamic cration of menu items not work it makes no sense.

aa = a.find { |line| line.include? "Average" } # for Ruby 1.8
or:
aa = a.lines.find { |line| line.include? "Average" } # for Ruby 1.9

Very nice :), thank you. I'm only seasonal coder so I'm using syntax
which is familiar to me :) I'm customer support who wants to make my own
life slighly easier.
 
J

James Gray

Yes it is ok but I'm in need to create that menu dynamically.

Did you notice that the example I gave created a menu dynamically?
Anyway, I recommend making sure that variable holds what you think it
does.

Yes I'm sure, by now it is included from other source file and looks
like that:
$ip =3D [
["tvp1", "238.269.2.1"],
["tvp2", "219.249.2.2"],
["tvp info", "219.269.2.3"],
["polsat", "219.269.2.4"], blahblah etc. one hundred more of them...

Alright, I'm out of easy guesses. :)

Here's what we can do. You can build a minimal script I can run to =20
see how HighLine is broken. That should be pretty easy, since you can =20=

just drop in the variable definition above and your ParsePING() method =20=

definition.

Please try to reduce the code as much as possible though, to just show =20=

the problem. For example, you probably don't need to shell out. You =20=

could instead just print the command you would have executed.

Send me that script and the steps to see the problem. "I picked the =20
third menu option by typing=85," will be fine.

I promise to use that to fix any bugs it uncovers. Fair enough?

James Edward Gray II
 
T

Tomasz Krakowski

James said:
I promise to use that to fix any bugs it uncovers. Fair enough?

Thank you James for your patience :)

Here is sample script:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "highline/import"

ip = [
["tvp1", "219.239.2.1"],
["tvp2", "219.239.2.2"],
["tvp info", "219.239.2.3"],
["polsat", "219.239.2.4"],
["tvn", "219.239.2.5"],
["tvp kultura", "219.239.2.6"],
["tvp polonia", "219.239.2.7"],
["tvp historia", "219.239.2.9"]]

choose do |menu|
menu.prompt = "Please choose your test: "
for j in 0..ip.length-1
menu.choice:)"#{ip[j][0]}\t\t\t#{ip[j][1]} ", "blabla#{j}")
do
puts "#{ip[j][0]}\t\t\t#{ip[j][1]} - your choice"
end
end#for
end

on my compilation there is always executed last option. Does not matter
which one you pick.

Regards
 
J

James Gray

Thank you James for your patience :)

I'm glad we're getting it figured out.
Here is sample script:

OK, the short story is that there's no bugs here. You are just
running into surprising "features" of Ruby.

First, let me show how to fix your code:
#!/usr/bin/env ruby -wKU

require "rubygems"
require "highline/import"

ip = [
["tvp1", "219.239.2.1"],
["tvp2", "219.239.2.2"],
["tvp info", "219.239.2.3"],
["polsat", "219.239.2.4"],
["tvn", "219.239.2.5"],
["tvp kultura", "219.239.2.6"],
["tvp polonia", "219.239.2.7"],
["tvp historia", "219.239.2.9"]]

choose do |menu|
menu.prompt = "Please choose your test: "
for j in 0..ip.length-1

ip.each_with_index do |row, i|
menu.choice:)"#{ip[j][0]}\t\t\t#{ip[j][1]} ",
"blabla#{j}") do

menu.choice("#{row.first}\t\t\t#{row.last} ", "blabla#{i}") do
puts "#{ip[j][0]}\t\t\t#{ip[j][1]} - your choice"

puts "#{row.first}\t\t\t#{row.last} - your choice"
end
end#for
end

Those simple changes should make your code run as expected.
on my compilation there is always executed last option. Does not
matter
which one you pick.

For an explanation of why you were seeing this, please see this write
up on my blog:

http://blog.grayproductions.net/articles/the_evils_of_the_for_loop

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top