Skip to content

Commit

Permalink
Merge pull request #2 from aleohl/master
Browse files Browse the repository at this point in the history
Polish the code to comply with shellcheck
  • Loading branch information
anupamsr committed Apr 13, 2022
2 parents fdd58ce + f413e5b commit 2e1a673
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions h.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function h() {
}

local _OPTS
_OPTS=""

# detect pipe or tty
if [[ -t 0 ]]; then
Expand All @@ -54,14 +55,14 @@ function h() {
esac
done

shift $(($OPTIND - 1))
shift "$(( OPTIND - 1 ))"

# set zsh compatibility
[[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays && setopt ignorebraces
[[ -n ${ZSH_VERSION} ]] && setopt localoptions && setopt ksharrays && setopt ignorebraces

local _i=0

if [[ -n $H_COLORS_FG ]]; then
if [[ -n ${H_COLORS_FG} ]]; then
local _CSV="$H_COLORS_FG"
local OLD_IFS="$IFS"
IFS=','
Expand All @@ -71,7 +72,7 @@ function h() {
done
IFS="$OLD_IFS"
else
_COLORS_FG=(
_COLORS_FG=(
"underline bold red" \
"underline bold green" \
"underline bold yellow" \
Expand All @@ -81,7 +82,7 @@ function h() {
)
fi

if [[ -n $H_COLORS_BG ]]; then
if [[ -n ${H_COLORS_BG} ]]; then
local _CSV="$H_COLORS_BG"
local OLD_IFS="$IFS"
IFS=','
Expand All @@ -91,7 +92,7 @@ function h() {
done
IFS="$OLD_IFS"
else
_COLORS_BG=(
_COLORS_BG=(
"bold on_red" \
"bold on_green" \
"bold black on_yellow" \
Expand All @@ -102,7 +103,7 @@ function h() {
)
fi

if [[ -z $n_flag ]]; then
if [[ -n ${n_flag} ]]; then
#inverted-colors-last scheme
_COLORS=("${_COLORS_FG[@]}" "${_COLORS_BG[@]}")
else
Expand All @@ -116,32 +117,35 @@ Check the content of your H_COLORS_FG and H_COLORS_BG environment variables or u
return 1
fi

if [ -n "$ZSH_VERSION" ]; then
if [[ -n ${ZSH_VERSION} ]]; then
local WHICH="whence"
else [ -n "$BASH_VERSION" ]
else [[ -n ${BASH_VERSION} ]]
local WHICH="type -P"
fi

local ACK
if ! ACKGREP_LOC="$($WHICH ack-grep)" || [ -z "$ACKGREP_LOC" ]; then
if ! ACK_LOC="$($WHICH ack)" || [ -z "$ACK_LOC" ]; then
echo "ERROR: Could not find the ack or ack-grep commands"
return 1
else
local ACK=$($WHICH ack)
ACK="$($WHICH ack)"
fi
else
local ACK=$($WHICH ack-grep)
ACK="$($WHICH ack-grep)"
fi

local _COMMAND
_COMMAND=""
# build the filtering command
for keyword in "$@"
do
local _COMMAND=$_COMMAND"$ACK $_OPTS --noenv --flush --passthru --color --color-match=\"${_COLORS[$_i]}\" '$keyword' |"
_COMMAND=$_COMMAND"$ACK $_OPTS --noenv --flush --passthru --color --color-match=\"${_COLORS[$_i]}\" '$keyword' |"
_i=$_i+1
done
#trim ending pipe
_COMMAND=${_COMMAND%?}
#echo "$_COMMAND"
cat - | eval $_COMMAND
cat - | eval "$_COMMAND"

}

0 comments on commit 2e1a673

Please sign in to comment.