sharkdp / bat

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

Wrap before a word #1140

Closed typkrft closed 4 years ago

typkrft commented 4 years ago

Is it possible to wrap before a word instead of in the middle of a word?

eth-p commented 4 years ago

Unfortunately, not at the moment. There's nothing technically preventing us from implementing word wrapping that splits on whitespace or parsed syntax tokens, but I'm skeptical of taking a naïve approach to it becsuse it could alter the meaning of the printed code.

Sublime syntax files don't specify how languages are supposed to handle wrapping, indentation and whitespace, and making assumptions about that would not have held for every language. For example, some languages like Python or Sass use indentation for determining blocks. If bat inserted a hard wrap in one of these languages and didn't get the indentation correct, it could mean something entirely different than what the unwraped code meant.

typkrft commented 4 years ago

Yeah I totally get that. I generally don't use bat for looking at code. My primary use for it is to attractively format article summaries I've pulled from newsboat with sumy. So an option would be great --text-wrap or something, but I totally get that this may be an edge case and not worth the effort. Thanks for the great program regardless.

sharkdp commented 4 years ago

If you are mainly interested in text, you could have a look at par (https://en.wikipedia.org/wiki/Par_(command)). It's an "ancient" tool, but works great.

As the desired width, you would take the actual terminal width (tput cols) and subtract the width of bats sidebar (9 characters for full style):

bat /tmp/txt | par $(($(tput cols) - 9)) | bat

(you would obviously put this into a shell function or similar)

image

Line numbers are obviously not related to the original line numbers. Git changes also wouldn't work.

typkrft commented 4 years ago

Amazing. I'd never even heard of that command. Truly appreciate the assistance. This is completely satisfactory and this issue can be closed as far as I'm concerned.