Skip to content

Commit

Permalink
Merge pull request #25 from jamesob/pr-23
Browse files Browse the repository at this point in the history
Desk works with fish
  • Loading branch information
jamesob committed Nov 3, 2015
2 parents 09fdbe4 + c07ad52 commit 71785ae
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ before_install:
- cp ./usr/bin/shellcheck .

env:
- PATH=$PATH:$(pwd) SHELL_CMD='-c ./run_tests.sh'
- PATH=$PATH:$(pwd)

script: make lint test bash zsh
script:
- make lint
- SHELL_CMD='-c ./run_tests.sh' make bash zsh
- SHELL_CMD='-c ./run_tests.fish' make fish
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ ADD desk /usr/local/bin/desk
ADD test/zshrc .zshrc
ADD test/bashrc .bashrc
ADD test/run_tests.sh run_tests.sh
ADD test/run_tests.fish run_tests.fish
ADD examples examples
RUN chown -R $USERNAME:$USERNAME .zshrc examples run_tests.sh .bashrc
RUN mkdir -p .config/fish && touch .config/fish/config.fish
RUN chown -R $USERNAME:$USERNAME .zshrc examples run_tests.fish run_tests.sh .bashrc .config

USER $USERNAME
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ fish: dockerbuild
.PHONY: lint
lint:
shellcheck -e SC2155 desk

.PHONY: test
test:
desk --help
57 changes: 44 additions & 13 deletions desk
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,46 @@ cmd_init() {
echo "${NEW_PREFIX} doesn't exist, attempting to create."
mkdir -p "$NEW_PREFIX/desks"
fi
case "$(get_running_shell)" in

local SHELLTYPE=$(get_running_shell)

case "${SHELLTYPE}" in
bash) local SHELLRC="${HOME}/.bashrc" ;;
fish) local SHELLRC="${HOME}/.config/fish/config.fish" ;;
zsh) local SHELLRC="${HOME}/.zshrc" ;;
esac

read -p "Where's your shell rc file? (default: ${SHELLRC}): " \
USER_SHELLRC
[ -z "${USER_SHELLRC}" ] && USER_SHELLRC="$SHELLRC"
if [ ! -f "$USER_SHELLRC" ]; then
echo "${USER_SHELLRC} doesn't exist"
exit 1
fi

echo "# Hook for desk activation" >> "$USER_SHELLRC"
echo "[ ! -z \"\$DESK_ENV\" ] && source \"\$DESK_ENV\"" >> "$USER_SHELLRC"

if [ "$SHELLTYPE" == "fish" ]; then
echo "test -n \"\$DESK_ENV\"; and . \"\$DESK_ENV\"" >> "$USER_SHELLRC"
else
echo "[ -n \"\$DESK_ENV\" ] && source \"\$DESK_ENV\"" >> "$USER_SHELLRC"
fi

echo "Done. Start adding desks to ${NEW_PREFIX}/desks!"
}


cmd_go() {
local TODESK="$1"
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}".sh)"
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}")"

if [ -z "$DESKPATH" ]; then
echo "Desk" "$TODESK" "not found in" "$DESKS"
echo "Desk $TODESK (${TODESK}${DESKEXT}) not found in $DESKS"
exit 1
else
DESK_NAME="${TODESK}" DESK_ENV="${DESKPATH}" $SHELL
local SHELL_EXEC="$(get_running_shell)"
DESK_NAME="${TODESK}" DESK_ENV="${DESKPATH}" "${SHELL_EXEC}"
fi
}

Expand All @@ -86,8 +100,10 @@ cmd_list() {
exit 1
fi

find "${DESKS}/" -name '*.sh' -print0 | while read -d '' -r f; do
local name=$(basename "${f/.sh//}")
local DESKEXT=$(get_deskfile_extension)

find "${DESKS}/" -name "*${DESKEXT}" -print0 | while read -d '' -r f; do
local name=$(basename "${f/${DESKEXT}//}")
local desc=$(echo_description "$f")

if [ -z "$desc" ]; then
Expand All @@ -101,20 +117,23 @@ cmd_list() {

cmd_current() {
local DESKPATH=$DESK_ENV
local DESK_NAME=${DESKPATH##*/}
local DESK_NAME=${DESK_NAME%.*}
if [ -z "$DESKPATH" ]; then
echo "No desk activated."
exit 2
else
basename "${DESKPATH/.sh//}"
echo "$DESK_NAME"
echo_description "$DESKPATH"
local CALLABLES=$(get_callables "$DESKPATH")

[ -z "$CALLABLES" ] || echo ""

for NAME in $CALLABLES; do
# Last clause in the grep regexp accounts for fish functions.
local DOCLINE=$(
grep -B 1 -E \
"^(alias ${NAME}=|(function )?${NAME}( )?\()" "$DESKPATH" \
"^(alias ${NAME}=|(function )?${NAME}\()|function $NAME" "$DESKPATH" \
| grep "#")

if [ -z "$DOCLINE" ]; then
Expand All @@ -128,21 +147,24 @@ cmd_current() {

## Utilities

FNAME_CHARS='[a-zA-Z0-9_-]'

# Echo the description of a desk. $1 is the deskfile.
echo_description() {
local descline=$(grep -E "#\s+Description" "$1")
echo "${descline##*Description: }"
}

# Return a list of aliases and functions for a given desk
# Echo a list of aliases and functions for a given desk
get_callables() {
local DESKPATH=$1
grep -E "^(alias |(function )?[a-zA-Z0-9_-]+( )?\()" "$DESKPATH" \
grep -E "^(alias |(function )?${FNAME_CHARS}+\()|function $NAME" "$DESKPATH" \
| sed 's/alias \([^= ]*\)=.*/\1/' \
| sed -E 's/(function )?([a-zA-Z0-9]*)\(\).*/\2/'
| sed -E "s/(function )?(${FNAME_CHARS}+)\(\).*/\2/" \
| sed -E "s/function (${FNAME_CHARS}+).*/\1/"
}

# Retrieve the name of the parent shell via procfs, if we have it available.
# Echo the name of the parent shell via procfs, if we have it available.
get_running_shell() {
# Get cmdline procfile of the process running this script.
local CMDLINE_FILE="/proc/$(grep PPid /proc/$$/status | cut -f2)/cmdline"
Expand All @@ -156,6 +178,15 @@ get_running_shell() {
fi
}

# Echo the extension of recognized deskfiles (.fish for fish)
get_deskfile_extension() {
if [ "$(get_running_shell)" == "fish" ]; then
echo '.fish'
else
echo '.sh'
fi
}


PROGRAM="${0##*/}"

Expand Down
6 changes: 6 additions & 0 deletions examples/hello.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Description: a simple test for the fish shell.

# Args: <hello_to>. Say hello to someone.
function say_hello
echo Hello $argv
end
17 changes: 17 additions & 0 deletions test/run_tests.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/fish
printf "\
" | desk init

cp examples/* ~/.desk/desks/

set LIST (desk ls)
echo $LIST | grep "hello" >/dev/null
test $status -ne 0; and echo "hello desk not found"; and exit 1

set CURRENT (env DESK_ENV=$HOME/.desk/desks/hello.fish desk)
echo $CURRENT | grep "say_hello - Args: <hello_to>. Say hello to someone." >/dev/null
test $status -ne 0; and echo "say_hello command not found"; and exit 1

exit 0
2 changes: 2 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

# Run automated tests to ensure desk is behaving as expected

ensure() {
Expand Down

0 comments on commit 71785ae

Please sign in to comment.