Skip to content

adding color#4

Open
Var7600 wants to merge 2 commits into
wjandrea:mainfrom
Var7600:main
Open

adding color#4
Var7600 wants to merge 2 commits into
wjandrea:mainfrom
Var7600:main

Conversation

@Var7600
Copy link
Copy Markdown

@Var7600 Var7600 commented Jul 30, 2024

dded color RED for: keyword, builtin, alias/file, function, multiple
files/absolute symlinks, relative symlink
and color GREEN for the rest.
added output example with color for the README file.

Var7600 and others added 2 commits July 30, 2024 17:10
@wjandrea
Copy link
Copy Markdown
Owner

wjandrea commented Aug 7, 2024

Thanks, this is interesting, but I want to make colours optional so that what still works properly on terminals that don't support colour.

Comment thread src/symlink-info.sh

## COLOR
GREEN="\033[0;32m"
NORMAL="\033[0;00m"
Copy link
Copy Markdown
Owner

@wjandrea wjandrea Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0;00 is redundant. Just use 0

Comment thread src/what.sh
## COLOR
GREEN="\033[0;32m"
NORMAL="\033[0;00m"
RED="\033[0;31m"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On source, these will be in the environment and could easily be overwritten. I would obfuscate them like the functions: _What_GREEN etc

Comment thread src/what.sh
# Print the *current* definition.
_What_indent 2
printf "definition: "
# shellcheck disable=SC2059
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to do this when you can use %b instead: printf "%bdefinition%b: " "$GREEN" "$NORMAL"

Comment thread src/what.sh

# Find the source by turning on extended debugging.
# Looping not required because only one definition exists at a time.
# Looping not required because only one exists at a time.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove? Is this just a typo?

@Var7600
Copy link
Copy Markdown
Author

Var7600 commented Aug 11, 2024

Thanks, this is interesting, but I want to make colours optional so that what still works properly on terminals that don't support colour.

so can i add a option like --color to output with colors?

@wjandrea
Copy link
Copy Markdown
Owner

so can i add a option like --color to output with colors?

Yeah, that'd work. Although, what uses getopts, so it'd have to be a single character like -c.

(Sorry for the delay. I missed your reply somehow.)

@Var7600
Copy link
Copy Markdown
Author

Var7600 commented Aug 20, 2024

so can i add a option like --color to output with colors?

Yeah, that'd work. Although, what uses getopts, so it'd have to be a single character like -c.

(Sorry for the delay. I missed your reply somehow.)

okay -c option i'm going to try base on your suggestions and reviews.

@Dunes
Copy link
Copy Markdown

Dunes commented Jan 24, 2026

Looks, like this got forgotten about. However, if you care about portable terminal colour codes then try using tput. It queries the terminfo database to see if the terminal supports the given capability and if so, outputs a sequence that the terminal will understand. It comes with most linux distributions, except things like alpine. Example usage:

RED=1
GREEN=2

put_green() {
    tput setaf $GREEN  # sets foreground colour to green if terminal supports colors
    echo $@
    tput sgr0 # resets terminal to initial state
}

Alternatively, if you want to avoid two tput calls for each line you want to print:

RED="$( tput setaf 1 )"
GREEN="$( tput setaf 2 )"
RESET="$( tput sgr0 )"

put_color() {
   printf "%s%s%s\n" "$1" "${*:2}" "$RESET"
}

put_color $GREEN foo bar

Almost everyone has an xterm, so this basically reduces to the escape codes you already have in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants