in reply to Special formatting of error messages

Sounds like something that formats would be useful for. Hmm... that's the first time I've ever said that. I've probably been missing out on a lot of fun.

Without formats, you just have to do a two-pass approach. First, determine the length of the widest string being output. length is handy for that. Second, each string needs to be padded with ( $length_max - $length_current ) / 2 leading space characters, and with $length_max - $new_current_length space characters trailing.


Dave

Replies are listed 'Best First'.
Re^2: Special formatting of error messages
by Ronnie (Scribe) on Feb 02, 2005 at 09:05 UTC
    Yes that was the original idea that I had but ..... (Now this may be the daftest part of the question) how is the space padding applied? Cheers, Ronnie PS I may be missing something very obvious here but for the life of me I can't see it!

      I kind of described it already, but here's one brainstorm that is untested:

      use strict; use warnings; my $string = <<HERE; A long time ago in a galaxy far far away a great adventure took place. HERE sub textbox { my( @lines ) = split /\n/, $_[0]; chomp @lines; my $longest = 0; foreach my $line ( @lines ) { my $size = length $line; $longest = $size if $size > $longest; } foreach my $line ( @lines ) { my $prepad = ' ' x ( ( $longest - length( $line ) ) / 2 ); $line = $prepad . $line; $line .= ' ' x ( $longest - length( $line ) ); $line = '* ' . $line . ' *'; } my $outline = '*' x ( $longest + 4 ); push @lines, $outline; unshift @lines, $outline; return join( "\n", @lines ) . "\n"; } print textbox( $string );

      This could use some streamlining, but I left it reasonably verbose so that it would be clear what's going on. Enjoy!


      Dave