sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
48.8k stars 1.23k forks source link

Add `bat --wrap word` #3079

Open oficsu opened 2 weeks ago

oficsu commented 2 weeks ago

The current --wrap character behavior

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| bat --decorations=never       \
    --paging=never             \
    --language bash           \
    --theme Nord             \
    --color=always          \
    --terminal-width 11    \
    --wrap character      \
    -

image

Suggestion

I suggest a new word mode for the --wrap option, which will split lines only by spaces (except in cases where a word is too long for a single line). These spaces should then be stripped

An example of how it should look with --wrap word:

image

einfachIrgendwer0815 commented 2 weeks ago

Issue #2751 is related and this comment also provides a shell workaround.

oficsu commented 2 weeks ago

@einfachIrgendwer0815, unfortunately, this workaround will break syntax highlighting, which goes completely against the initial purpose of using bat for syntax highlighting:

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| bat -p                        \
| fold -w 12 -s                \
| bat --decorations=never     \
    --paging=never           \
    --language bash         \
    --theme Nord           \
    --color=always        \
    -

image

oficsu commented 2 weeks ago

Ok, I just have found a better workaround that works in my case:

echo Lorem ipsum dolor sit a met,   \
     consectetur adipiscing elit.  \
     Nullam venenatis nec est     \
     eu tristique                \
| fold -w 12 -s                 \
`# replace newlines by unique` \
`# invisible character that ` \
`# is ignored by syntax    ` \
`# highlighting           ` \
| sed -zE "s/\n/\x2062/g"  \
| bat --decorations=never \
    --paging=never       \
    --language bash     \
    --theme Nord       \
    --color=always    \
    -                \
`# revert it back ` \
| sed -zE "s/\x2062/\n/g"

image