Skip to content

Commit

Permalink
feat(dotfiles): add dog utility
Browse files Browse the repository at this point in the history
Continuing in the tradition of `portool` and `gg`, this is `dog`. At the
moment, it doesn't do much at all (just `go` to jump to a tmux session
and `help` to show help). I resurrected the completion from the last
tool, but note that for now there are no commands that take file
arguments; that bit is commented out for now because I don't want to
have to look it up again in the future when I have such a subcommand.
  • Loading branch information
wincent committed Jun 12, 2024
1 parent e45aadf commit b04972b
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 17 deletions.
27 changes: 27 additions & 0 deletions aspects/dotfiles/files/.zsh/completions/_dog
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#compdef dog

typeset -A opt_args

_arguments -C \
'1:cmd:->cmds' \
'*:: :->args' \
&& ret=0

case "$state" in
(cmds)
local commands; commands=(
"go:Go to (or start) tmux session"
"help:Show help"
)
_describe -t commands 'command' commands && ret=0
;;
# (args)
# case $line[1] in
# (command_that_takes_a_file_argument)
# _files && ret=0
# ;;
# esac
# ;;
esac

return $ret
8 changes: 8 additions & 0 deletions aspects/dotfiles/files/.zsh/datadog/bin/dog
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Wrapper script to stop recent Node versions from complaining about:
# TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for gg

LIB=$(cd $(dirname "$BASH_SOURCE")/..; echo "$(pwd)/lib")

exec node "$LIB/dog/main.js" "$@"
31 changes: 31 additions & 0 deletions aspects/dotfiles/files/.zsh/datadog/lib/dog/helpers/.common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Based on: https://stackoverflow.com/a/8351489/2103996
backoff() {
local max_attempts=${ATTEMPTS-5}
local timeout=${TIMEOUT-1}
local attempt=1
local exitCode=0

while (( $attempt < $max_attempts ))
do
if "$@"
then
return 0
else
exitCode=$?
fi

echo "error: retrying in $timeout..." 1>&2
sleep $timeout
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
done

if [[ $exitCode != 0 ]]
then
echo "error: maximum attempt count exceeded ($@)" 1>&2
fi

return $exitCode
}
35 changes: 35 additions & 0 deletions aspects/dotfiles/files/.zsh/datadog/lib/dog/helpers/go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

set -e

SESSION=web-ui
BASE="$HOME/dev/web-ui"

if [ ! -d "$BASE" ]; then
echo "error: Cannot find BASE directory"
exit 1
fi

if tmux has-session -t $SESSION 2> /dev/null; then
TARGET="$SESSION"
else
TARGET="$SESSION:vim.right"

cd "$BASE"

# 1. Main editor window: Vim on the left, shell on the right.
tmux new-session -d -s $SESSION -n vim -x $(tput cols) -y $(tput lines)
tmux split-window -t $SESSION:vim -h
tmux send-keys -t $SESSION:vim.left "vim -c CommandT" Enter

# 2. General shell use: two panes separated by a vertical split.
tmux new-window -t $SESSION -n zsh
tmux split-window -t $SESSION:zsh -h

fi

if [ -n "$TMUX" ]; then
tmux switch-client -t "$TARGET"
else
tmux attach -t "$TARGET"
fi
7 changes: 7 additions & 0 deletions aspects/dotfiles/files/.zsh/datadog/lib/dog/helpers/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

echo "Usage: dog <subcommand> [...arguments]"
echo
echo "Subcommands:"
echo " go 🚗 go to (or start a) tmux session"
echo " help 🤷 show this help"
Loading

0 comments on commit b04972b

Please sign in to comment.