Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created July 15, 2024 02:19
Show Gist options
  • Save Silva97/e0067eaf31f4983ba301b8589113b1e9 to your computer and use it in GitHub Desktop.
Save Silva97/e0067eaf31f4983ba301b8589113b1e9 to your computer and use it in GitHub Desktop.
Just a script to shortcuts (poorly tested)
#!/bin/bash
if ! which yad >/dev/null; then
echo "The 'yad' is required to run this script. Try:" >&2
echo " sudo apt install yad" >&2
exit 1
fi
CONFIG_FILE=~/.chorts
LOG_FILE=~/.chorts.log
function show_help() {
cat <<:EOF
Developed by Luiz Felipe Silva <silva97@freedev.com.br>
Make your custom shortcuts and run it easy.
USAGE
chorts <action>
ACTIONS
help Show this help message.
gui Run the GUI dialog to select and run a shortcut.
example Show an example of configuration file.
FILES PATH
Configuration file: $CONFIG_FILE
Logs file: $LOG_FILE
CONFIGURATION FILE
The configuration file use the following syntax:
Shortcut name: command
Where "Shortcut name" is the name displayed for the shortcut, and "command"
is the command that will be run on terminal when you run this shortcut.
You should define one shortcut per line.
To see an example of content for this file, run:
$ chorts example
GUI
The displayed GUI will list the shortcuts on the same order defined on the
configuration file. To search a command, just type the name of the
command.
The column "Command" can be edited before run the shortcut. You just need
to double-click the command, make your changes and then press enter.
:EOF
}
function main() {
local action="$1"
local action_function="run_$action"
if [ -z "$action" ] || [[ "$action" =~ ^(help|--help|-h)$ ]]; then
show_help
exit 0
fi
if [ "$(type -t "$action_function")" != "function" ]; then
echo "Invalid action '$action'!" >&2
echo "See help: chorts help" >&2
exit 1
fi
shift 1
$action_function "$@"
}
function run_example() {
cat <<:EOF
Edit shortcuts: gnome-text-editor ~/.chorts
Hello world: echo "Hello World!"
Complex command: echo You can use && echo any valid :shell: command && echo \$USER
Really complex command: ~/scripts/my-script.sh --options --etc foobar
:EOF
}
function run_gui() {
if [ ! -s "$CONFIG_FILE" ]; then
yad \
--title="Configure your '$CONFIG_FILE' file" \
--text="File '$CONFIG_FILE' doesn't exists or is empty. Please, configure your shortcuts first.\n\nRun: chorts help" \
--button="OK:0" \
--width=400 \
--height=50
exit 0
fi
command=$(sed -E 's/([^:]+):\s*(.+)/\1\n\2/' "$CONFIG_FILE" \
| yad \
--list \
--title="Choice the shortcut" \
--column="Name" \
--column="Command" \
--no-markup \
--width=800 \
--height=600 \
--search-column=1 \
--print-column=2 \
--editable \
--editable-cols=2 \
--expand-column=1 \
--separator "")
if [ -z "$command" ]; then
exit 0
fi
{
echo "$(date +"%m/%d/%Y %H:%M:%S") running:"
echo "\$ $command"
} >> "$LOG_FILE"
(eval "$command" 2>&1) | tee -a "$LOG_FILE"
echo -e "\n" >> "$LOG_FILE"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment