1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-23 14:20:08 +00:00

feat(def): Add def Plugin

This commit is contained in:
Nikita Brancatisano 2022-04-23 22:22:45 +02:00
parent a879ff1515
commit cc43eb93eb
2 changed files with 103 additions and 0 deletions

26
plugins/def/README.md Normal file
View file

@ -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.

77
plugins/def/def.plugin.zsh Executable file
View file

@ -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
}