A "bat has different behavior than cat" bug report (I have bat aliased to cat now):
β‘~/proj/stderr$ ipython -c "print('hello')"
hello
β‘~/proj/stderr$ ipython -c "print('hello')" | cat
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β 0;IPython: proj/stderr^Ghello
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β‘~/proj/stderr$ ipython -c "print('hello')" | \cat
hello
β‘~/proj/stderr$ python3 -c "print('hello')" | cat
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β hello
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
I dunno what the 0;...^G syntax means or why IPython adds it, but figured I'd report.
Thanks again for bat!
Thank you for reporting this.
Interestingly, we can use bats new -A/--show-all option (#395) to figure out what's going on here. This option shows non-printable characters in a human-readable form:
/tmp
βΆ ipython -c "print('hello')" | bat -A
ββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β β]0;IPython:β’/tmpβhelloβ
ββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Instead of just printing helloβ (hello + line feed / new line), IPython first outputs β]0; (some form of escape sequence), then IPython: /tmp (I'm in the /tmp folder), then β (BEL ASCII character). It turns out that these three pieces make up a Terminal control sequence to set the title of the terminal Window (see e.g. http://tldp.org/HOWTO/Xterm-Title-3.html). Indeed, if you just execute ipython -c "print('hello')", you should be able to see your terminal title change (just as long as the process is running).
IPython has a --no-term-title option to disable this behavior, so this works:
βΆ ipython --no-term-title -c "print('hello')" | bat
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β hello
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
(I'm slightly surprised that IPython does not use this setting when writing to a pipe)
Finally, there is still the question why this doesn't work when piping into bat. It turns out that this is actually a problem with the pager (less), and not bat itself. If we turn off paging, everything works fine:
βΆ ipython -c "print('hello')" | bat --paging=never
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β hello
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
On the other hand, if you pipe the output of ipython into less, you will see the same garbled output.
It turns out that less has an option to fix this. If we use -r/--raw-control-chars instead of -R/--RAW-CONTROL-CHARS, everything works as expected:
βΆ ipython -c "print('hello')" | bat --pager="less -FrX"
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β hello
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Unfortunately, using -r has some downsides. From lesss man page:
Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.
So in the end, I'm afraid we have to close this as "won't fix" :confused: (unless we can somehow show that using -r is fine)
See also:
@kbd: Any update on this? It looks like this has been addressed upstream.
Hi @sharkdp. I have not gotten to look into the ipython bug like I wanted to (and said I would in that thread) but this is definitely not a problem in bat so this should be closed.
However, the question remained whether you can use -r (vs -R) in less and have it be ok, but I don't know anything about that. Are there test cases that break in one or the other? FWIW, -r does make this case work properly:
β‘~$ export BAT_PAGER="less -RFX"
β‘~$ ipython -c "print('testing')" | bat
ββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β 0;IPython: Users/kbd^Gtesting
ββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β‘~$ export BAT_PAGER="less -rFX"
β‘~$ ipython -c "print('testing')" | bat
ββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STDIN
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β testing
ββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
However, the question remained whether you can use
-r(vs-R) inlessand have it be ok, but I don't know anything about that.
I don't know much about it either. One major problem is that we have to deal with multiple versions of less, some of which behave completely different. It would be great if there was a sane cross-platform pager (library) that we could ship with (use in) bat.
I am going to close this for now, as I don't see any way to proceed here.
Most helpful comment
Thank you for reporting this.
Interestingly, we can use
bats new-A/--show-alloption (#395) to figure out what's going on here. This option shows non-printable characters in a human-readable form:Instead of just printing
helloβ(hello + line feed / new line), IPython first outputsβ]0;(some form of escape sequence), thenIPython: /tmp(I'm in the/tmpfolder), thenβ(BEL ASCII character). It turns out that these three pieces make up a Terminal control sequence to set the title of the terminal Window (see e.g. http://tldp.org/HOWTO/Xterm-Title-3.html). Indeed, if you just executeipython -c "print('hello')", you should be able to see your terminal title change (just as long as the process is running).IPython has a
--no-term-titleoption to disable this behavior, so this works:(I'm slightly surprised that IPython does not use this setting when writing to a pipe)
Finally, there is still the question why this doesn't work when piping into
bat. It turns out that this is actually a problem with the pager (less), and notbatitself. If we turn off paging, everything works fine:On the other hand, if you pipe the output of
ipythonintoless, you will see the same garbled output.It turns out that
lesshas an option to fix this. If we use-r/--raw-control-charsinstead of-R/--RAW-CONTROL-CHARS, everything works as expected:Unfortunately, using
-rhas some downsides. Fromlesss man page:So in the end, I'm afraid we have to close this as "won't fix" :confused: (unless we can somehow show that using
-ris fine)See also: