Skip to content

Instantly share code, notes, and snippets.

@Ajnasz
Last active July 10, 2024 16:53
Show Gist options
  • Save Ajnasz/4f1586775d4c6edd3666fc376d4b3296 to your computer and use it in GitHub Desktop.
Save Ajnasz/4f1586775d4c6edd3666fc376d4b3296 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# This script extends the functionality of dmenu_run by allowing the user to
# include custom commands and executables. The custom commands are read from a
# file specified by the environment variable DMENU_RUN_COMMANDS, and the
# executables are read from a directory specified by the environment variable
# DMENU_RUN_BIN.
#
# Environment Variables:
# DMENU_RUN_COMMANDS: Path to a file containing custom commands to be included
# in the dmenu. The file should contain one command per line.
#
# DMENU_RUN_BIN: Path to a directory containing executables.
# The executables should echo the commands to be included in
# the dmenu.
#
# Usage:
# Set the environment variables DMENU_RUN_COMMANDS and DMENU_RUN_BIN to point
# to your custom commands file and executables directory, respectively. Then,
# run the script. The script will add the custom commands and executables to
# the dmenu.
#
# Example:
# DMENU_RUN_COMMANDS=/path/to/commands.txt DMENU_RUN_BIN=/path/to/bin \
# ./dmenu_run2
#
# Note:
# The script assumes that all files in the DMENU_RUN_BIN directory are
# executable and that they will echo the correct commands. Ensure that these
# conditions are met to avoid unexpected behavior.
#
# The script runs in the background, and the selected command from the dmenu
# is executed in a subshell.
if [ -z "$DMENU_RUN_BIN" ] || [ ! -d "$DMENU_RUN_BIN" ];then
echo "DMENU_RUN_BIN is not set or is not a directory" >&2
fi
if [ -z "$DMENU_RUN_COMMANDS" ] || [ ! -f "$DMENU_RUN_COMMANDS" ];then
echo "DMENU_RUN_COMMANDS is not set or is not a file" >&2
fi
{
# always include dmenu_path
dmenu_path;
# include output of executables from DMENU_RUN_BIN
if [ -n "$DMENU_RUN_BIN" ] && [ -d "$DMENU_RUN_BIN" ]; then
find "$DMENU_RUN_BIN" -maxdepth 1 -type f -executable -exec {} \;
fi
# include custom commands from DMENU_RUN_COMMANDS
if [ -n "$DMENU_RUN_COMMANDS" ] && [ -f "$DMENU_RUN_COMMANDS" ]; then
cat "$DMENU_RUN_COMMANDS"
fi
} | dmenu "$@" | ${SHELL:-"/usr/bin/env sh"} &
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment