=head1 NAME

CFX5::Output::Array - output routines for an in-memory array

=head1 SYNOPSIS

  use CFX5::Output;

=head1 DESCRIPTION

The C<CFX5::Output::Array> class implements copying of errors to an
array in memory, presumably for later access.

=cut

package CFX5::Output::Array;
@ISA=qw(CFX5::Output::Base);
use strict;

use CFX5::Output::Base;

use CFX5::Output qw(:all);

=head2 STANDARD METHODS

=head3 init

=cut

sub init {
  my $self=shift;

  $self->{__PACKAGE__ . '::output'} = [];

  unless (exists $self->{settings}) {
    # The default on/off settings
    #    1 => display
    #    0 => do not display
    $self->{settings}=
      {
       'internal' => 1,
       'abort'    => 1,
       'error'    => 1,
       'warn'     => 1,
       'info'     => 1,
       'moreinfo' => 0,
       'trace'    => 0,
       'debug'    => 0,
       'stack'    => 0,
       'print'    => 1,
      };
  }

  1;
}

=head3 output

This version of the C<output> function uses the on/off settings from
C<set_verbosity> to decide whether to output a given message or not.

=cut

sub output {
  my $self=shift;
  my $type=shift;
  my $severity=shift;
  my $title=shift;
  my $text=join('',@_);

  return unless $self->{settings}{$type};

  push @{$self->{__PACKAGE__ . '::output'}},
    { type => $type,
      severity => $severity,
      title => $title,
      text => $text };

  return 1;
}

=head2 CUSTOM METHODS

=head3 as_text

  my @text=$outarray->as_text();

Formats the current contents and returns a list of strings which can
just be printed, or have other things done to them.

=cut

sub as_text {
  my $self=shift;
  my @rv;

  for my $o (@{$self->{__PACKAGE__ . '::output'}}) {
    push @rv, $self->text_to_show($o->{title}, $o->{text});
  }

  @rv;
}

=head3 as_text

  my @text=$outarray->in_box();

Formats the current contents and returns a list of strings which can
just be printed, or have other things done to them.  The output is
contained in a box suitable for inserting into the solver output file,
containing all the errors (if any!) collected together.

=cut

sub in_box {
  my $self=shift;

  my $width=72;

  my $oldwidth = $self->wrapwidth();
  $self->set_wrapwidth(-($width-6));

  my @rv;
  push @rv, " +" . "-" x ($width-4) . "+\n";
  for my $o (@{$self->{__PACKAGE__ . '::output'}}) {
    my $t=$self->text_to_show($o->{title}, $o->{text});
    my @t=split("\n\r*", $t);
    for my $l (@t) {
      $l .= " " x ($width-6-length($l));
      push @rv, " | $l |\n";
    }
  }
  push @rv, " +" . "-" x ($width-4) . "+\n";

  $self->set_wrapwidth($oldwidth);

  return @rv;
}

1;
