Forum: Ruby HighLine - examples for using "list"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 07:55
(Received via mailing list)
http://highline.rubyforge.org/doc/classes/HighLine.html

I'm still very green, and I find it easier to read code than
documentation.  I'm not sure how to use HighLine's "list" feature
(specifically colums_across) and I can't find any example code
anywhere.

Does anyone have experience with list?
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-04-15 13:44
(Received via mailing list)
On 15-Apr-06, at 1:52 AM, Sy Ali wrote:

> http://highline.rubyforge.org/doc/classes/HighLine.html
>
> I'm still very green, and I find it easier to read code than
> documentation.  I'm not sure how to use HighLine's "list" feature
> (specifically colums_across) and I can't find any example code
> anywhere.
>
> Does anyone have experience with list?

Have you tried playing with it?  I had a look at the docs and then
grabbed the gem; does this (or its output) give you anything help:

#!/usr/bin/env ruby

require 'rubygems'
require 'highline'

h = HighLine.new
names = %w( Adrian Tony Bill Robert Peter Michael John Keith )

puts h.list(names)
puts h.list(names, :columns_across)
puts h.list(names, :columns_across, 3)
h.wrap_at = 20
puts h.list(names, :columns_across)

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 17:00
(Received via mailing list)
On 4/15/06, Mike Stok <mike@stok.ca> wrote:
> On 15-Apr-06, at 1:52 AM, Sy Ali wrote:
> > http://highline.rubyforge.org/doc/classes/HighLine.html

> Have you tried playing with it?  I had a look at the docs and then
> grabbed the gem; does this (or its output) give you anything help:

I did play with it, but just wasn't able to figure out the proper
syntax.

Although I have worked with things in the form of
a = <value>
puts h.list(a, :rows)

I have not yet worked with anything in the form of
h = HighLine.new

So I was trying all kinds of wacky and totally incorrect things.

Thanks to your code I can get it to run enough to understand what's
going on.  Now I need to figure out how to format things the way it
expects, or otherwise use it correctly.

Maybe I misunderstand what it can do.  I'll play some more.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 17:19
(Received via mailing list)
Ok, I played with it some more and it appears it won't do what I want.
 I'll have to hunt some more, or code my own solution.

require 'rubygems'
require 'highline'

h = HighLine.new
a = []
a << "a a a a"
a << "b b b b b"
a << "--Some more text-- --Some more text-- --Some more text-- --Some
more text-- --Some more text--"

puts h.list(a, :columns_across)  # explodes [1]
puts h.list(a, :columns_down)    # explodes [1]
puts h.list(a, :rows)  # works, but not what I want.
puts h.list(a, :inline, " ")  # wacky


I'm looking to create the equivalent of this:

a_a_a_a__b_b_b_b__--Some more text--
__________________--Some more text--
__________________--Some more text--
__________________--Some more text--
__________________--Some more text--



[1]
/usr/lib/ruby/gems/1.8/gems/highline-1.2.0/lib/highline.rb:362:in
`ceil': Infinity (FloatDomainError)
        from
/usr/lib/ruby/gems/1.8/gems/highline-1.2.0/lib/highline.rb:362:in
`list'
        from ./test.rb:19
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-15 17:34
(Received via mailing list)
On Apr 15, 2006, at 10:00 AM, Sy Ali wrote:

> I have not yet worked with anything in the form of
> h = HighLine.new

The list() method is also available inside HighLine's message
strings, using ERB:

 >> require "highline/import"
=> true
 >> say "<%= list(%w{One Two Three}) %>"
One
Two
Three
=> nil

James Edward Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-15 17:50
(Received via mailing list)
On Apr 15, 2006, at 10:19 AM, Sy Ali wrote:

> a << "--Some more text-- --Some more text-- --Some more text-- --Some
> more text-- --Some more text--"

I don't really understand the goal, but perhaps this will give you
ideas...

 >> first_line = a[0..-2].map { |str| str.tr(" ", "_") + "__" }.join
=> "a_a_a_a__b_b_b_b_b__"
 >> result = a.last.split(/\s+(?=-)/).map { |str| "_" *
first_line.length + str }
=> ["____________________--Some more text--", "____________________--
Some more text--", "____________________--Some more text--",
"____________________--Some more text--", "____________________--Some
more text--"]
 >> result.first.sub!(/_+/, first_line)
=> "a_a_a_a__b_b_b_b_b__--Some more text--"
 >> puts result
a_a_a_a__b_b_b_b_b__--Some more text--
____________________--Some more text--
____________________--Some more text--
____________________--Some more text--
____________________--Some more text--
=> nil

James Edward Gray II
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 19:41
(Received via mailing list)
On 4/15/06, James Edward Gray II <james@grayproductions.net> wrote:
> I don't really understand the goal, but perhaps this will give you
> ideas...

This definitely points me in a direction I can work with.

The underscores were there because a lot of people have variable-width
fonts and wouldn't see the columns of text properly.  I think (I hope)
I can excize that part of your example and fiddle around to understand
how things are working.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 20:39
(Received via mailing list)
On 4/15/06, Sy Ali <sy1234@gmail.com> wrote:
> This definitely points me in a direction I can work with.

I'm definitely drifting off topls, but I suppose this is a feature I'd
love to see in HighLine.

I think I understood where you were going.  I cut it apart and made a
simplified version which demonstrates my goal.  I haven't achieved
what I wanted yet.. because I don't know how to take a string and
split it into an array at a certain length number.

I'm thinking in very small units right now.  In the end this ought to
deal with any one of the items in the original array being long enough
to require it wrap on the next line.


require 'desc/sy_helper.rb'

# Can have an arbitrary number and length of items on the left.
# These are a[0..-2]
a = []
a << "foo"
a << "bar"
a << "baz"
a << " "

# This is a.last
a << "Some more text Some more text Some more text Some more text Some
more text Some more text"

# Create the first line of content.
# Turn the array into a string separated by spaces
left = array_to_string(a[0..-2], " ")

# TODO: This should be more universal, and wrap on a certain character
number of the length of the third column.  Preferably to the nearest
whitespace character so that words aren't chopped off.
puts       left        + a.last[ 0..30]
puts " " * left.length + a.last[31..61]
puts " " * left.length + a.last[62..-1]

# =>
# foo bar baz  Some more text Some more text S
#              ome more text Some more text So
#              me more text Some more text
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-15 21:41
(Received via mailing list)
On Apr 15, 2006, at 1:36 PM, Sy Ali wrote:

> On 4/15/06, Sy Ali <sy1234@gmail.com> wrote:
>> This definitely points me in a direction I can work with.
>
> I'm definitely drifting off topls, but I suppose this is a feature I'd
> love to see in HighLine.

I'm not very clear on what feature you are requesting.

If it is word wrap, HighLine already does this (see the wrap
parameter to the constructor).  If you are instead trying to pad list
items, just run through your Array before handing it off, calling
String#ljust/center/rjust.

If I am still way off, try explaining again the feature you are
requesting.  Maybe without the code this time, because I think that
funny Array is confusing me...

James Edward Gray II
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-15 23:07
(Received via mailing list)
On 4/15/06, James Edward Gray II <james@grayproductions.net> wrote:
> I'm not very clear on what feature you are requesting.
>
> If it is word wrap, HighLine already does this (see the wrap
> parameter to the constructor).  If you are instead trying to pad list
> items, just run through your Array before handing it off, calling
> String#ljust/center/rjust.
>
> If I am still way off, try explaining again the feature you are
> requesting.  Maybe without the code this time, because I think that
> funny Array is confusing me...

It feels like HighLine can do this columnizing but I'm not using it
right.

Let's imagine I have an array which has a short string of text and
then a long string of text.

a = ["a short line_", "A long line of text which should wrap"]

Imagine that the second element ( a[1] ) could be very long and would
not display well on my very thin screen / xterm.  Normally, if I
outputted one element after the other it would look like this:

print a[0], a[1]
=>
a short line_A long line of text
which should wrap
(imagine my screen is thin)

The long line of text wraps on the second line, starting at the far
left.  I want it to instead look like this:

a short line_A long line of text
             which should wrap
(imagine my screen is thin)

----
so the left-hand column is:
a short line_

and the right-hand column is
A long line of text
which should wrap
----

I've played and played but I am very green and still don't understand
how to do most things.  Looking at code really helps if it's simple
enough.
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-04-16 00:02
(Received via mailing list)
On 15-Apr-06, at 5:06 PM, Sy Ali wrote:

>> funny Array is confusing me...
> not display well on my very thin screen / xterm.  Normally, if I
>
> which should wrap
> ----

Sounds like you want something similar to a table layout where both
the columns and contents of the cells are managed for you.  I'm not
sure if HighLine is intended to do this...

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-16 00:57
(Received via mailing list)
On Apr 15, 2006, at 5:00 PM, Mike Stok wrote:

> Sounds like you want something similar to a table layout where both
> the columns and contents of the cells are managed for you.  I'm not
> sure if HighLine is intended to do this...

Yeah, that's my feeling.  If you can separate the columns, you could
use HighLine to wrap the text (just pass a StringIO as the output
stream).  Then you could zip() and join() the columns.

I can't decide if it would be scary cool if HighLine did this, or
grossly over-reaching it's scope...  :)

Maybe we should just port Damian's format library from Perl.

James Edward Gray II
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-16 16:56
(Received via mailing list)
On 4/15/06, James Edward Gray II <james@grayproductions.net> wrote:
> I can't decide if it would be scary cool if HighLine did this, or
> grossly over-reaching it's scope...  :)

I had suggested this when I was working on the menu system a few
months ago.  I wanted to add a HighLine table class.  I can't remember
why we didn't.  It may have been that it was beyond the scope, it may
have been that I didn't have the necessary voodoo at the time to do it
nicely, might have been laziness.  Can't remember.

I actually would be willing to work on this, if I could find the time.
 But I'm not sure if that's going to happen or if it's really
something HighLine needs.

Porting the perl lib might be a cheap and easy way out.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-16 20:53
(Received via mailing list)
On Apr 16, 2006, at 9:55 AM, Gregory Brown wrote:

> Porting the perl lib might be a cheap and easy way out.

I just think, if we are going to ask HighLine to do a lot of wicked
formatting, why not just add an uber cool formatting library to the
mix.  I'm not saying it has to be the Perl 6 library, but there are
at least some good ideas there:

http://www.perl.com/pub/a/2004/02/27/exegesis7.html

This seems like it could be a huge win for Ruport as well...  ;)

James Edward Gray II
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-16 21:45
(Received via mailing list)
Although I've made / discovered some of the other little tools I've
needed (as part of learning), the columnizing that I'm describing is
beyond my means right now.  I sure would be interested in seeing a
library ported, and I'd help if I could be useful.

I would have thought that such things already existed.  After all,
people who make forms and such would do this for theall the time.  I
wonder if there's anything in PDF::Writer or one of the other
pdf-generating libraries.

..

As an aside, I was thinking of writing some kind of wrapper to help
with the colour functions in HighLine, so that I cound use strings
like this:

colour_puts "@RBold Red @rred @YBold yellow"

(using your imagination)
Bold Red red Bold yellow

etc.  It doesn't feel very difficult, but it would sure be easier for
me to use colour codes like this then to bust strings up and feed them
to HighLine the way I am right now.

In such markup
rgbcmywg = red, green, blue, cyan, magenta, yellow, white (grey),
green .. and with capitals = bold
@@ = @

Some other convention for background colour and other tags (underline?
blink?) could work as well.. but that would be for down the road.

I figure this is a bit of a stretch for me but would be educational to
try.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-16 22:00
(Received via mailing list)
On Apr 16, 2006, at 2:44 PM, Sy Ali wrote:

> me to use colour codes like this then to bust strings up and feed them
> to HighLine the way I am right now.

Are HighLine's ERB escapes not enough for this?  Can you show an
example where you have to bust a String up?

James Edward Gray II
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-16 23:30
(Received via mailing list)
On 4/16/06, James Edward Gray II <james@grayproductions.net> wrote:
>
> This seems like it could be a huge win for Ruport as well...  ;)

Hmm... this does sound interesting... and could definitely be good for
Ruport too.  Unfortunately for the next 4->5 weeks, I have to do
boring 'real' work at work and school.  After that, I'm all good to go
though, so this might be one of the first things I jump on.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-16 23:33
(Received via mailing list)
On 4/16/06, James Edward Gray II <james@grayproductions.net> wrote:
> >
> > etc.  It doesn't feel very difficult, but it would sure be easier for
> > me to use colour codes like this then to bust strings up and feed them
> > to HighLine the way I am right now.
>
> Are HighLine's ERB escapes not enough for this?  Can you show an
> example where you have to bust a String up?

Hmm... this is really what those ERB escapes are for... so if there is
a problem that they aren't solving, we'll need to consider that...
Still, I think this can be done via ERB.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-17 00:59
(Received via mailing list)
On 4/16/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 4/16/06, James Edward Gray II <james@grayproductions.net> wrote:
> > On Apr 16, 2006, at 2:44 PM, Sy Ali wrote:
> > > colour_puts "@RBold Red @rred @YBold yellow"
> > >
> > Are HighLine's ERB escapes not enough for this?  Can you show an
> > example where you have to bust a String up?
>
> Hmm... this is really what those ERB escapes are for... so if there is
> a problem that they aren't solving, we'll need to consider that...
> Still, I think this can be done via ERB.

I'm going to tinker and see if the need is indeed there.  I may do it
just for the learning experience or because I want to make a
translator back and forth for mud markup.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-17 02:49
(Received via mailing list)
On Apr 16, 2006, at 4:28 PM, Gregory Brown wrote:

>> http://www.perl.com/pub/a/2004/02/27/exegesis7.html
>>
>> This seems like it could be a huge win for Ruport as well...  ;)
>
> Hmm... this does sound interesting... and could definitely be good for
> Ruport too.  Unfortunately for the next 4->5 weeks, I have to do
> boring 'real' work at work and school.  After that, I'm all good to go
> though, so this might be one of the first things I jump on.

I checked the source.  It's 1,300+ lines of code.  That won't be an
easy port.  I still say it could be nice though...

James Edward Gray II
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-17 03:07
(Received via mailing list)
On 4/16/06, James Edward Gray II <james@grayproductions.net> wrote:
> >> at least some good ideas there:
> I checked the source.  It's 1,300+ lines of code.  That won't be an
> easy port.  I still say it could be nice though...

I've been looking to get at least my eyes back on some Perl.  ( Though
I can't believe I just said that ;) )

Give me a month, and if you want to get your hands dirty, you and I
can take on yet another one of those jobs that sound like they are
made for us ;)

So... ruby community... do you want Perl6::Form ?
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-04-17 05:59
(Received via mailing list)
Gregory Brown wrote:
> So... ruby community... do you want Perl6::Form ?

Yes.

Cheers,
Dave
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-04-17 07:00
(Received via mailing list)
On 4/16/06, Sy Ali <sy1234@gmail.com> wrote:
> As an aside, I was thinking of writing some kind of wrapper to help
> with the colour functions in HighLine, so that I cound use strings
> like this:

Just to round off my comment in this thread...

I did take some time to play with this colour markup concept, and I
found an inventive solution.  Unfortunately HighLine wasn't working at
home so I got it working with ansicolor[1].

http://jrandomhacker.info/TRN_-_Projects/colourcodes

Example of use:
colour_codes("@rred     @Rbold red\n")
colour_codes("@ggreen   @Gbold green\n")
colour_codes("@bblue    @Bbold blue\n")
colour_codes("@ccyan    @Cbold cyan\n")
colour_codes("@mmagenta @Mbold magenta\n")
colour_codes("@yyellow  @Ybold yellow\n")
colour_codes("@wwhite   @Wbold white\n")
colour_codes("@w") # A single colour code can be passed.
colour_codes("@@an at symbol @xan invalid code can be optionally
displayed\n")


[1] http://term-ansicolor.rubyforge.org/
Bb6ecee0238ef2461bef3416722b35c5?d=identicon&s=25 pat eyler (Guest)
on 2006-04-17 15:35
(Received via mailing list)
On 4/16/06, James Edward Gray II <james@grayproductions.net> wrote:
> >> at least some good ideas there:
> I checked the source.  It's 1,300+ lines of code.  That won't be an
> easy port.  I still say it could be nice though...


One of the hackers in the BYU Ruby Users Group is already working on
this.  I'm cross-posting to try to get the conversation started.


>
> James Edward Gray II
>
>
>


--
thanks,
-pate
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2006-06-07 16:01
(Received via mailing list)
ruby-talk@ruby-lang.org, Gregory Brown <gregory.t.brown@gmail.com>,
"James Edward Gray II" <james@grayproductions.net>,

On 4/16/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> >
>  But I'm not sure if that's going to happen or if it's really
> something HighLine needs.
>
> Porting the perl lib might be a cheap and easy way out.

Just a quick comment on this older thread.  I ended up getting
columnization working well, thanks to Text::Reform[1] .. if fuller
columnization support for Highline is still on the radar, perhaps
lifting the functionality from them would be possible?

[1] http://rubyforge.org/frs/?group_id=294
Their test cases demonstrate the functionality pretty well..
This topic is locked and can not be replied to.