bat icon indicating copy to clipboard operation
bat copied to clipboard

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

Open etiennecollin opened this issue 8 months ago • 1 comments

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 avatar Jun 22 '24 07:06 etiennecollin