Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Starting off s

  • Loading branch information...
commit ff8158b5d35c0ef02a25b13dc32707dbde3ca4c3 0 parents
Steve Klabnik authored
1  bin/s
14 completions/s.bash
@@ -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 completions/s.zsh
@@ -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 libexec/s
@@ -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 libexec/s-commands
@@ -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 libexec/s-completions
@@ -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 libexec/s-help
@@ -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 libexec/s-init
@@ -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 libexec/s-sh-shell
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -e
+
+echo $1
4 share/s/example
@@ -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?
Please sign in to comment.
Something went wrong with that request. Please try again.