0

In Perl6::Form, is it possible to wrap text at any character not just spaces?

| Item                                    |
| {[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[} |
  @phrase,

Above formatting generates,

| Item                                    |
| Word1                                   |
| Super_________long________________word1 |

I want it to break the word itself. Is it possible to do that? Like below

| Item                                    |
| Word1 Super_________long_______________ |
| _word1                                  |

Also, in this example. Why isn't blah2 or 1.3 or 101.0 not aligned with with its corresponding values of the table 12

my @name=();
my @score=();
my @time=();
my @normalized=();

push(@name,'blah1');
push(@score,'115555555');
push(@time,1.1);
push(@normalized,100);
push(@name,'blah2');
push(@score,'12');
push(@time,1.3);
push(@normalized,101);


print form
    '-------------------------------------------',
    'Name             Score   Time  | Normalized',
    '-------------------------------------------',
    '{[[[[[[[[[[[[}   {[[[}  {II}  |  {]]].[[} ',
     \@name,          \@score,\@time,  \@normalized; 



-------------------------------------------
Name             Score   Time  | Normalized
-------------------------------------------
blah1            1155-  1.1   |   100.0
blah2            55555  1.3   |   101.0
                 12           |

From documentation, I learned I can use layout and break options to accomplish the behavior I was looking for

print form
{layout=>"tabular"},
{break=>\&break_width},

But the function posted in the doc has compile issues:

sub break_width {
    my ($data_ref, $width, $ws) = @_;
    for ($$data_ref) {
        # Treat any squeezed or vertical whitespace as a single character
        # (since they'll subsequently be squeezed to a single space)
        my $single_char = qr{ $ws | [\n\r]+ | . }
 
        # Give up if there are no more characters to grab...
        return ("", 0) unless m/\G (single_char{1,$width}) /gcx;
 
        # Squeeze the resultant substring...
        (my $result = $1) =~ s/ $ws | [\n\r] / /gx;
 
        # Check for any more data still to come...
        my $more = m/\G (?= .* \S) /gcx;
 
        # Return the squeezed substring and the "more" indicator...
        return ($result, $more);
    }
}

After fixing basic issues with this function, I am still hitting uninitialized $ws. Clearly this is not tested code. Am I missing something?

Jean
  • 21,665
  • 24
  • 69
  • 119
  • Sorry Jean, I thought you used [tag:raku] since you use `Perl6::Form` but I see now that it actually works in perl5 too. Please remove the [tag:raku] tag if you actually use perl5. – Ted Lyngmo Aug 28 '23 at 19:41
  • You don't use Perl6::Form in raku because `form` is already there. – brian d foy Aug 28 '23 at 20:31
  • You could wrap the text according to your own criteria before passing it to `form` – ikegami Aug 28 '23 at 21:24
  • The exact behavior as per `break_width` defined in the doc was what I was looking for, but I am hitting basic compile issues with it. Looks like the document code is not validated. – Jean Aug 29 '23 at 16:12

1 Answers1

1

From the docs:

Line-breaking

Whenever a field is passed more data than it can accommodate in a single line, form is forced to "break" that data somewhere.

If the field in question is W columns wide, form first squeezes any whitespace (as specified by the user's ws option) and then looks at the next W columns of the string.

form's breaking algorithm then searches for a newline, a carriage return, any other whitespace character, or a hyphen. If it finds a newline or carriage return within the first W columns, it immediately breaks the data string at that point. Otherwise it locates the last whitespace or hyphen in the first W columns and breaks the string immediately after that space or hyphen. If it can't find anywhere suitable to break the string, it breaks it at the (W-1)th column and appends a hyphen.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Thanks for the pointer. I delved through more documentation. Updated same in question above. – Jean Aug 29 '23 at 15:36