Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions desk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Usage:
Activate a desk. Extra arguments are passed onto shell. If called with
no arguments, look for a Deskfile in the current directory. If not a
recognized desk, try as a path to directory containing a Deskfile.
$PROGRAM path <desk-name>
Returns the path for a desk's environment.
$PROGRAM run <desk-name> <cmd>
Run a command within a desk's environment then exit. Think '\$SHELL -c'.
$PROGRAM edit [desk-name]
Expand Down Expand Up @@ -101,27 +103,7 @@ cmd_go() {
# - empty -> `./Deskfile`
#
local TODESK="$1"
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}" 2>/dev/null)"

local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}"
if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then
POSSIBLE_DESKFILE_DIR="."
fi

# If nothing could be found in $DESKS/, check to see if this is a path to
# a Deskfile.
if [[ -z "$DESKPATH" && -d "$POSSIBLE_DESKFILE_DIR" ]]; then
if [ ! -f "${POSSIBLE_DESKFILE_DIR}/${DESKFILE_NAME}" ]; then
echo "No Deskfile found in '${POSSIBLE_DESKFILE_DIR}'"
exit 1
fi

local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd )
DESKPATH="${REALPATH}/${DESKFILE_NAME}"
TODESK=$(basename "$REALPATH")
fi

local DESKPATH=$(get_desk_path $TODESK)
# Shift desk name so we can forward on all arguments to the shell.
shift;

Expand All @@ -135,6 +117,17 @@ cmd_go() {
}


cmd_path() {
local DESKNAME=$1
local DESKPATH=$(get_desk_path $DESKNAME)
if [ -z "$DESKPATH" ]; then
exit 1
else
echo ${DESKPATH}
fi
}


cmd_run() {
local TODESK="$1"
shift;
Expand Down Expand Up @@ -272,6 +265,32 @@ get_callables() {
| sed -E "s/function (${FNAME_CHARS}+).*/\1/"
}

get_desk_path() {
local TODESK=$1
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}" 2>/dev/null)"

local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}"
if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then
POSSIBLE_DESKFILE_DIR="."
fi

# If nothing could be found in $DESKS/, check to see if this is a path to
# a Deskfile.
if [[ -z "$DESKPATH" && -d "$POSSIBLE_DESKFILE_DIR" ]]; then
if [ ! -f "${POSSIBLE_DESKFILE_DIR}/${DESKFILE_NAME}" ]; then
echo "No Deskfile found in '${POSSIBLE_DESKFILE_DIR}'"
exit 1
fi

local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd )
DESKPATH="${REALPATH}/${DESKFILE_NAME}"
TODESK=$(basename "$REALPATH")
fi

echo $DESKPATH
}

get_running_shell() {
# Echo the name of the parent shell via procfs, if we have it available.
# Otherwise, try to use ps with bash's parent pid.
Expand Down Expand Up @@ -326,6 +345,7 @@ case "$1" in
version|--version) shift; cmd_version "$@" ;;
ls|list) shift; cmd_list "$@" ;;
go|.) shift; cmd_go "$@" ;;
path) shift; cmd_path "$@" ;;
run) shift; cmd_run "$@" ;;
edit) shift; cmd_edit "$@" ;;
*) cmd_current "$@" ;;
Expand Down