Skip to content

Commit

Permalink
Starting off s
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Sep 28, 2012
0 parents commit ff8158b
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/s
14 changes: 14 additions & 0 deletions completions/s.bash
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
_s() {
COMPREPLY=()
local word="${COMP_WORDS[COMP_CWORD]}"

if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=( $(compgen -W "$(s commands)" -- "$word") )
else
local command="${COMP_WORDS[1]}"
local completions="$(s completions "$command")"
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
fi
}

complete -F _s s
19 changes: 19 additions & 0 deletions completions/s.zsh
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
if [[ ! -o interactive ]]; then
return
fi

compctl -K _s s

_s() {
local word words completions
read -cA words
word="${words[2]}"

if [ "${#words}" -eq 2 ]; then
completions="$(s commands)"
else
completions="$(s completions "${word}")"
fi

reply=("${(ps:\n:)completions}")
}
41 changes: 41 additions & 0 deletions libexec/s
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -e

resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done

pwd
cd "$cwd"
}

libexec_path="$(abs_dirname "$0")"
export _S_ROOT="$(abs_dirname "$libexec_path")"
export PATH="${libexec_path}:$PATH"

command="$1"
case "$command" in
"" | "-h" | "--help" )
exec s-help
;;
* )
command_path="$(command -v "s-$command" || true)"
if [ ! -x "$command_path" ]; then
echo "s: no such command \`$command'" >&2
exit 1
fi

shift
exec "$command_path" "$@"
;;
esac
42 changes: 42 additions & 0 deletions libexec/s-commands
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# Usage: s commands
# Summary: List all s commands
# Help: This command is mostly used for autocompletion in various shells, and for `s help`.
# Also, this command helps find commands that are named the same as potentially builtin shell commands (which, cd, etc)

set -e

# Provide s completions
if [ "$1" = "--complete" ]; then
echo --sh
echo --no-sh
exit
fi

if [ "$1" = "--sh" ]; then
sh=1
shift
elif [ "$1" = "--no-sh" ]; then
nosh=1
shift
fi

shopt -s nullglob

{ for path in ${PATH//:/$'\n'}; do
for command in "${path}/s-"*; do
command="${command##*s-}"
if [ -n "$sh" ]; then
if [ ${command:0:3} = "sh-" ]; then
echo ${command##sh-}
fi
elif [ -n "$nosh" ]; then
if [ ${command:0:3} != "sh-" ]; then
echo ${command##sh-}
fi
else
echo ${command##sh-}
fi
done
done
} | sort | uniq
14 changes: 14 additions & 0 deletions libexec/s-completions
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -e

COMMAND="$1"
if [ -z "$COMMAND" ]; then
echo "usage: s completions COMMAND [arg1 arg2...]" >&2
exit 1
fi

COMMAND_PATH="$(command -v "s-$COMMAND")"
if grep -i "^# provide s completions" "$COMMAND_PATH" >/dev/null; then
shift
exec "$COMMAND_PATH" --complete "$@"
fi
45 changes: 45 additions & 0 deletions libexec/s-help
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -e

print_summaries() {
for file in $_S_ROOT/libexec/s-*; do
if [ ! -h $file ]; then
local summary=$(grep "^# Summary:" $file | cut -d ' ' -f3-)
if [ -n "$summary" ]; then
local name=$(basename $file | sed 's/s-//')
echo "$name" | awk '{ printf " %-20s ", $1}'
echo -n $summary
echo
fi
fi
done
}

print_help() {
local usage=$(grep "^# Usage:" $1 | cut -d ' ' -f2-)
local halp="$(awk '/^# Help:/,/^[^#]/' $1 | grep "^#" | sed "s/^# Help: //" | sed "s/^# //" | sed "s/^#//")"

if [ -n "$usage" ]; then
echo $usage
[ -n "$halp" ] && echo && echo "$halp"
else
echo "Sorry, this command isn't documented yet."
fi
}

case "$1" in
"") echo "Usage: s <command> [<args>]
Some useful s commands are:
$(print_summaries)
See 's help <command>' for information on a specific command."
;;
*)
command_path="$(command -v "$_S_ROOT/libexec/s-$1" || true)"
if [ -n "$command_path" ]; then
print_help "$_S_ROOT/libexec/s-$1"
else
echo "s: no such command \`$1'"
fi
esac
82 changes: 82 additions & 0 deletions libexec/s-init
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bash
set -e

print=""
if [ "$1" = "-" ]; then
print=1
shift
fi

shell="$1"
if [ -z "$shell" ]; then
shell="$(basename "$SHELL")"
fi

resolve_link() {
$(type -p greadlink readlink | head -1) $1
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done

pwd
cd "$cwd"
}

root="$(abs_dirname "$0")/.."

if [ -z "$print" ]; then
case "$shell" in
bash )
profile='~/.bash_profile'
;;
zsh )
profile='~/.zshenv'
;;
* )
profile='your profile'
;;
esac

{ echo "# Load s automatically by adding"
echo "# the following to ${profile}:"
echo
echo "eval \"\$(${_S_ROOT}/bin/s init -)\""
echo
} >&2

exit 1
fi

echo "export PATH=\"\${PATH}:${_S_ROOT}/bin\""

case "$shell" in
bash | zsh )
echo "source \"$root/completions/s.${shell}\""
;;
esac

commands=(`s commands --sh`)
IFS="|"
cat <<EOS
s() {
local command="\$1"
if [ "\$#" -gt 0 ]; then
shift
fi
case "\$command" in
${commands[*]})
eval \`s "sh-\$command" "\$@"\`;;
*)
command s "\$command" "\$@";;
esac
}
EOS
4 changes: 4 additions & 0 deletions libexec/s-sh-shell
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e

echo $1
4 changes: 4 additions & 0 deletions share/s/example
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
here's an example file you could read
it has stuff in it
maybe you want some config values
or even some YAML?

0 comments on commit ff8158b

Please sign in to comment.