Skip to content

Instantly share code, notes, and snippets.

@s-n-g
Last active August 29, 2024 23:58
Show Gist options
  • Save s-n-g/2f1ef5c764222d26e5bb0075b2adddb1 to your computer and use it in GitHub Desktop.
Save s-n-g/2f1ef5c764222d26e5bb0075b2adddb1 to your computer and use it in GitHub Desktop.
A helper script to execute pyradio (https://github.com/coderholic/pyradio) through tmux
Installation
------------
1. Download the script and place it in a directory in your PATH
2. Make it executable
chmod +x tmux_pyradio
3. Execute
tmux_pyradio -h
to get started...
You are done!
Enjoy!
#!/usr/bin/env bash
# set -x
# A helper script to execute pyradio through tmux
# This script is part of pyradio - https://github.com/coderholic/pyradio
#
# Copyright 2024, S. Georgaras <sng@hellug.gr>
#
# 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.
#
function print_usage(){
echo -e "\033[32m${1}\033[0m - A helper script to execute \033[31mpyradio\033[0m through \033[33mtmux\033[0m
Usage: \033[32m${1}\033[0m [\033[36mtmux session name\033[0m] -- [\033[36mpyradio parameters\033[0m]
When invoked without any parameters, the script will execute \033[33mtmux\033[0m, creating
a session called \"\033[35mpyradio\033[0m\" and execute \033[31mpyradio\033[0m without any parameters.
If you need to give a name for the \033[33mtmux\033[0m session, pass it as a parameter to
the script and add a \"--\" to the command line.
Example:
\033[32m${1}\033[0m \033[35mmy_session\033[0m --
This will create a \033[33mtmux\033[0m session called \"\033[35mmy_session\033[0m\" and execute \033[31mpyradio\033[0m
without any arguments.
If you also want to pass parameters to \033[31mpyradio\033[0m, add them after the \"--\".
Example:
\033[32m${1}\033[0m \033[35mmy_session\033[0m -- \033[36m-p 5\033[0m
This will create a \033[33mtmux\033[0m session called \"\033[35mmy_session\033[0m\" and execute \033[31mpyradio\033[0m
with the argument \"\033[36m-p 5\033[0m\".
If you just want to pass arguments to \033[31mpyradio\033[0m, without specifying a \033[33mtmux\033[0m
session name, just add them to the command line.
Example:
\033[32m${1}\033[0m \033[36m-p 5\033[0m
Finally, since \033[33mtmux\033[0m will exit and clear the screen as soon as \033[31mpyradio\033[0m exits,
the output will not be available to the user. This may be a problem in case
33[31mpyradio\033[0m crashes or prints info to the screen (using the \033[32m-h\033[0m, \033[32m-ls\033[0m or \033[32m-l\033[0m
command line parameters, for example.)
To avoid this behavior, you can use the \033[32m-d\033[0m parameter, before the \"--\" token.
This will force the script to pause before \033[33mtmux\033[0m terminates so that the output
will be prserved until \033[33mENTER\033[0m is pressed.
For example, to create a \033[33mtmux\033[0m session called \"\033[35mmy_session\033[0m\", execute \033[31mpyradio\033[0m
with the argument \"\033[36m-ls\033[0m\" (list playlists) and pause before \033[33mtmux\033[0m exits, you
would insert the command:
\033[32m${1}\033[0m \033[35mmy_session\033[0m \033[33m-d\033[0m -- \033[36m-ls\033[0m
"
}
# find a suitable location for the temporary script
[ -w /tmp ] && SCRIPT=/tmp/pyradio.$$
[ -z "${SCRIPT}" ] && [ -w ~/tmp ] && SCRIPT=~/tmp/pyradio.$$
[ -z "${SCRIPT}" ] && SCRIPT=~/pyradio.$$
#############################################################################
# Start of args parsing
#############################################################################
# Initialize variables
BEFORE_TOKEN=()
AFTER_TOKEN=()
found_token=false
# Loop through all the arguments
for arg in "$@"; do
if [[ "$arg" == "--" ]]; then
found_token=true
elif [ "$found_token" = false ] && [ "$arg" == "-d" ]; then
READ_LINE='read'
elif [ "$found_token" = true ]; then
[ "$arg" == "-h" ] && print_usage $(basename $0) && exit
AFTER_TOKEN+=("$arg")
else
[ "$arg" == "-h" ] && print_usage $(basename $0) && exit
BEFORE_TOKEN+=("$arg")
fi
done
# Convert arrays to strings for output
BEFORE_TOKEN_STRING="${BEFORE_TOKEN[*]}"
AFTER_TOKEN_STRING="${AFTER_TOKEN[*]}"
if [ "${found_token}" = false ]
then
# token not found
AFTER_TOKEN_STRING="${BEFORE_TOKEN_STRING}"
BEFORE_TOKEN_STRING="pyradio"
fi
#############################################################################
# End of args parsing #
#############################################################################
# create the temporary script
echo "#!/bin/bash
tmux set-option status off
pyradio ${AFTER_TOKEN_STRING}
${READ_LINE}" > ${SCRIPT}
# make the temporary script executable
chmod +x ${SCRIPT}
# make sure we have a sexxion name
[ -z "${BEFORE_TOKEN_STRING}" ] && BEFORE_TOKEN_STRING=pyradio
# execute tmux
tmux new-session -s ${BEFORE_TOKEN_STRING} ${SCRIPT}
# finally, delete the temporary script
rm ${SCRIPT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment