[shell_aliases] try fix array looping
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
# Released under the LGPL (GNU Lesser General Public License)
|
# Released under the LGPL (GNU Lesser General Public License)
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
if sed -E 's///g' </dev/null &>/dev/null; then
|
if sed -E 's///g' </dev/null &>/dev/null; then
|
||||||
SED_REGEX_ARG="E"
|
SED_REGEX_ARG="E"
|
||||||
elif sed -r 's///g' </dev/null &>/dev/null; then
|
elif sed -r 's///g' </dev/null &>/dev/null; then
|
||||||
@@ -13,7 +12,6 @@ else
|
|||||||
echo "Cannot determine extended regex argument for sed! (Doesn't respond to either -E or -r)"
|
echo "Cannot determine extended regex argument for sed! (Doesn't respond to either -E or -r)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Wrap common commands with numeric argument expansion.
|
# Wrap common commands with numeric argument expansion.
|
||||||
# Prepends everything with exec_scmb_expand_args,
|
# Prepends everything with exec_scmb_expand_args,
|
||||||
# even if commands are already aliases or functions
|
# even if commands are already aliases or functions
|
||||||
@@ -21,14 +19,14 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
|
|||||||
# Do it in a function so we don't bleed variables
|
# Do it in a function so we don't bleed variables
|
||||||
function _git_wrap_commands() {
|
function _git_wrap_commands() {
|
||||||
# Define 'whence' for bash, to get the value of an alias
|
# Define 'whence' for bash, to get the value of an alias
|
||||||
type whence > /dev/null 2>&1 || function whence() { LC_MESSAGES="C" type "$@" | sed -$SED_REGEX_ARG -e "s/.*is aliased to \`//" -e "s/'$//"; }
|
type whence >/dev/null 2>&1 || function whence() { LC_MESSAGES="C" type "$@" | sed -$SED_REGEX_ARG -e "s/.*is aliased to \`//" -e "s/'$//"; }
|
||||||
local cmd=''
|
local cmd=''
|
||||||
for cmd in $scmb_wrapped_shell_commands; do
|
for cmd in "${scmb_wrapped_shell_commands[@]}"; do
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: Wrapping $cmd..."; fi
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: Wrapping $cmd..."; fi
|
||||||
|
|
||||||
# Special check for 'cd', to make sure SCM Breeze is loaded after RVM
|
# Special check for 'cd', to make sure SCM Breeze is loaded after RVM
|
||||||
if [ "$cmd" = 'cd' ]; then
|
if [ "$cmd" = 'cd' ]; then
|
||||||
if [ -e "$HOME/.rvm" ] && ! type rvm > /dev/null 2>&1; then
|
if [ -e "$HOME/.rvm" ] && ! type rvm >/dev/null 2>&1; then
|
||||||
echo -e "\\033[0;31mSCM Breeze must be loaded \\033[1;31mafter\\033[0;31m RVM, otherwise there will be a conflict when RVM wraps the 'cd' command.\\033[0m"
|
echo -e "\\033[0;31mSCM Breeze must be loaded \\033[1;31mafter\\033[0;31m RVM, otherwise there will be a conflict when RVM wraps the 'cd' command.\\033[0m"
|
||||||
echo -e "\\033[0;31mPlease move the line that loads SCM Breeze to the bottom of your ~/.bashrc\\033[0m"
|
echo -e "\\033[0;31mPlease move the line that loads SCM Breeze to the bottom of your ~/.bashrc\\033[0m"
|
||||||
continue
|
continue
|
||||||
@@ -39,12 +37,14 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
|
|||||||
|
|
||||||
# Don't do anything if command already aliased, or not found.
|
# Don't do anything if command already aliased, or not found.
|
||||||
*'exec_scmb_expand_args'*)
|
*'exec_scmb_expand_args'*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is already wrapped"; fi;;
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is already wrapped"; fi
|
||||||
|
;;
|
||||||
|
|
||||||
*'not found'*)
|
*'not found'*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd not found!"; fi;;
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd not found!"; fi
|
||||||
|
;;
|
||||||
|
|
||||||
*'aliased to'*|*'is an alias for'*)
|
*'aliased to'* | *'is an alias for'*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an alias"; fi
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an alias"; fi
|
||||||
# Store original alias
|
# Store original alias
|
||||||
local original_alias="$(whence $cmd)"
|
local original_alias="$(whence $cmd)"
|
||||||
@@ -54,15 +54,16 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
|
|||||||
# Detect original $cmd type, and escape
|
# Detect original $cmd type, and escape
|
||||||
case "$(LC_MESSAGES="C" type "$cmd" 2>&1)" in
|
case "$(LC_MESSAGES="C" type "$cmd" 2>&1)" in
|
||||||
# Escape shell builtins with 'builtin'
|
# Escape shell builtins with 'builtin'
|
||||||
*'is a shell builtin'*) local escaped_cmd="builtin $cmd";;
|
*'is a shell builtin'*) local escaped_cmd="builtin $cmd" ;;
|
||||||
# Get full path for files with 'find_binary' function
|
# Get full path for files with 'find_binary' function
|
||||||
*) local escaped_cmd="$(find_binary $cmd)";;
|
*) local escaped_cmd="$(find_binary $cmd)" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Expand original command into full path, to avoid infinite loops
|
# Expand original command into full path, to avoid infinite loops
|
||||||
local expanded_alias="$(echo $original_alias | sed -$SED_REGEX_ARG "s%(^| )$cmd($| )%\\1$escaped_cmd\\2%")"
|
local expanded_alias="$(echo $original_alias | sed -$SED_REGEX_ARG "s%(^| )$cmd($| )%\\1$escaped_cmd\\2%")"
|
||||||
# Wrap previous alias with escaped command
|
# Wrap previous alias with escaped command
|
||||||
alias $cmd="exec_scmb_expand_args $expanded_alias";;
|
alias $cmd="exec_scmb_expand_args $expanded_alias"
|
||||||
|
;;
|
||||||
|
|
||||||
*'is a'*'function'*)
|
*'is a'*'function'*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is a function"; fi
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is a function"; fi
|
||||||
@@ -71,30 +72,32 @@ if [ "$shell_command_wrapping_enabled" = "true" ] || [ "$bash_command_wrapping_e
|
|||||||
# Remove function
|
# Remove function
|
||||||
unset -f "$cmd"
|
unset -f "$cmd"
|
||||||
# Create function that wraps old function
|
# Create function that wraps old function
|
||||||
eval "${cmd}(){ exec_scmb_expand_args __original_${cmd} \"\$@\"; }";;
|
eval "${cmd}(){ exec_scmb_expand_args __original_${cmd} \"\$@\"; }"
|
||||||
|
;;
|
||||||
|
|
||||||
*'is a shell builtin'*)
|
*'is a shell builtin'*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is a shell builtin"; fi
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is a shell builtin"; fi
|
||||||
# Handle shell builtin commands
|
# Handle shell builtin commands
|
||||||
alias $cmd="exec_scmb_expand_args builtin $cmd";;
|
alias $cmd="exec_scmb_expand_args builtin $cmd"
|
||||||
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an executable file"; fi
|
if [ "${scmbDebug:-}" = "true" ]; then echo "SCMB: $cmd is an executable file"; fi
|
||||||
# Otherwise, command is a regular script or binary,
|
# Otherwise, command is a regular script or binary,
|
||||||
# and the full path can be found with 'find_binary' function
|
# and the full path can be found with 'find_binary' function
|
||||||
alias $cmd="exec_scmb_expand_args '$(find_binary $cmd)'";;
|
alias $cmd="exec_scmb_expand_args '$(find_binary $cmd)'"
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
# Clean up
|
# Clean up
|
||||||
declare -f whence > /dev/null && unset -f whence
|
declare -f whence >/dev/null && unset -f whence
|
||||||
}
|
}
|
||||||
_git_wrap_commands
|
_git_wrap_commands
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Function wrapper around 'll'
|
# Function wrapper around 'll'
|
||||||
# Adds numbered shortcuts to output of ls -l, just like 'git status'
|
# Adds numbered shortcuts to output of ls -l, just like 'git status'
|
||||||
if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/null 2>&1; then
|
if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby >/dev/null 2>&1; then
|
||||||
# BSD ls is different to Linux (GNU) ls
|
# BSD ls is different to Linux (GNU) ls
|
||||||
# Test for BSD ls
|
# Test for BSD ls
|
||||||
if ! (ls --version 2>/dev/null || echo "BSD") | grep GNU >/dev/null 2>&1; then
|
if ! (ls --version 2>/dev/null || echo "BSD") | grep GNU >/dev/null 2>&1; then
|
||||||
@@ -103,15 +106,16 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/nu
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Test if readlink supports -f option, test for greadlink on Mac, then fallback to perl
|
# Test if readlink supports -f option, test for greadlink on Mac, then fallback to perl
|
||||||
if \readlink -f / > /dev/null 2>&1; then
|
if \readlink -f / >/dev/null 2>&1; then
|
||||||
_abs_path_command=(readlink -f)
|
_abs_path_command=(readlink -f)
|
||||||
elif greadlink -f / > /dev/null 2>&1; then
|
elif greadlink -f / >/dev/null 2>&1; then
|
||||||
_abs_path_command=(greadlink -f)
|
_abs_path_command=(greadlink -f)
|
||||||
else
|
else
|
||||||
_abs_path_command=(perl -e 'use Cwd abs_path; print abs_path(shift)')
|
_abs_path_command=(perl -e 'use Cwd abs_path; print abs_path(shift)')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unalias ll > /dev/null 2>&1; unset -f ll > /dev/null 2>&1
|
unalias ll >/dev/null 2>&1
|
||||||
|
unset -f ll >/dev/null 2>&1
|
||||||
function ls_with_file_shortcuts {
|
function ls_with_file_shortcuts {
|
||||||
local ll_output
|
local ll_output
|
||||||
local ll_command # Ensure sort ordering of the two invocations is the same
|
local ll_command # Ensure sort ordering of the two invocations is the same
|
||||||
@@ -142,7 +146,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/nu
|
|||||||
else # It's a file, expand the current directory
|
else # It's a file, expand the current directory
|
||||||
rel_path=.
|
rel_path=.
|
||||||
fi
|
fi
|
||||||
elif [[ -d $arg || ( -f $arg && $rel_path != . ) ]]; then
|
elif [[ -d $arg || (-f $arg && $rel_path != .) ]]; then
|
||||||
if [[ -f $arg ]]; then arg=$PWD; fi # Get directory for current argument
|
if [[ -f $arg ]]; then arg=$PWD; fi # Get directory for current argument
|
||||||
# We've already seen a different directory. Quit to avoid damage (issue #274)
|
# We've already seen a different directory. Quit to avoid damage (issue #274)
|
||||||
printf 'scm_breeze: Cannot list relative to both directories:\n %s\n %s\n' "$arg" "$rel_path" >&2
|
printf 'scm_breeze: Cannot list relative to both directories:\n %s\n %s\n' "$arg" "$rel_path" >&2
|
||||||
@@ -159,7 +163,7 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/nu
|
|||||||
if [ -e "$HOME"/.user_sym ]; then
|
if [ -e "$HOME"/.user_sym ]; then
|
||||||
# Little bit of ruby golf to rejustify the user/group/size columns after replacement
|
# Little bit of ruby golf to rejustify the user/group/size columns after replacement
|
||||||
# TODO(ghthor): Convert this to a cat <<EOF to improve readibility
|
# TODO(ghthor): Convert this to a cat <<EOF to improve readibility
|
||||||
function rejustify_ls_columns(){
|
function rejustify_ls_columns() {
|
||||||
ruby -e "o=STDIN.read;re=/^(([^ ]* +){2})(([^ ]* +){3})/;\
|
ruby -e "o=STDIN.read;re=/^(([^ ]* +){2})(([^ ]* +){3})/;\
|
||||||
u,g,s=o.lines.map{|l|l[re,3]}.compact.map(&:split).transpose.map{|a|a.map(&:size).max+1};\
|
u,g,s=o.lines.map{|l|l[re,3]}.compact.map(&:split).transpose.map{|a|a.map(&:size).max+1};\
|
||||||
puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}"
|
puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}"
|
||||||
@@ -168,12 +172,12 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/nu
|
|||||||
local USER_SYM=$(/bin/cat $HOME/.user_sym)
|
local USER_SYM=$(/bin/cat $HOME/.user_sym)
|
||||||
if [ -f "$HOME/.staff_sym" ]; then
|
if [ -f "$HOME/.staff_sym" ]; then
|
||||||
local STAFF_SYM=$(/bin/cat $HOME/.staff_sym)
|
local STAFF_SYM=$(/bin/cat $HOME/.staff_sym)
|
||||||
ll_output=$(echo "$ll_output" | \
|
ll_output=$(echo "$ll_output" |
|
||||||
\sed -$SED_REGEX_ARG "s/ $USER staff/ $USER_SYM $STAFF_SYM /g" | \
|
\sed -$SED_REGEX_ARG "s/ $USER staff/ $USER_SYM $STAFF_SYM /g" |
|
||||||
rejustify_ls_columns)
|
rejustify_ls_columns)
|
||||||
else
|
else
|
||||||
ll_output=$(echo "$ll_output" | \
|
ll_output=$(echo "$ll_output" |
|
||||||
\sed -$SED_REGEX_ARG "s/ $USER/ $USER_SYM /g" | \
|
\sed -$SED_REGEX_ARG "s/ $USER/ $USER_SYM /g" |
|
||||||
rejustify_ls_columns)
|
rejustify_ls_columns)
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -186,7 +190,8 @@ if [ "$shell_ls_aliases_enabled" = "true" ] && builtin command -v ruby > /dev/nu
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Use ruby to inject numbers into ls output
|
# Use ruby to inject numbers into ls output
|
||||||
echo "$ll_output" | ruby -e "$( \cat <<EOF
|
echo "$ll_output" | ruby -e "$(
|
||||||
|
\cat <<EOF
|
||||||
output = STDIN.read
|
output = STDIN.read
|
||||||
e = 1
|
e = 1
|
||||||
re = /^(([^ ]* +){8})/
|
re = /^(([^ ]* +){8})/
|
||||||
@@ -196,7 +201,7 @@ output.lines.each do |line|
|
|||||||
e += 1
|
e += 1
|
||||||
end
|
end
|
||||||
EOF
|
EOF
|
||||||
)"
|
)"
|
||||||
|
|
||||||
# Set numbered file shortcut in variable
|
# Set numbered file shortcut in variable
|
||||||
local e=1
|
local e=1
|
||||||
|
|||||||
@@ -7,11 +7,13 @@
|
|||||||
#
|
#
|
||||||
# Unit tests for shell command wrapping
|
# Unit tests for shell command wrapping
|
||||||
|
|
||||||
export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
|
export scmbDir="$(cd -P "$(dirname "$0")" && pwd)/../../.."
|
||||||
|
|
||||||
# Zsh compatibility
|
# Zsh compatibility
|
||||||
if [ -n "${ZSH_VERSION:-}" ]; then
|
if [ -n "${ZSH_VERSION:-}" ]; then
|
||||||
shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit
|
shell="zsh"
|
||||||
|
SHUNIT_PARENT=$0
|
||||||
|
setopt shwordsplit
|
||||||
else
|
else
|
||||||
# Bash needs this option so that 'alias' works in a non-interactive shell
|
# Bash needs this option so that 'alias' works in a non-interactive shell
|
||||||
shopt -s expand_aliases
|
shopt -s expand_aliases
|
||||||
@@ -25,7 +27,7 @@ source "$scmbDir/lib/scm_breeze.sh"
|
|||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
oneTimeSetUp() {
|
oneTimeSetUp() {
|
||||||
export shell_command_wrapping_enabled="true"
|
export shell_command_wrapping_enabled="true"
|
||||||
export scmb_wrapped_shell_commands="not_found cat rm cp mv ln cd sed"
|
export scmb_wrapped_shell_commands=("not_found" "cat" "rm" "cp" "mv" "ln" "cd" "sed")
|
||||||
export shell_ls_aliases_enabled="true"
|
export shell_ls_aliases_enabled="true"
|
||||||
|
|
||||||
alias rvm="test" # Ensure tests run if RVM isn't loaded but $HOME/.rvm is present
|
alias rvm="test" # Ensure tests run if RVM isn't loaded but $HOME/.rvm is present
|
||||||
@@ -52,16 +54,15 @@ oneTimeSetUp() {
|
|||||||
|
|
||||||
# Define 'whence' function for Bash.
|
# Define 'whence' function for Bash.
|
||||||
# Must come after sourcing shell_shortcuts
|
# Must come after sourcing shell_shortcuts
|
||||||
type whence > /dev/null 2>&1 || function whence() { type "$@" | sed -e "s/.*is aliased to \`//" -e "s/'$//"; }
|
type whence >/dev/null 2>&1 || function whence() { type "$@" | sed -e "s/.*is aliased to \`//" -e "s/'$//"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
# Helper function to test that alias is defined properly.
|
# Helper function to test that alias is defined properly.
|
||||||
# (Works for both zsh and bash)
|
# (Works for both zsh and bash)
|
||||||
assertAliasEquals(){
|
assertAliasEquals() {
|
||||||
assertEquals "$1" "$(whence $2)"
|
assertEquals "$1" "$(whence $2)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
# Setup and tear down
|
# Setup and tear down
|
||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
@@ -70,7 +71,6 @@ setUp() {
|
|||||||
unset QUOTING_STYLE # Use default quoting style for ls
|
unset QUOTING_STYLE # Use default quoting style for ls
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
# Unit tests
|
# Unit tests
|
||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
@@ -105,7 +105,7 @@ test_ls_with_file_shortcuts() {
|
|||||||
# Run command in shell, load output from temp file into variable
|
# Run command in shell, load output from temp file into variable
|
||||||
# (This is needed so that env variables are exported in the current shell)
|
# (This is needed so that env variables are exported in the current shell)
|
||||||
temp_file=$(mktemp -t scm_breeze.XXXXXXXXXX)
|
temp_file=$(mktemp -t scm_breeze.XXXXXXXXXX)
|
||||||
ls_with_file_shortcuts > "$temp_file"
|
ls_with_file_shortcuts >"$temp_file"
|
||||||
ls_output=$(<"$temp_file" strip_colors)
|
ls_output=$(<"$temp_file" strip_colors)
|
||||||
|
|
||||||
# Compare as fixed strings (F), instead of normal grep behavior
|
# Compare as fixed strings (F), instead of normal grep behavior
|
||||||
@@ -123,7 +123,7 @@ test_ls_with_file_shortcuts() {
|
|||||||
assertEquals "$TEST_DIR/test_file" "$e5"
|
assertEquals "$TEST_DIR/test_file" "$e5"
|
||||||
|
|
||||||
# Test ls with subdirectory
|
# Test ls with subdirectory
|
||||||
ls_with_file_shortcuts 'a "b"' > $temp_file
|
ls_with_file_shortcuts 'a "b"' >$temp_file
|
||||||
ls_output=$(<$temp_file strip_colors)
|
ls_output=$(<$temp_file strip_colors)
|
||||||
assertIncludes "$ls_output" '[1] c' F
|
assertIncludes "$ls_output" '[1] c' F
|
||||||
# Test that env variable is set correctly
|
# Test that env variable is set correctly
|
||||||
|
|||||||
Reference in New Issue
Block a user