App-RecordStream

 view release on metacpan or  search on metacpan

lib/App/RecordStream/Operation/totable.pm  view on Meta::CPAN

  $this->{'FIELDS'} = $fields;

  if(!$no_header) {
    $this->push_line(
      $this->format_row(
        $fields,
        \%widths,
        sub { return $_[1]; },
        ""
      )
    );

    if ( ! $this->{'SPREADSHEET'} ) {
      $this->push_line(
        $this->format_row(
          $fields,
          \%widths,
          sub { return ("-" x $widths{$_[1]}); },
          ""
        )
      );
    }
  }

  my %last = map { $_ => "" } (keys(%widths));
  foreach my $record (@$records) {
    $this->push_line(
      $this->format_row(
        $fields,
        \%widths,
        \&format_field,
        [$record, \%last]
      )
    );
  }
}

sub format_field
{
  my ($this, $field, $thunk) = @_;
  my ($r, $lastr) = @$thunk;

  my $value = ${$r->guess_key_from_spec($field)};
  $value = '' if ( ! defined $value );

  if ( ref($value) )
  {
    $value = App::RecordStream::OutputStream::hashref_string($value);
  }

  if($this->{'CLEAR'})
  {
    if($value eq $lastr->{$field})
    {
      # This column matches the "last" value so we clear the cell.
      $value = "";
    }
    else
    {
      # This column did not match so we do not clear the cell.  We also
      # invalidate all "last" field values to the right of this column.
      my $startInvalidating = 0;
      for(@{$this->{'FIELDS'}})
      {
        if($_ eq $field)
        {
          $startInvalidating = 1;
        }
        elsif($startInvalidating)
        {
          $lastr->{$_} = "";
        }
      }
      $lastr->{$field} = $value;
    }
  }

  return $value;
}


sub format_row {
  my ($this, $fieldsr, $widthsr, $format_fieldr, $thunk) = @_;

  my $first = 1;
  my $row_string = "";

  foreach my $field (@$fieldsr) {
    my $field_string = $format_fieldr->($this, $field, $thunk);

    unless ( defined $field_string ) {
      $field_string = '';
    }

    if ( (! $this->{'SPREADSHEET'}) &&
      (length($field_string) < $widthsr->{$field})) {

      $field_string .= " " x ($widthsr->{$field} - length($field_string));
    }

    if($first) {
      $first = 0;
    }
    else {
      $row_string .= ($this->{'SPREADSHEET'}) ? $this->{'DELIMITER'} : "   ";
    }

    $row_string .= $field_string;
  }

  return $row_string;
}

# Max helper function
sub max {
  my $max = shift;

  foreach my $value (@_) {
    if($value > $max) {
      $max = $value;
    }



( run in 0.730 second using v1.01-cache-2.11-cpan-e1769b4cff6 )