From cc43eb93ebca3371e57210ab87a8d24acf64bf91 Mon Sep 17 00:00:00 2001 From: Nikita Brancatisano Date: Sat, 23 Apr 2022 22:22:45 +0200 Subject: [PATCH] feat(def): Add def Plugin --- plugins/def/README.md | 26 +++++++++++++ plugins/def/def.plugin.zsh | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 plugins/def/README.md create mode 100755 plugins/def/def.plugin.zsh diff --git a/plugins/def/README.md b/plugins/def/README.md new file mode 100644 index 000000000..f49a51e7e --- /dev/null +++ b/plugins/def/README.md @@ -0,0 +1,26 @@ +### def +This plugin is used to specify and run a default command in any directory of your choice + +To use it you can either run `def init` to create the default config file or create a `.def` file on a folder by folder basis + +## Configuration +The default configuration file is stored in `$XDG_CONFIG_HOME/def/`. If `$XDG_CONFIG_HOME` is not set then it defaults to `$HOME/.config` + +The structure of the file is a folder with relative command for each line. The folders can be specified as regex. The ~ character is automatically expanded to the `$HOME` directory + +If you use a local `.def` file then it should only hold the command to be executed + +# Example +``` +/home/vinter/frontend npm run +^/home/vinter/projects/go go build +~/stuff/git git pull +``` + +## Additional commands +You can add a command for the current folder by using `def add [command]`. +``` +def add npm run +``` + +You can remove any commands that match the current folder by using `def remove`. Please note that it **will also remove** any local `.def` files. diff --git a/plugins/def/def.plugin.zsh b/plugins/def/def.plugin.zsh new file mode 100755 index 000000000..4e69eb318 --- /dev/null +++ b/plugins/def/def.plugin.zsh @@ -0,0 +1,77 @@ +### A zsh plugin that executes a default command specified in the def.config file. +def () { + if [ -z "$XDG_CONFIG_HOME" ]; then + export XDG_CONFIG_HOME="$HOME/.config" + fi + local dir="$(pwd)" + + if [ "$1" = "init" ]; then + ### If there's no def.config file, then we create one. + if [ ! -f "$XDG_CONFIG_HOME/def/def.config" ]; then + mkdir -p "$XDG_CONFIG_HOME"/def + touch "$XDG_CONFIG_HOME/def"/def.config + echo "A def.config file was created in $XDG_CONFIG_HOME/def." + return + fi + echo "A 'def.config' file already exists in $XDG_CONFIG_HOME." + return + fi + + if [ "$1" = "add" ]; then + if [ ! -f "$XDG_CONFIG_HOME/def/def.config" ]; then + echo "No default config file found. Create one in with the 'def init' command." + return + fi + echo "$dir ${@:2}" >> "$XDG_CONFIG_HOME/def/def.config" + echo "Added '${@:2}' to the default config file." + return + fi + + if [ "$1" = "remove" ]; then + if [ ! -f "$XDG_CONFIG_HOME/def/def.config" ]; then + echo "No default config file found. Create one in with the 'def init' command" + return + fi + if [ -f $dir/.def ]; then + rm $dir/.def + echo "Removed local .def file" + fi + while read line; do + if [[ $line =~ ^$dir ]]; then + sed -i "\%$line%d" "$XDG_CONFIG_HOME/def/def.config" + echo "Removed '$line' from the default config file." + fi + done < "$XDG_CONFIG_HOME/def/def.config" + return + fi + + if [ -z "$1" ]; then + if [ -f $dir/.def ] && [ "$dir" != "$XDG_CONFIG_HOME" ]; then + . $dir/.def + else + if [ -f $XDG_CONFIG_HOME/def/def.config ]; then + while read line; do + if [ "$line" != "" ]; then + local directory=$(echo $line | cut -d' ' -f1) + local command=$(echo $line | cut -d' ' -f2-) + + if [[ $directory == *"~"* ]]; then + directory=$(echo $directory | sed 's%~%'$HOME'%g') + fi + + if [[ $dir =~ $directory ]]; then + echo $(eval $command) + return + fi + fi + done < $XDG_CONFIG_HOME/def/def.config + echo "No default command found for the current directory" + else + echo "No default config file found. Create one in with the 'def init' command dmdmd." + fi + fi + else + echo "Unknown command '$1'" + fi +} +