Skip Menu |

This queue is for tickets about the Perl6-Form CPAN distribution.

Report information
The Basics
Id: 80014
Status: open
Priority: 0/
Queue: Perl6-Form

People
Owner: Nobody in particular
Requestors: taggart [...] debian.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Perl6::Form ellipsis
Date: Thu, 04 Oct 2012 15:36:12 -0700
To: bug-Perl6-Form [...] rt.cpan.org
From: Matt Taggart <taggart [...] debian.org>
Hi, I am switching from using perl5's format to Perl6::Form and one thing I haven't figured out how to do is truncate a field like format's '...' ellipsis field definition. Is there a way to do this with form? Thanks, -- Matt Taggart taggart@debian.org
Subject: Re: [rt.cpan.org #80014] Perl6::Form ellipsis
Date: Fri, 5 Oct 2012 10:56:25 +0200
To: bug-Perl6-Form [...] rt.cpan.org
From: Damian Conway <damian [...] conway.org>
Hi Mike, You asked: Show quoted text
> I am switching from using perl5's format to Perl6::Form and one thing I > haven't figured out how to do is truncate a field like format's '...' > ellipsis field definition. Is there a way to do this with form?
Yes, but it's not built-in. Perl6::Form has an extension mechanism that makes adding new fields easy (for sufficiently hard values of "easy"). The appended code demonstrates how to add "ellipsis breaking". Note that the break_with_ellipsis() subroutine and the 'use Perl6::Form {field...' code could be placed in a separate module, which you could then just load when needed to enable the new feature. Hope this helps, Damian -----cut----------cut----------cut----------cut----------cut----------cut----------cut----------cut----- use 5.010; use warnings; use Perl6::Form; sub break_with_ellipsis { my ($breaker) = @_; # This generates the replacement line-breaking subroutine... return sub { my ($str_ref, $width, $ws) = @_; # Remember where we started this broken line... my $start_pos = pos $$str_ref; # Break it with the original breaker.... my ($nextline, $more) = $breaker->($str_ref, $width, $ws); # If not at end-of-string... if ($more) { # If there's no room for the ellipsis... if (length $nextline > $width-3) { # Reset line to be (re)broken... pos $$str_ref = $start_pos; # Rebreak it with room for ellipsis... ($nextline, $more) = $breaker->($str_ref, $width-3, $ws); } # Add the ellipsis... $nextline .= '...'; } return ($nextline, $more); } } use Perl6::Form { # Specify a new kind of field... field => [ # Match fields with trailing ellipses... qr/ [{] [^}]* \.\.\. [}] /x # And do this with them... => sub { my ($match, $opts) = @_; # Temporarily wrap breaking algorithm with ellipsis appender... $opts->{break} = break_with_ellipsis( $opts->{break} ); # Rewrite format (removing ellipses from it)... my $fieldtype = substr($match,1,-4); return '{' . $fieldtype . substr($fieldtype,-1) x 3 . '}'; } ] }; # Test it... my $text = 'Now is the winter of our discontent made glorious summer by this son of York'; print form '|---------------------------|', ' {<<<<<<<<<<<<<<<<<<<<<<...}', $text, ' {VVVVVVVVVVVVVVVVVVVVVVVVV}', '|---------------------------|';