Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap width customization #305

Closed
gAmUssA opened this issue Sep 11, 2018 · 15 comments
Closed

Wrap width customization #305

gAmUssA opened this issue Sep 11, 2018 · 15 comments
Labels
question Further information is requested

Comments

@gAmUssA
Copy link

gAmUssA commented Sep 11, 2018

hello

Quick question: Is there a way to customize word wrap width?
It looks like by default it uses 80 characters with which is to low for my use cases (when I run iTem2 in the external monitor I have a little bit more real estate.)

Thank you

@eth-p
Copy link
Collaborator

eth-p commented Sep 11, 2018

The word wrap width should automatically adjust to the terminal's width. If it's not, something odd might be going on.

  • Do the horizontal lines in --style=grid,header fill the entire terminal?
  • What happens if you run tput cols?

@sharkdp
Copy link
Owner

sharkdp commented Sep 11, 2018

@gAmUssA Thank you for your feedback.

Another possibility (I think):

  • The pager does report a (virtual) terminal width of 80 characters. You could try to run
    bat --paging=never ...
    
    and see if that helps.

@sharkdp sharkdp added the question Further information is requested label Sep 11, 2018
@gAmUssA
Copy link
Author

gAmUssA commented Sep 18, 2018

looks like it was a glitch on my side.
everything works like a charm.
Thank you, folks @eth-p @sharkdp !

@gAmUssA gAmUssA closed this as completed Sep 18, 2018
@sergeevabc
Copy link

sergeevabc commented Mar 31, 2021

Sorry to bother a long-closed issue, yet I am not satisfied about resolution.

  • I use a dual-pane file manager called Far opened full width to see both panes (so it’s not an option to resize it)
  • there are *.md files which have fluid formatting (i.e. long lines)
  • $ bat diary.md renders fine, with respect to markdown syntax, nevertheless reading is difficult
  • $ bat --terminal-width 80 --line-range 8: diary.md almost solves the issue, yet it does not respect word boundaries
───────┬──────────────────────────────────────────────────────────────────────── 
       │ File: diary.md                                             
───────┼──────────────────────────────────────────────────────────────────────── 
   8   │ # 2017-09-16 суббота                                                    
   910   │ Dictatorship is another way of defining a leader who doesn't dance to U    <-- U is expected to be
       │ S tune these days, how about focusing on your president first.                   on this very line
  11   │ https://twitter.com/badowsky1/status/909017928682663941                 
───────┴──────────────────────────────────────────────────────────────────────── 

@sharkdp
Copy link
Owner

sharkdp commented Apr 3, 2021

$ bat --terminal-width 80 --line-range 8: diary.md almost solves the issue, yet it does not respect word boundaries

Unfortunately, bat does not have word-wrapping, just character wrapping. Proper word-wrapping is something that might never be implemented, to be honest.

@eth-p
Copy link
Collaborator

eth-p commented Apr 3, 2021

To elaborate on what @sharkdp said,

Word wrapping is kind of a complex issue with source code. While bat is meant to display files for humans to read, it's important for the code to keep its meaning.

Consider Python:

def my_function():
    return "some" + "long line"

As a human, I know it's safe to break it at the + operator, and to indent the wrapped line by another 4 spaces:

def my_function():
    return "some" +
        "long line"

The .sublime-syntax format doesn't have any mechanism to explain that to bat. That either means bat has to implement its own formatter for every supported language, or it implements a contextually-unaware word wrap.

The former is a massive maintenance burden, and would probably be better off as it's own project (e.g. a Rust-based alternative to prettier).

The latter would just break the code, potentially misleading the person reading it:

def my_function():
    return "some" + "long
line"

Unless some newer format of .sublime-syntax can solve this problem, it's better to just not implement word wrapping. Ugly wrapping is better than misleading wrapping, in my opinion.

@eth-p
Copy link
Collaborator

eth-p commented Apr 3, 2021

@sergeevabc, if you need a solution specifically tailored to .txt or .md files, I could probably find a solution for you with the prettybat script, though.

@sergeevabc
Copy link

sergeevabc commented Apr 3, 2021

…it's important for the code to keep its meaning…

What? I provided an example with no code. And asked about a switch, non a default behavior. Also, there is obvious implementation that exists for ages — break input at spaces as follows

; mimics bat --terminal-width 80
$ fold -w 80 lorem.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i
ncididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostru
d exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aut
e irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat n
ulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
 officia deserunt mollit anim id est laborum.                                   

; pay attention to -s, which activates “break at spaces mode”
$ fold -w 80 -s lorem.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor  
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis      
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.   
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in  
culpa qui officia deserunt mollit anim id est laborum.                          

At present, I pipe it to bat as follows fold -w 80 -s lorem.txt | bat, but why can’t we have it on board? You know, carrying the whole zoo of MSYS2 dlls around required by fold to work is not that portable. To improve word-wrap further, check out Par (exists since 1993). It does miracles, but, unfortunately, has troubles with Unicode, but nobody to date cared to integrate this patch and build a new Windows binary (I am a tester, not a developer with proper tools to do that).

@sharkdp
Copy link
Owner

sharkdp commented May 11, 2021

@sergeevabc I fully agree that this would be nice to have. There's even a library for Rust that does "minimum raggedness" wrapping (a la par): https://crates.io/crates/textwrap

Maybe we should reconsider this. It would be great if we could open a new ticket for that though.

@masklinn
Copy link

masklinn commented May 30, 2021

@sharkdp I kinda have a related but opposite question, but am not sure whether I should create a new issue: bat currently seems to implement a GFM-style markdown rendering rather than commonmark: it will keep the existing (single) line breaks of the original document rather than filling to the screen and only adding paragraph break.

D'ya think it'd be possible to opt into "unrapping" paragraphs?

@sharkdp
Copy link
Owner

sharkdp commented Jun 10, 2021

@masklinn It would be great if you could open a new ticket with a detailed description. Thank you!

@miguelvalente
Copy link

miguelvalente commented Mar 17, 2022

@sergeevabc
Using fold removes colour highlighting. Do you have a workaround?

@mgeisler
Copy link

mgeisler commented Jul 6, 2022

D'ya think it'd be possible to opt into "unrapping" paragraphs?

Hey @masklinn, it just so happens that I've implemented that kind of functionality in https://crates.io/crates/textwrap, see the unfill method. It's rather simple, though, and a much more robust way would be to use a real Markdown parser such as https://github.com/kivikakk/comrak. I've used that in the past to format Markdown files and I know it supports wrapping at a given line width.

Maybe we should reconsider this. It would be great if we could open a new ticket for that though.

Currently, Textwrap takes a line of text (a paragraph) as input and returns a Vec<&str> with the wrapped text (actually a Cow<str> but that's less important). You can send multiple lines to Textwrap and it will wrap then one by one (it splits on \n and treats each line independently).

I have no idea of the internals of bat, but I would imagine that wrapping could be done by iterating over the output lines and wrapping them one by one. Textwrap will let ANSI escape sequences pass-through, so the syntax highlighting can be done before this step. This approach should give the pager data to work with immediately.

Infact, this makes me think that I should include a Textwrap example which simply reads lines from stdin and sends wrapped output to stdout. That example would work like the fold or par programs mentioned above.

@YodaEmbedding
Copy link

YodaEmbedding commented Aug 4, 2023

@sergeevabc Using fold removes colour highlighting. Do you have a workaround?

Just run bat at the end and remind bat what the language is.

fold -s README.md | bat --language=md

Convenience function:

batw() {
  local f="$1"
  fold -s "$f" | bat --language="${f##*.}" "${@:2}"
}

Another alternative:

batw() {
  local f="$1"
  local d="$(mktemp -d)"
  fold -s "$f" > "$d/$f"
  bat "$d/$f"
  rm "$d/$f"
  rm -d "$d"
}

bat_word_wrap

@sergeevabc
Copy link

Still no switch to break input text at spaces?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants