Merge branch 'master' of git://github.com/g5pw/scm_breeze into g5pw-master

Conflicts:
	lib/git/shell_shortcuts.sh
This commit is contained in:
Nathan Broadbent
2012-10-23 10:22:49 +13:00

View File

@@ -74,85 +74,97 @@ fi
# BSD ls is different to Linux (GNU) ls # BSD ls is different to Linux (GNU) ls
_uname="$(uname)" # Test for BSD ls
if [ "$_uname" = "Linux" ]; then if ! ls --color=auto > /dev/null 2>&1; then
# Linux ls commands # ls is BSD
_ll_command="ls -lhv --group-directories-first --color" _ls_bsd="BSD"
_ll_sys_command="ls -v --group-directories-first --color=never"
_abs_path_command="readlink -f"
elif [ "$_uname" = "Darwin" ]; then
# OS X ls commands
_ll_command="CLICOLOR_FORCE=1 ls -l -G"
_ll_sys_command="ls"
# Use perl abs_path, since readlink -f isn't available on OS X
_abs_path_command="perl -e 'use Cwd \"abs_path\"; print abs_path(shift)'"
fi fi
if [ -n "$_ll_command" ]; then # Test if readlink supports -f option, otherwise use perl (a bit slower)
# Function wrapper around 'll' if ! readlink -f > /dev/null 2>&1; then
# Adds numbered shortcuts to output of ls -l, just like 'git status' _abs_path_command='perl -e "use Cwd "abs_path"; print abs_path(shift)"'
unalias ll > /dev/null 2>&1; unset -f ll > /dev/null 2>&1 else
function ls_with_file_shortcuts { _abs_path_command="readlink -f"
# Wrap each arg in quotes fi
local wrapped_args
for arg in "$@"; do wrapped_args+=" \"$arg\""; done
local ll_output="$(eval $_ll_command $wrapped_args)"
# Parse path from args # Function wrapper around 'll'
OLDIFS="$IFS"; IFS=$'\n' # Adds numbered shortcuts to output of ls -l, just like 'git status'
for arg in $@; do unalias ll > /dev/null 2>&1; unset -f ll > /dev/null 2>&1
if [ -d "$arg" ]; then local rel_path="${arg%/}"; fi function ls_with_file_shortcuts {
done local ll_output=''
IFS="$OLDIFS"
# Replace user/group with user symbol, if defined at ~/.user_sym if [ -z $_ls_bsd ]; then
# Before : -rw-rw-r-- 1 ndbroadbent ndbroadbent 1.1K Sep 19 21:39 scm_breeze.sh ll_output="$(ls -lhv --group-directories-first --color "$@")"
# After : -rw-rw-r-- 1 𝐍 𝐍 1.1K Sep 19 21:39 scm_breeze.sh else
if [ -e $HOME/.user_sym ]; then ll_output="$(CLICOLOR_FORCE=1 ls -l -G "$@")"
# Little bit of ruby golf to rejustify the user/group/size columns after replacement fi
function rejustify_ls_columns(){
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};\
puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}"
}
ll_output=$(echo "$ll_output" | sed "s/ $USER/ $(/bin/cat $HOME/.user_sym)/g" | rejustify_ls_columns)
fi
if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then # Parse path from args
echo -e "\e[33mToo many files to create shortcuts. Running plain ll command...\e[0m" zsh_compat # Ensure sh_word_split is on
echo "$ll_output" OLDIFS="$IFS"; IFS=$'\n'
return 1 for arg in $@; do
fi if [ -d "$arg" ]; then local rel_path="${arg%/}"; fi
done
IFS="$OLDIFS"
# Use ruby to inject numbers into ls output # Replace user/group with user symbol, if defined at ~/.user_sym
ruby -e "$( cat <<EOF # Before : -rw-rw-r-- 1 ndbroadbent ndbroadbent 1.1K Sep 19 21:39 scm_breeze.sh
output = "$ll_output" # After : -rw-rw-r-- 1 𝐍 𝐍 1.1K Sep 19 21:39 scm_breeze.sh
e = 1 if [ -e $HOME/.user_sym ]; then
re = /^(([^ ]* +){8})/ # Little bit of ruby golf to rejustify the user/group/size columns after replacement
output.lines.each do |line| function rejustify_ls_columns(){
next unless line.match(re) ruby -e "o=STDIN.read;re=/^(([^ ]* +){2})(([^ ]* +){3})/;\
puts line.sub(re, "\\\1\e[2;37m[\e[0m#{e}\e[2;37m]\e[0m" << (e < 10 ? " " : " ")) u,g,s=o.lines.map{|l|l[re,3]}.compact.map(&:split).transpose.map{|a|a.map(&:size).max+1};\
e += 1 puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}"
end }
ll_output=$(echo "$ll_output" | sed "s/ $USER/ $(/bin/cat $HOME/.user_sym)/g" | rejustify_ls_columns)
fi
if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then
echo -e "\e[33mToo many files to create shortcuts. Running plain ll command...\e[0m"
echo "$ll_output"
return 1
fi
# Use ruby to inject numbers into ls output
ruby -e "$( cat <<EOF
output = "$ll_output"
e = 1
re = /^(([^ ]* +){8})/
output.lines.each do |line|
next unless line.match(re)
puts line.sub(re, "\\\1\e[2;37m[\e[0m#{e}\e[2;37m]\e[0m" << (e < 10 ? " " : " "))
e += 1
end
EOF EOF
)" )"
# Set numbered file shortcut in variable # Set numbered file shortcut in variable
local e=1 local e=1
local ll_files="$(eval $_ll_sys_command $wrapped_args)" local ll_files=''
local file=''
OLDIFS="$IFS"; IFS=$'\n' if [ -z $_ls_bsd ]; then
if [[ $shell == "zsh" ]]; then setopt shwordsplit; fi ll_files="$(ls -v --group-directories-first --color=never "$@")"
for file in $ll_files; do else
if [ -n "$rel_path" ]; then file="$rel_path/$file"; fi ll_files="$(ls "$@")"
export $git_env_char$e="$(eval $_abs_path_command \"$file\")" fi
if [ "${scmbDebug:-}" = "true" ]; then echo "Set \$$git_env_char$e => $file"; fi
let e++ OLDIFS="$IFS"; IFS=$'\n'
done if [[ $shell == "zsh" ]]; then
IFS="$OLDIFS" # Ensure sh_word_split is on
if [[ $shell == "zsh" ]]; then unsetopt shwordsplit; fi zsh_compat
} setopt shwordsplit
fi fi
for file in $ll_files; do
if [ -n "$rel_path" ]; then file="$rel_path/$file"; fi
export $git_env_char$e="$(eval $_abs_path_command \"$file\")"
if [ "${scmbDebug:-}" = "true" ]; then echo "Set \$$git_env_char$e => $file"; fi
let e++
done
IFS="$OLDIFS"
if [[ $shell == "zsh" ]]; then unsetopt shwordsplit; fi
}
# Setup aliases # Setup aliases
alias ll="exec_scmb_expand_args ls_with_file_shortcuts" alias ll="exec_scmb_expand_args ls_with_file_shortcuts"