HighLine - examples for using "list"


#1

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?


#2

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 J. 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 S. removed_email_address@domain.invalid
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.


#3

On 4/15/06, Mike S. removed_email_address@domain.invalid 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 =
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.


#4

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 T. Three}) %>”
One
Two
Three
=> nil

James Edward G. II


#5

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:inlist’
from ./test.rb:19


#6

On 4/15/06, James Edward G. II removed_email_address@domain.invalid 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.


#7

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 G. II


#8

On Apr 15, 2006, at 1:36 PM, Sy Ali wrote:

On 4/15/06, Sy Ali removed_email_address@domain.invalid 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 G. II


#9

On 4/15/06, Sy Ali removed_email_address@domain.invalid 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


#10

On 4/15/06, James Edward G. II removed_email_address@domain.invalid 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.


#11

On Apr 15, 2006, at 5:00 PM, Mike S. 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… :slight_smile:

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

James Edward G. II


#12

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 S. removed_email_address@domain.invalid
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.


#13

On Apr 16, 2006, at 9:55 AM, Gregory B. 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… :wink:

James Edward G. II


#14

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.


#15

On 4/15/06, James Edward G. II removed_email_address@domain.invalid wrote:

I can’t decide if it would be scary cool if HighLine did this, or
grossly over-reaching it’s scope… :slight_smile:

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.


#16

On 4/16/06, James Edward G. II removed_email_address@domain.invalid 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.


#17

On 4/16/06, James Edward G. II removed_email_address@domain.invalid wrote:

This seems like it could be a huge win for Ruport as well… :wink:

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.


#18

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 G. II


#19

On 4/16/06, Gregory B. removed_email_address@domain.invalid wrote:

On 4/16/06, James Edward G. II removed_email_address@domain.invalid 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.


#20

On 4/16/06, James Edward G. II removed_email_address@domain.invalid 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 :wink: )

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 :wink:

So… ruby community… do you want Perl6::Form ?