[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Feature] For fold, Add 'Avoid breaking on Char' flag
From: |
Pádraig Brady |
Subject: |
Re: [Feature] For fold, Add 'Avoid breaking on Char' flag |
Date: |
Mon, 11 May 2015 15:59:16 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 |
On 09/05/15 18:16, RabidCicada wrote:
> Function:
> Fold on width boundary avoiding breaking (don't break) on the target char by
> folding one char early or one char late.
>
> Syntax:
> Example: 'fold -d\\- -w30'
>
> Flag Usage:
> -d<chartoavoid><minus sign for folding early plus sign for folding late>
> default behavior is to fold early (break lines to less than width)
>
>
> Motivating Example:
> I was putting together a shell script to break a long string into pieces for
> a header file. In particular I was taking a human formatted xmlfile and
> wanting to split it into multiple lines and adhere to the typical 80 char
> limit. The problem was that there are escaped quotes within the xml. Fold,
> in general, seemed like the appropriate tool, but I couldn't find a
> reasonable way to split at 80 but avoid breaking directly following the '\'.
>
> Bad split string result:
> "tart</property><property name=\"margin_bottom\">6</property><property name=\"
> ""hexpand\">True</property><property name=\"label\">[title]</property><attrib"
> This results in the final quote being escaped and messing up the header file.
>
> Good split string result:
> "tart</property><property name=\"margin_bottom\">6</property><property name="
> "\"hexpand\">True</property><property name=\"label\">[title]</property><attri"
>
> It splits the string one char early to avoid the '\' wreaking havoc.
>
>
> My final shell script, using my custom modified fold, looks like this:
> sed 's/^[[:space:]]*//' $1 | sed 's/[[:space:]]*$//' | sed -z 's/\n//g' | sed
> 's/"/\\"/g' | fold -w76 -d\\ | sed 's/^/ "/g' | sed 's/$/"/g' > $2
>
> It takes the regular xml file, chomps off extraneous whitespace (seds 1 & 2),
> then removes all newlines to condense into single blob(sed 3), then escapes
> all inline quotes (sed 4), then folds using my special flag to avoid the
> escape characters, then tacks on the opening and closing quotes and spaces
> for the header file string (seds 5 & 6).
>
> I looked all around and I couldn't find a tool that could fold/split, but
> avoid splitting on chosen char. I thought I would find one...but alas...none.
>
> I have this implemented and usage messages updated but don't have other
> documentation and tasking in order yet. If this would be an accepted
> feature, I would be happy to finalize all the housework to submit patches
> with full documentation, texi, etc. I certainly see it being useful for
> other similar tasks where a key char should not be at the fold barrier.
>
> I looked at the rejected features idea and don't see ANY listed for fold:).
> Maybe it can get some love? Anyone support or object to the idea?
Thanks for taking the time to read the rejected ideas.
In general, given the overlap between pr, fmt, and fold
there is a high barrier to adding new options.
In your case you could change the processing steps
after the fold to:
... | fold in -w76 | sed 's/\\$//; s/^"/\\"/; s/^/"/; s/$/"/'
That would bump lines slightly longer, and assumes
no other \ in the data, but you get the idea.
cheers,
Pádraig.