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

adding color #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 8 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,24 @@ 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

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:

Expand Down Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions src/symlink-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#
# See functions "_usage" and "_help" for more details.

## COLOR
GREEN="\033[0;32m"
NORMAL="\033[0;00m"
Copy link
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


function _help {
_usage
echo
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
38 changes: 25 additions & 13 deletions src/what.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
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


function _What_alias { (
# Get info about an alias.

Expand Down Expand Up @@ -38,7 +43,8 @@ function _What_alias { (
if [[ $print_definition == true ]]; then
# Print the *current* definition.
_What_indent 2
printf "definition: "
# shellcheck disable=SC2059
Copy link
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"

printf "${GREEN}definition${NORMAL}: "
alias -- "$alias"
fi
) }
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
) }
Expand All @@ -228,19 +236,20 @@ 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.
Copy link
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?

read -r _ line_num filename <<< "$(
shopt -s extdebug
declare -F -- "$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
Expand All @@ -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
Expand All @@ -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' \
Expand All @@ -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 {
Expand Down