|
#!/bin/bash |
|
# TARGET_FILE=~/.local/bin/launch |
|
|
|
STORAGE_DB="$HOME/.cache/aziis98.scripts.launch/history.db" |
|
STORAGE_BUFFER="$HOME/.cache/aziis98.scripts.launch/history_buffer" |
|
mkdir -p "$(dirname "$STORAGE_DB")" |
|
|
|
sqlite3 "$STORAGE_DB" <<-EOF |
|
CREATE TABLE IF NOT EXISTS history( |
|
date TEXT, |
|
session TEXT, |
|
command TEXT |
|
); |
|
EOF |
|
|
|
help() { |
|
cat <<-EOF |
|
usage: $(basename "$0") ARGS... |
|
|
|
syntax: |
|
|
|
<SESSION_NAME> '<COMMAND>' |
|
Will $(basename "$0") a background session with the given command. |
|
|
|
list, ls |
|
Prints the list of openned sessions. |
|
(or also "$(basename "$0") ls") |
|
|
|
show <SESSION_NAME> |
|
Attaches to the tmux session. |
|
|
|
history, hist, h |
|
Shows previously launched commands. |
|
|
|
past <SESSION_NAME> |
|
Launch a previously launched session from the stored command. |
|
|
|
--help, -h |
|
Show this help |
|
|
|
EOF |
|
exit 0 |
|
} |
|
|
|
case "$1" in |
|
--help|-h) |
|
help |
|
;; |
|
list|ls) |
|
if [ "$#" -ne 1 ]; then |
|
echo -e "Too many parameters, see $(basename "$0") --help for usage" |
|
exit 1 |
|
fi |
|
tmux ls |
|
exit 0 |
|
;; |
|
history|hist|h) |
|
echo -e "Launch Command History:" |
|
sqlite3 "$STORAGE_DB" <<-EOF |
|
.mode box |
|
SELECT date as "Date", session as "Session", command as "Command" FROM history; |
|
EOF |
|
exit 0 |
|
;; |
|
past) |
|
if [ "$#" -ne 2 ]; then |
|
echo -e "Incorrect number of parameters, see $(basename "$0") --help for usage" |
|
exit 1 |
|
fi |
|
SESSION_NAME="$2" |
|
COMMAND="$(sqlite3 "$STORAGE_DB" <<-EOF |
|
.headers off |
|
.mode tabs |
|
SELECT command FROM history WHERE session = "$SESSION_NAME" LIMIT 1; |
|
EOF |
|
)" |
|
tmux new -d -s "$SESSION_NAME" "$COMMAND" |
|
echo -e "Created tmux session \"$SESSION_NAME\" with (previous) command" |
|
echo |
|
echo -e " $COMMAND" |
|
echo |
|
echo -e "reattach with \"$(basename "$0") show $SESSION_NAME\"" |
|
exit 0 |
|
;; |
|
show) |
|
if [ "$#" -ne 2 ]; then |
|
echo -e "Incorrect number of parameters, see $(basename "$0") --help for usage" |
|
exit 1 |
|
fi |
|
tmux attach -t "$2" |
|
exit 0 |
|
;; |
|
esac |
|
|
|
if [ "$#" -ne 2 ]; then |
|
echo -e "Too many parameters, see $(basename "$0") --help for usage" |
|
exit 1 |
|
fi |
|
|
|
tmux new -d -s "$1" "$2" |
|
|
|
printf "$(date +'%Y-%m-%d %H:%M:%S')\t$1\t$2" > "$STORAGE_BUFFER" |
|
sqlite3 "$STORAGE_DB" <<-EOF |
|
.separator "\t" |
|
.import $STORAGE_BUFFER history |
|
EOF |
|
|
|
echo -e "Created tmux session \"$1\" with command" |
|
echo |
|
echo -e " $2" |
|
echo |
|
echo -e "reattach with \"$(basename "$0") show $1\"" |
|
|