Permalink
Browse files

Sub stub

  • Loading branch information...
1 parent 82bb719 commit ff6478202b8f91c976712c6658020be8e4e901a3 @pengwynn committed Dec 9, 2012
View
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Sam Stephenson, Nick Quaranto, 37signals
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,5 @@
+# wynn
+
+My fork of [37signals/sub][sub].
+
+[sub]: https://github.com/37signals/sub
View
@@ -0,0 +1,14 @@
+_wynn() {
+ COMPREPLY=()
+ local word="${COMP_WORDS[COMP_CWORD]}"
+
+ if [ "$COMP_CWORD" -eq 1 ]; then
+ COMPREPLY=( $(compgen -W "$(wynn commands)" -- "$word") )
+ else
+ local command="${COMP_WORDS[1]}"
+ local completions="$(wynn completions "$command")"
+ COMPREPLY=( $(compgen -W "$completions" -- "$word") )
+ fi
+}
+
+complete -F _wynn wynn
@@ -0,0 +1,19 @@
+if [[ ! -o interactive ]]; then
+ return
+fi
+
+compctl -K _wynn wynn
+
+_wynn() {
+ local word words completions
+ read -cA words
+ word="${words[2]}"
+
+ if [ "${#words}" -eq 2 ]; then
+ completions="$(wynn commands)"
+ else
+ completions="$(wynn completions "${word}")"
+ fi
+
+ reply=("${(ps:\n:)completions}")
+}
View
@@ -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 _WYNN_ROOT="$(abs_dirname "$libexec_path")"
+export PATH="${libexec_path}:$PATH"
+
+command="$1"
+case "$command" in
+"" | "-h" | "--help" )
+ exec wynn-help
+ ;;
+* )
+ command_path="$(command -v "wynn-$command" || true)"
+ if [ ! -x "$command_path" ]; then
+ echo "wynn: no such command \`$command'" >&2
+ exit 1
+ fi
+
+ shift
+ exec "$command_path" "$@"
+ ;;
+esac
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+# Usage: wynn commands
+# Summary: List all wynn commands
+# Help: This command is mostly used for autocompletion in various shells, and for `wynn help`.
+# Also, this command helps find commands that are named the same as potentially builtin shell commands (which, cd, etc)
+
+set -e
+
+# Provide wynn 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}/wynn-"*; do
+ command="${command##*wynn-}"
+ 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
@@ -0,0 +1,14 @@
+#!/usr/bin/env bash
+set -e
+
+COMMAND="$1"
+if [ -z "$COMMAND" ]; then
+ echo "usage: wynn completions COMMAND [arg1 arg2...]" >&2
+ exit 1
+fi
+
+COMMAND_PATH="$(command -v "wynn-$COMMAND")"
+if grep -i "^# provide wynn completions" "$COMMAND_PATH" >/dev/null; then
+ shift
+ exec "$COMMAND_PATH" --complete "$@"
+fi
View
@@ -0,0 +1,101 @@
+#!/usr/bin/env bash
+set -e
+
+print_summaries() {
+ local commands=()
+ local summaries=()
+ local longest_command=0
+ local command
+
+ for command in $(wynn-commands); do
+ local file="$(command_path "$command")"
+ if [ ! -h "$file" ]; then
+ local summary="$(summary "$file")"
+ if [ -n "$summary" ]; then
+ commands["${#commands[@]}"]="$command"
+ summaries["${#summaries[@]}"]="$summary"
+
+ if [ "${#command}" -gt "$longest_command" ]; then
+ longest_command="${#command}"
+ fi
+ fi
+ fi
+ done
+
+ local index
+ local columns="$(tput cols)"
+ local summary_length=$(( $columns - $longest_command - 5 ))
+
+ for (( index=0; index < ${#commands[@]}; index++ )); do
+ printf " %-${longest_command}s %s\n" "${commands[$index]}" \
+ "$(truncate "$summary_length" "${summaries[$index]}")"
+ done
+}
+
+print_help() {
+ local file="$1"
+ local usage="$(usage "$file")"
+
+ if [ -n "$usage" ]; then
+ echo "$usage"
+
+ local help="$(help "$file")"
+ [ -n "$help" ] && echo && echo "$help"
+ else
+ echo "Sorry, this command isn't documented yet."
+ fi
+}
+
+command_path() {
+ command -v "wynn-$command" || command -v "wynn-sh-$command" || true
+}
+
+summary() {
+ sed -n "s/^# Summary: \(.*\)/\1/p" "$1"
+}
+
+usage() {
+ sed -n "s/^# \(Usage: .*\)/\1/p" "$1"
+}
+
+help() {
+ awk '/^[^#]/{p=0} /^# Help:/{p=1} p' "$1" | sed "s/^# Help: //;s/^# //;s/^#//"
+}
+
+truncate() {
+ local max_length="$1"
+ local string="$2"
+
+ if [ "${#string}" -gt "$max_length" ]; then
+ local length=$(( $max_length - 3 ))
+ echo "${string:0:$length}..."
+ else
+ echo "$string"
+ fi
+}
+
+# Provide wynn completions
+if [ "$1" = "--complete" ]; then
+ exec "wynn-commands"
+ exit
+fi
+
+command="$1"
+case "$command" in
+"") echo "Usage: wynn <command> [<args>]
+
+Some useful wynn commands are:
+$(print_summaries)
+
+See 'wynn help <command>' for information on a specific command."
+;;
+*)
+ file="$(command_path "$command")"
+
+ if [ -n "$file" ]; then
+ print_help "$file"
+ else
+ echo "wynn: no such command \`$command'" >&2
+ exit 1
+ fi
+esac
@@ -0,0 +1,94 @@
+#!/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 wynn automatically by adding"
+ echo "# the following to ${profile}:"
+ echo
+ echo "eval \"\$(${_WYNN_ROOT}/bin/wynn init -)\""
+ echo
+ } >&2
+
+ exit 1
+fi
+
+echo "export PATH=\"\${PATH}:${_WYNN_ROOT}/bin\""
+
+case "$shell" in
+bash | zsh )
+ echo "source \"$root/completions/wynn.${shell}\""
+ ;;
+esac
+
+commands=(`wynn commands --sh`)
+IFS="|"
+cat <<EOS
+_wynn_wrapper() {
+ local command="\$1"
+ if [ "\$#" -gt 0 ]; then
+ shift
+ fi
+
+ case "\$command" in
+ ${commands[*]})
+ eval \`wynn "sh-\$command" "\$@"\`;;
+ *)
+ command wynn "\$command" "\$@";;
+ esac
+}
+EOS
+
+# zsh can't pass argument with aliases, but bash can.
+# zsh can have functions with the name being only numbers, but bash can't.
+# fix both cases here by letting zsh have a function, and bash have its alias.
+case "$shell" in
+bash )
+ echo "alias wynn=_wynn_wrapper"
+ ;;
+zsh )
+ echo "wynn=_wynn_wrapper"
+ ;;
+esac
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -e
+
+echo $1
@@ -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?
View
@@ -26,3 +26,6 @@ compinit
# load every completion after autocomplete loads
for config_file ($ZSH/**/completion.sh) source $config_file
+
+# source sub
+eval "$($DOTFILES/wynn/bin/wynn init -)"

0 comments on commit ff64782

Please sign in to comment.