=head1 NAME

CFX5::Output::OutFile - output routines for a solver-style out file

=head1 SYNOPSIS

  use CFX5::Output;

=head1 DESCRIPTION

The C<CFX5::Output::OutFile> class implements copying of errors to a
file, formatting them in a manner compatible with the output from the
ANSYS CFX solver.  It is based on the C<CFX5::Output::File> class.

=cut

package CFX5::Output::OutFile;
@ISA=qw(CFX5::Output::File);
use strict;

use CFX5::Output::File;

use CFX5::Output qw(:all);
use CFX5::Output::Utils 'wraptext';

=head2 STANDARD METHODS

=head3 text_to_show

This is the routine we override.  It takes the same arguments as the
version in CFX5::Output::Base, but formats the text to be inside a box
if there is a title specified.  If there is no title, the output is
the same.

=cut

sub text_to_show {
  my $self=shift;
  my $title=shift;

  if (!defined $title) {
    my $t=$self->SUPER::text_to_show($title,@_);
    #return $t;
    # we indent all text by a single space.
    $t =~ s!\n!\n !g;
    $t=' '.substr($t,0,-1);
    return $t;
  }

  my $text=join('',@_);

  my $width=72;
  my $out='';

  $out .= "\n +" . "-" x ($width-4) . "+\n";

  if (defined $title and $title ne '') {
    my $wtitle=wraptext($width-4, $title);
    for my $line (split(/\n/,$wtitle)) {
      next unless $line =~ m!\S!;
      my $pad1=int(($width-4-length($line))/2);
      my $pad2=($width-4-$pad1-length($line));
      $out .= " |" . " " x $pad1 . $line . " " x $pad2 . "|\n";
    }
    $out .= " |" . " " x ($width-4) . "|\n";
  }

  # get the text and -- if requested -- a stack trace, and wrap them
  # to fit in the requisite number of columns.
  my $wtext=$text;
  $wtext .= "\n\n" . $self->stack_trace() if $self->{settings}{stack};
  $wtext = wraptext($width-6,$wtext);

  # now fit a box around them
  for my $line (split(/\n/,$wtext)) {
    $line =~ s!\s+$!!g;
    my $pad=$width-5-length($line);
    $out .= " | " . $line . " " x $pad . "|\n";
  }

  $out .= " +" . "-" x ($width-4) . "+\n\n";  
  
  $out;
}

sub wrapwidth {
  return 71;
}

1;
