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

bat -pp Adds Non-Printable Characters, Making it Incompatible with cat #3004

Open
etiennecollin opened this issue Jun 22, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@etiennecollin
Copy link

What steps will reproduce the bug?

> echo "This is a test" > test.txt
> bat -ppA test.txt
This·is·a·test␊
> echo "This is a test" | bat -pp > test.txt
> bat -ppA test.txt
␛[38;2;251;241;199mThis·is·a·test␛[0m␊

What happens?

These non-printable characters are added: ␛[38;2;251;241;199m and ␛[0m.
This causes a bunch of issues when trying to use an alias such as alias cat="bat -pp" because when using cat to, for example, put the contents of a file in a variable, the text content is contaminated by these characters. This causes a many of scripts and functions to either break of have the wrong output.

For example, the following script fails:

alias cat="bat -pp"
# Let's imagine that the `echo` is a simple text file which contains the path to a directory as its content.
# The code would in reality be `local x="$(cat -- "$file")"`.
local x="$(echo '~/Downloads' | cat)"
chdir "$x"

the output is as follows (note that the path ~/Downloads exists and can be accessed with chdir ~/Downloads):

chdir: no such file or directory: ^[[38;2;251;241;199m~/Downloads^[[0m

What did you expect to happen instead?

The output of bat -pp should be clean; i.e. exempt from any formatting. In its current state, bat cannot be used to replace cat as it does not output truly plain text.

My current workaround is as follows:

# See https://superuser.com/a/380778 for the sed command that removes the color codes
function cat {
	bat -pp "$@" | sed -e 's/\x1b\[[0-9;]*m//g'
}

How did you install bat?

homebrew: brew install bat


bat version and environment

Software version

bat 0.24.0

Operating system

macOS 14.4.1 (Darwin 23.4.0)

Command-line

bat -pp --diagnostic

Environment variables

SHELL=/bin/zsh
PAGER=bat
LESS=<not set>
LANG=en_CA.UTF-8
LC_ALL=<not set>
BAT_PAGER=<not set>
BAT_PAGING=<not set>
BAT_CACHE_PATH=<not set>
BAT_CONFIG_PATH=<not set>
BAT_OPTS=<not set>
BAT_STYLE=<not set>
BAT_TABS=<not set>
BAT_THEME=<not set>
XDG_CONFIG_HOME=<not set>
XDG_CACHE_HOME=<not set>
COLORTERM=truecolor
NO_COLOR=<not set>
MANPAGER='sh -c '\''col -bx | bat -l man -p'\'''

System Config file

Could not read contents of '/etc/bat/config': No such file or directory (os error 2).

Config file

--color=always
--italic-text=always
--decorations=auto
--style="numbers,header-filename,header-filesize,grid,changes"
--theme="gruvbox-dark"

Custom assets metadata

bat_version: 0.24.0
creation_time:
  secs_since_epoch: 1719014819
  nanos_since_epoch: 607396000

Custom assets

  • metadata.yaml, 97 bytes
  • syntaxes.bin, 915557 bytes
  • themes.bin, 40606 bytes

Compile time information

  • Profile: release
  • Target triple: aarch64-apple-darwin
  • Family: unix
  • OS: macos
  • Architecture: aarch64
  • Pointer width: 64
  • Endian: little
  • CPU features: aes,crc,dit,dotprod,dpb,dpb2,fcma,fhm,flagm,fp16,frintts,jsconv,lor,lse,neon,paca,pacg,pan,pmuv3,ras,rcpc,rcpc2,rdm,sb,sha2,sha3,ssbs,vh
  • Host: aarch64-apple-darwin

Less version

> less --version
less 581.2 (POSIX regular expressions)
Copyright (C) 1984-2021  Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Home page: https://greenwoodsoftware.com/less
@etiennecollin etiennecollin added the bug Something isn't working label Jun 22, 2024
@etiennecollin
Copy link
Author

etiennecollin commented Jun 22, 2024

Ooops, my bad, I just realized that I had --color=always in my config file. Apparently, that flag isn't automatically set to --color=never when passing the flags -pp. Is this expected behaviour?

It might be a good idea to update the alias example in the README (see this section) to suggest the use of the flags -pp --color=never.

If you believe it should be updated, I will take care of it and open a PR.

What do you think?
Thank you for your time and amazing software!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant