[nSLUG] Any Perl Experience around?

Joel Maxuel j.maxuel at gmail.com
Thu Jun 22 00:39:39 ADT 2017


Thank you Robert and Hatem,

I am getting closer.  There was still a couple bugs that long names do not
truncate, and that colour-ified would (at least in theory) be a few
characters shorter when truncated (given the hidden escaped characters)

I managed to truncate the colour-ified ones by making an adjustment for
multiple char's between the truncate and the colour off escape:

ls -F --color | perl -lne 's/(.{24}).*(\x1b\[0m)/$1...$2/s ; print' |
column -x

I then expanded to:

ls -F --color | perl -lne 's/(\x1b\[0.{4}m)(.{24}).*(\x1b\[0m)/$1$2...$3/s
; print' | column -x

Problem being, match one and three are not optional (so if not in colour,
no replace occours).  I tried adding question marks to make them optional,
to no avail.

Packing it in for now:

ls -F --color | perl -lne 'if (<STDIN> =~ /\e/) {
s/(\e\[.{5}m)(.{18,24}).*(\e\[0m.*)/$1$2$3.../s ; print; } else {
s/(.{24}).*/$1... /s ; print; }' | column -x

The remaining issue are filenames that switch to turn colour off in the
truncate zone.  The (.{18,24}) seems not to favour less than 24 chars when
needed.



--
Cheers,
Joel Maxuel

"One should strive to achieve, not sit in bitter regret."
 - Ronan Harris / Mark Jackson

On Wed, Jun 21, 2017 at 9:54 PM, Hatem Nassrat <hnassrat at gmail.com> wrote:

> Let me take a stab at explaining the regex ...
>
> . Means any Charachter
> {24} means repeated 24 times
> Surrounding paranthesis means capture that pattern (since it is the first
> it can be accessed with $1)
> .* Means followed by any number of characters
>
> This is replaced with $1 (ie the first 24 characters) then three ...
>
> The switches to perl -lne mean something, not sure off the top, but I
> think -e is for extending regex expressions. The rest may have something to
> do with processing escape characters, not sure.
>
> This is all passed to the column command, which splits on whitespace by
> default and prints the output into tabular columns based on the width of
> the display.
>
> Without running this, my guess is the colors are adding a few characters
> which are within the 24 characters captured and then are now whitespace
> which are being mangled by the column command.
>
> Hope this helps,
>
> -- Hatem
>
>
> On Jun 21, 2017 7:30 PM, "Joel Maxuel" <j.maxuel at gmail.com> wrote:
>
> Hi all,
>
> I encountered this example script in a StackExchange question[1]:
>
> https://github.com/atav32/dotfiles/blob/master/bash_alias#L46
>
> What it does is truncates the results (by file/directory) of an ls
> statement to organize in predictable columns.
>
> It was not long before I thought, "why not have colour?"
>
> So I made a quick addition, and regretted it the rest of the night:
>
> ls -F --color | perl -lne 's/(.{24}).*/$1... /s ; print' | column -x
>
> Why so?  Colour is made up of additional/escape characters which the
> close-out gets truncated and can leave symbols on-screen.  So I started to
> analyze the enclosed Perl.  I see that a substitution is used (find > n
> chars, replace with truncated version + ".... "), but I don't understand
> the inner workings.
>
> Essentially, looks like I want to trim a result if it is n characters wide
> and no \e char to be found, or if with an \e, trim within it (which looks
> to be 8 extra chars to the left and 4 to the right).
>
> This may be a regex thing.
>
> Any ideas?  Thanks.
>
> [1] https://unix.stackexchange.com/questions/112335/can-i-trunca
> te-long-file-names-in-ls-listing
>
> --
> Cheers,
> Joel Maxuel
>
> "One should strive to achieve, not sit in bitter regret."
>  - Ronan Harris / Mark Jackson
>
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
>
>
>
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20170622/bc8ddd00/attachment.html>


More information about the nSLUG mailing list