Fully justified/center justified output

Discussion in 'Ruby' started by Ben Thomas, Nov 15, 2006.

  1. Ben Thomas

    Ben Thomas Guest

    Hi, how do I go about fully justifying/center justifying output text in
    ruby?

    I'm guessing I have to use sprintf but I can't seem to get it working
    properly, not sure of the correct "%" characters (don't know the proper
    name for them).

    Thanks,
    Ben
    Ben Thomas, Nov 15, 2006
    #1
    1. Advertising

  2. > Hi, how do I go about fully justifying/center justifying output text in ruby?
    >
    > I'm guessing I have to use sprintf but I can't seem to get it working
    > properly, not sure of the correct "%" characters (don't know the proper name
    > for them).


    http://corelib.rubyonrails.org/classes/String.html#M001536

    str.center(integer, padstr) => new_str

    If integer is greater than the length of str, returns a new String of
    length integer with str centered and padded with padstr; otherwise,
    returns str.

    "hello".center(4) #=> "hello"
    "hello".center(20) #=> " hello "
    "hello".center(20, '123') #=> "1231231hello12312312"
    Philip Hallstrom, Nov 15, 2006
    #2
    1. Advertising

  3. Philip Hallstrom wrote:
    >> Hi, how do I go about fully justifying/center justifying output text
    >> in ruby?
    >>
    >> I'm guessing I have to use sprintf but I can't seem to get it working
    >> properly, not sure of the correct "%" characters (don't know the
    >> proper name for them).

    >
    > http://corelib.rubyonrails.org/classes/String.html#M001536
    >
    > str.center(integer, padstr) => new_str
    >
    > If integer is greater than the length of str, returns a new String of
    > length integer with str centered and padded with padstr; otherwise,
    > returns str.
    >
    > "hello".center(4) #=> "hello"
    > "hello".center(20) #=> " hello "
    > "hello".center(20, '123') #=> "1231231hello12312312"
    >
    >

    If you're printing to terminal, you'll need to know the terminal width
    before use can use String#center. This is simple in Windows because
    cmd.exe terminal is fixed-width. In Linux and OSX this is more
    complicated. The easiest way I know is this:

    require "curses"

    width = Curses::cols
    "hello".center(width)

    It's not totally clear from your post if this is what you're trying to
    do, but if it is I hope this works you.
    Juozas Gaigalas, Nov 15, 2006
    #3
  4. Ben Thomas

    Ben Thomas Guest

    Juozas Gaigalas wrote:
    > Philip Hallstrom wrote:
    >>> Hi, how do I go about fully justifying/center justifying output text
    >>> in ruby?
    >>>
    >>> I'm guessing I have to use sprintf but I can't seem to get it working
    >>> properly, not sure of the correct "%" characters (don't know the
    >>> proper name for them).

    >>
    >> http://corelib.rubyonrails.org/classes/String.html#M001536
    >>
    >> str.center(integer, padstr) => new_str
    >>
    >> If integer is greater than the length of str, returns a new String of
    >> length integer with str centered and padded with padstr; otherwise,
    >> returns str.
    >>
    >> "hello".center(4) #=> "hello"
    >> "hello".center(20) #=> " hello "
    >> "hello".center(20, '123') #=> "1231231hello12312312"
    >>
    >>

    > If you're printing to terminal, you'll need to know the terminal width
    > before use can use String#center. This is simple in Windows because
    > cmd.exe terminal is fixed-width. In Linux and OSX this is more
    > complicated. The easiest way I know is this:
    >
    > require "curses"
    >
    > width = Curses::cols
    > "hello".center(width)
    >
    > It's not totally clear from your post if this is what you're trying to
    > do, but if it is I hope this works you.
    >
    >


    Yeah I think that's what I want from what I can see.

    Can't get it to work for what I want though because the data is in an
    array. Tried converting it to a string but can't work that out either.
    Not been doing ruby long as you might have guessed!

    Thanks,
    Ben.
    Ben Thomas, Nov 15, 2006
    #4
  5. On 11/16/06, Juozas Gaigalas <> wrote:
    > Philip Hallstrom wrote:
    > >> Hi, how do I go about fully justifying/center justifying output text
    > >> in ruby?
    > >>
    > >> I'm guessing I have to use sprintf but I can't seem to get it working
    > >> properly, not sure of the correct "%" characters (don't know the
    > >> proper name for them).

    > >
    > > http://corelib.rubyonrails.org/classes/String.html#M001536
    > >
    > > str.center(integer, padstr) => new_str
    > >
    > > If integer is greater than the length of str, returns a new String of
    > > length integer with str centered and padded with padstr; otherwise,
    > > returns str.
    > >
    > > "hello".center(4) #=> "hello"
    > > "hello".center(20) #=> " hello "
    > > "hello".center(20, '123') #=> "1231231hello12312312"
    > >
    > >

    > If you're printing to terminal, you'll need to know the terminal width
    > before use can use String#center. This is simple in Windows because
    > cmd.exe terminal is fixed-width. In Linux and OSX this is more
    > complicated. The easiest way I know is this:
    >
    > require "curses"
    >
    > width = Curses::cols
    > "hello".center(width)
    >
    > It's not totally clear from your post if this is what you're trying to
    > do, but if it is I hope this works you.


    To do that without curses:

    h, w = `stty size`.split.map{|e| e.to_i}
    # [70, 55]
    'foo'.center(w)
    # " foo "
    Michael Fellinger, Nov 15, 2006
    #5
  6. On 11/16/06, Ben Thomas <> wrote:
    > Juozas Gaigalas wrote:
    > > Philip Hallstrom wrote:
    > >>> Hi, how do I go about fully justifying/center justifying output text
    > >>> in ruby?
    > >>>
    > >>> I'm guessing I have to use sprintf but I can't seem to get it working
    > >>> properly, not sure of the correct "%" characters (don't know the
    > >>> proper name for them).
    > >>
    > >> http://corelib.rubyonrails.org/classes/String.html#M001536
    > >>
    > >> str.center(integer, padstr) => new_str
    > >>
    > >> If integer is greater than the length of str, returns a new String of
    > >> length integer with str centered and padded with padstr; otherwise,
    > >> returns str.
    > >>
    > >> "hello".center(4) #=> "hello"
    > >> "hello".center(20) #=> " hello "
    > >> "hello".center(20, '123') #=> "1231231hello12312312"
    > >>
    > >>

    > > If you're printing to terminal, you'll need to know the terminal width
    > > before use can use String#center. This is simple in Windows because
    > > cmd.exe terminal is fixed-width. In Linux and OSX this is more
    > > complicated. The easiest way I know is this:
    > >
    > > require "curses"
    > >
    > > width = Curses::cols
    > > "hello".center(width)
    > >
    > > It's not totally clear from your post if this is what you're trying to
    > > do, but if it is I hope this works you.
    > >
    > >

    >
    > Yeah I think that's what I want from what I can see.
    >
    > Can't get it to work for what I want though because the data is in an
    > array. Tried converting it to a string but can't work that out either.
    > Not been doing ruby long as you might have guessed!


    read the documentation of Array... especially #join

    > Thanks,
    > Ben.
    Michael Fellinger, Nov 15, 2006
    #6
  7. On 11/15/06, Ben Thomas <> wrote:
    > Hi, how do I go about fully justifying/center justifying output text in
    > ruby?
    >
    > I'm guessing I have to use sprintf but I can't seem to get it working
    > properly, not sure of the correct "%" characters (don't know the proper
    > name for them).


    Check out Text::Format. It will even work with Text::Hyphen to
    properly hyphenate your text.

    http://rubyforge.org/projects/text-format/

    I think that's right. Like my own home number, I don't tend to visit
    my own project pages that often. ;)

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
    Austin Ziegler, Nov 16, 2006
    #7
  8. Ben Thomas

    Jan Svitok Guest

    On 11/15/06, Juozas Gaigalas <> wrote:
    > This is simple in Windows because cmd.exe terminal is fixed-width.


    Not always. You can manually specify different size (at least in XP+
    and I guess in 2000 as well).
    Jan Svitok, Nov 16, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    7
    Views:
    5,661
    Joe Smith
    Jul 22, 2004
  2. Mike
    Replies:
    5
    Views:
    2,760
    Adam Maass
    Jul 1, 2003
  3. Duncan
    Replies:
    20
    Views:
    782
  4. Replies:
    5
    Views:
    1,112
    Ian Rastall
    Jun 29, 2005
  5. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Left justified or center justified under XSL:FO

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Feb 9, 2007, in forum: XML
    Replies:
    0
    Views:
    492
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Feb 9, 2007
Loading...

Share This Page