From 899e1acbc6f39379726063710146c1ca410926a6 Mon Sep 17 00:00:00 2001 From: doudou diawara Date: Tue, 30 Jul 2024 17:10:02 -0400 Subject: [PATCH 1/2] adding color for what.sh symlink-info.sh --- src/symlink-info.sh | 8 ++++++-- src/what.sh | 38 +++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/symlink-info.sh b/src/symlink-info.sh index d24fd8d..c3fee10 100755 --- a/src/symlink-info.sh +++ b/src/symlink-info.sh @@ -3,6 +3,10 @@ # # See functions "_usage" and "_help" for more details. +## COLOR +GREEN="\033[0;32m" +NORMAL="\033[0;00m" + function _help { _usage echo @@ -98,7 +102,7 @@ for path; do target="$(readlink -- "$path")" _indent 1 - printf 'symlink: %s\n' "$target" + printf "${GREEN}symlink${NORMAL}: %s\n" "$target" if [[ $target == /* ]]; then # Target is absolute. @@ -113,7 +117,7 @@ for path; do path_canonical="$(readlink -m -- "$path")" if [[ $path_canonical != "$target" ]]; then _indent 1 - printf 'canonical path: %s\n' "$path_canonical" + printf "${GREEN}canonical path${NORMAL}: %s\n" "$path_canonical" fi if [[ ! -e $path_canonical ]]; then diff --git a/src/what.sh b/src/what.sh index b381e85..2af2127 100755 --- a/src/what.sh +++ b/src/what.sh @@ -4,6 +4,11 @@ # For more details, see functions _What_usage and _What_help # as well as _What_info. +## COLOR +GREEN="\033[0;32m" +NORMAL="\033[0;00m" +RED="\033[0;31m" + function _What_alias { ( # Get info about an alias. @@ -38,7 +43,8 @@ function _What_alias { ( if [[ $print_definition == true ]]; then # Print the *current* definition. _What_indent 2 - printf "definition: " + # shellcheck disable=SC2059 + printf "${GREEN}definition${NORMAL}: " alias -- "$alias" fi ) } @@ -51,19 +57,20 @@ function _What_alias_match_parse { if [[ -z $match ]]; then _What_indent 2 - printf 'possible source: %s\n' '(not found)' + printf "${GREEN}possible source:${NORMAL} %s\n" '(not found)' return fi IFS=: read -r filename line_num line <<< "$match" _What_indent 2 - printf 'possible source: %s:%s\n' "$filename" "$line_num" + printf "${GREEN}possible source:${NORMAL} %s:%s\n" "$filename" "$line_num" if [[ $print_definition == true ]]; then # Print the definition *from the file*. _What_indent 3 - printf "definition: " + # shellcheck disable=SC2059 + printf "${GREEN}definition${NORMAL}: " sed 's/^ *//; s/ *$//' <<< "$line" # Strip surrounding whitespace fi } @@ -107,7 +114,7 @@ function _What_command { ( fi _What_indent 1 - printf '%s\n' "$type" + printf "${RED}%s\n${NORMAL}" "$type" if [[ $print_type_only == true ]]; then continue @@ -204,7 +211,7 @@ function _What_filepath { ( path="$1" _What_indent 2 - printf 'path: %s\n' "$path" + printf "${GREEN}path:${NORMAL} %s\n" "$path" # If the file is a symlink. if [[ -L $path ]]; then @@ -215,7 +222,8 @@ function _What_filepath { ( # Show brief file info. _What_indent 2 - printf 'file type: ' + # shellcheck disable=SC2059 + printf "${GREEN}file type:${NORMAL} " file -bL -- "$path" | cut -d, -f1 ) } @@ -228,7 +236,7 @@ function _What_function { ( read -r _ attrs _ <<< "$(declare -pF -- "$function")" # 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. read -r _ line_num filename <<< "$( shopt -s extdebug declare -F -- "$function" @@ -236,11 +244,12 @@ function _What_function { ( # Print. _What_indent 2 - printf 'source: %s:%s\n' "$filename" "$line_num" + printf "${GREEN}source${NORMAL}: %s:%s\n" "$filename" "$line_num" # Print export status. _What_indent 2 - printf 'export: ' + # shellcheck disable=SC2059 + printf "${GREEN}export${NORMAL}: " if [[ $attrs == *x* ]]; then echo yes else @@ -250,7 +259,9 @@ function _What_function { ( if [[ $print_definition == true ]]; then # Print the function definition. _What_indent 2 - printf 'definition:\n' + # shellcheck disable=SC2059 + printf "${GREEN}definition${NORMAL}:\n" + declare -f -- "$function" | _What_indent_many 3 fi @@ -267,7 +278,8 @@ function _What_hashed { fi _What_indent 1 - printf 'hashed\n' + # shellcheck disable=SC2059 + printf "${RED}hashed${NORMAL}\n" if ! [[ -f $hashpath ]]; then printf >&2 '%s: %s: %s: Hashed file does not exist: %s\n' \ @@ -283,7 +295,7 @@ function _What_hashed { fi _What_indent 2 - printf 'path: %s\n' "$hashpath" + printf "${GREEN}path:${NORMAL} %s\n" "$hashpath" } function _What_help { From f4dd16a2892bf3678e2990a015983deee4b6436d Mon Sep 17 00:00:00 2001 From: Var7600 <77991946+Var7600@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:25:47 -0400 Subject: [PATCH 2/2] Update README.md added images showing output with colors --- README.md | 68 +++++++------------------------------------------------ 1 file changed, 8 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index c89c831..0cc812e 100644 --- a/README.md +++ b/README.md @@ -16,45 +16,14 @@ Along with it is `symlink-info`, which details complicated symlinks. `what` uses Show basic info about a variety of commands: -```none -$ what if type find what -if - keyword -type - builtin -find - file - path: /usr/bin/find - file type: ELF 64-bit LSB shared object -what - function - source: /home/wja/.local/lib/bash/what.sh:348 - export: no -``` +![what-color-1](https://github.com/user-attachments/assets/c5354c3d-dfed-4ac7-a57f-13aeb4c4627c) + A bit more complex: -```none -$ what awk sh ls -awk - file - path: /usr/bin/awk - symlink: /etc/alternatives/awk - symlink: /usr/bin/mawk - file type: ELF 64-bit LSB shared object -sh - file - path: /bin/sh - symlink: dash - canonical path: /bin/dash - file type: ELF 64-bit LSB shared object -ls - alias - possible source: /home/wja/.bash_aliases - file - path: /bin/ls - file type: ELF 64-bit LSB shared object -``` +![what-bash-color2](https://github.com/user-attachments/assets/d9c6cc08-2f7d-4e04-8e55-c6b4e7ebe5a7) + + #### Show definitions of aliases and functions @@ -62,22 +31,9 @@ Use `what -d`: ```none $ function foo { bar; } -$ what -d foo ll -foo - function - source: main:2 - export: no - definition: - foo () - { - bar - } -ll - alias - possible source: /home/wja/.bash_aliases:25 - definition: alias ll='ls -alF' # all, long, classified - definition: alias ll='ls -alF' ``` +![what-bash-color3](https://github.com/user-attachments/assets/cb421d8c-4a68-40de-b084-ddbd99913aa9) + Note that the source of a function can be traced, but not an alias. `what` basically guesses at alias sources. Specifically, it tries to find the alias name in the most common files, using a regex. It doesn't look at the definition, for example: @@ -181,15 +137,7 @@ Resolve a symlink, recursively and canonically Borrowing from the above `what` example: -```none -$ symlink-info /usr/bin/awk /bin/sh -/usr/bin/awk - symlink: /etc/alternatives/awk - symlink: /usr/bin/mawk -/bin/sh - symlink: dash - canonical path: /bin/dash -``` +![symlink-color-1](https://github.com/user-attachments/assets/96bfafcf-439c-4b17-9942-62b76a60be1c) ### Help