bat icon indicating copy to clipboard operation
bat copied to clipboard

Bash scripts with .txt are not colored

Open Tonus1 opened this issue 8 months ago • 3 comments

What steps will reproduce the bug?

  1. change the extension of your script to nn script.nn, it works
  2. change to txt, script.txt, it no longer displays colors

Would have espected the syntax is detected even when the file ends in .txt

How did you install bat?

Used the SlackBuild provided at slackbuilds.org

bat 0.24.0 on Slackware64-15.0

Software version

bat 0.24.0

Operating system

Linux 5.15.117

Command-line

/usr/bin/bat --diagnostic mutt-colors.txt 

Environment variables

SHELL=/bin/bash
PAGER=<not set>
LESS=<not set>
LANG=fr_FR.utf8
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 --paging always'\'''

System Config file

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

Config file

# This is `bat`s configuration file. Each line either contains a comment or
# a command-line option that you want to pass to `bat` by default. You can
# run `bat --help` to get a list of all possible configuration options.

# Specify desired highlighting theme (e.g. "TwoDark"). Run `bat --list-themes`
# for a list of all available themes
#--theme="TwoDark"
--theme="Solarized (dark)"

# Enable this to use italic text on the terminal. This is not supported on all
# terminal emulators (like tmux, by default):
--italic-text=always

# Uncomment the following line to disable automatic paging:
#--paging=never

# Uncomment the following line if you are using less version >= 551 and want to
# enable mouse scrolling support in `bat` when running inside tmux. This might
# disable text selection, unless you press shift.
#--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"

# Syntax mappings: map a certain filename pattern to a language.
#   Example 1: use the C++ syntax for Arduino .ino files
#   Example 2: Use ".gitignore"-style highlighting for ".ignore" files
#--map-syntax "*.ino:C++"
#--map-syntax ".ignore:Git Ignore"

--style="grid,snip,header,header-filesize,changes"

Custom assets metadata

Could not read contents of '/home/tonus/.cache/bat/metadata.yaml': No such file or directory (os error 2).

Custom assets

'/home/tonus/.cache/bat' not found

Compile time information

  • Profile: release
  • Target triple: x86_64-unknown-linux-gnu
  • Family: unix
  • OS: linux
  • Architecture: x86_64
  • Pointer width: 64
  • Endian: little
  • CPU features: fxsr,sse,sse2
  • Host: x86_64-unknown-linux-gnu

Less version

> less --version 
less 590 (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

Tonus1 avatar Oct 25 '23 04:10 Tonus1

What does the first line of your bash script look like?

keith-hall avatar Oct 27 '23 01:10 keith-hall

Either won't work :

#!/usr/bin/env bash
#!/bin/bash

Here's the script that made me notice

#!/usr/bin/env bash

#### Have a look at config file.

#### Colors listed with script :

#
# (c) 2016 Shudmanul Chowdhury
#
# A simple script to test and get escape sequences for 256 terminal colors.
#

NAME="${0##*/}"
UNDER="\e[4m"
RESET="\e[0m"
TCOLS=$(tput cols)

# prints foreground escape sequence
fgc() {
    echo "\e[38;5;${1}m"
}

# prints background escape sequence
bgc() {
    echo "\e[48;5;${1}m"
}

usage() {
    while IFS= read -r line; do
        echo -e "$line"
    done << EOF
Usage: $NAME [-a] [-b num] [-f num]
    -a  display all colors
    -b  display the background color escape sequence for ${UNDER}num${RESET}
    -f  display the foreground color escape sequence for ${UNDER}num${RESET}
    ${UNDER}num${RESET} should be a value from 0 to 255.
EOF
    exit 1
}

# displays all 256 colors
disp() {
    local c # color code

    # print basic colors
    echo "Basic (0 to 15):"
    for c in {0..15}; do
        # set the background color to its respective color code
        echo -en "$(bgc $c)   $RESET"
    done
    printf "\n\n"

    # print greyscale
    echo "Greyscale (232 to 255):"
    for c in {232..255}; do
        # set the background color to its respective color code
        echo -en "$(bgc $c)  $RESET"
    done
    printf "\n\n"

    # show a chart of six 6x6 color palettes
    echo "6x6x6 Cubic Palette (16 to 231): "
    local colc=0                # column counter
    local sixs=$(($TCOLS / 30)) # number of 6x6 palettes across screen
    local cols=$(($sixs * 6))   # number of color columns (5 characters wide)
    c=16                        # starting color code
 
    while [[ $c -lt 232 ]]; do  # 16 to 231

        # set the background color to its respective color code
        printf "$(bgc $c) %03d $RESET" "$c"

        # increment the color code and the 
        ((c++))
        ((colc++))

        # enter a new line if the terminal width has been reached
        if [[ $colc -eq $cols ]]; then

            # go back to the first set of 6x6 colors on this line if any left
            if [[ $((($c - 16) % 36)) -ne 0 ]]; then
                c=$(($c - ($sixs - 1) * 36))
            fi

            colc=0  # go back to the first column
            echo    # new line

        # go to the next set of 6x6 colors
        elif [[ $(($colc % 6)) -eq 0 ]] && [[ $(($c + 30)) -lt 232 ]]; then
            ((c+=30))
        fi        
    done
    echo -e $RESET
}

# process only one option
if getopts ab:f: OPT; then
    case "$OPT" in
    a)  # display all colors
        disp
        ;;
    b|f)# display escape sequence
        if [[ $OPTARG =~ ^[0-9]+$ && $OPTARG -ge 0 ]]; then
            [[ $OPT == "b" ]] && bgc $OPTARG
            [[ $OPT == "f" ]] && fgc $OPTARG
            exit 0
        else
            usage
        fi
        ;;
    esac
else
    usage
fi

Tonus1 avatar Oct 27 '23 10:10 Tonus1

Thanks. I am able to reproduce this. I believe the file extension, .txt , takes precedence over the first line matching logic. I note that using --ignored-suffix .txt doesn't help either...

keith-hall avatar Oct 31 '23 19:10 keith-hall