Skip to content

Commit 2aa2b29

Browse files
committed
Initial commit and release
0 parents  commit 2aa2b29

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.unwrap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dont-delete-me
2+
or me
3+
especially_not_me

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Unwrap: A plugin for ZSH
2+
3+
This simple plugin allows you to remove a directory without removing its contents.
4+
5+
## Usage
6+
7+
In your `zsh` command line, navigate into the directory you want to unwrap,
8+
then type `unwrap`. You will be given a prompt to confirm the action and if you
9+
confirm, unwrap will remove the current directory and move all of its contents
10+
into the parent folder.
11+
12+
## Installation
13+
14+
If you are using oh-my-zsh:
15+
16+
Copy the branch folder to `.oh-my-zsh/custom/plugins`, for example:
17+
18+
```bash
19+
cd ~/.oh-my-zsh/custom/plugins
20+
git clone [email protected]:foxliegh81/unwrap-zsh-plugin.git unwrap
21+
```
22+
23+
Enable the plugin in your `.zshrc` file. Edit `~/.zshrc` to activate the plugin, for example: `plugins=(unwrap)`
24+
And restart the terminal to apply (or just type `source ~/.zshrc`).
25+
26+
27+
## Protected Directories
28+
29+
Unwrap will not remove any directories that are in the root of your system
30+
(or any directory with the same name as one of those folders). Nor will it
31+
remove the users home directory.
32+
33+
You can add custom-protected directories by creating an `.unwrap` file in your
34+
home directory. The file should contain a list of directories to protect.
35+
36+
An example of a `.unwrap` file can be found in the plugin install directory.
37+
38+
If your folder is not covered by the above, use `unwrap` with caution.
39+
40+
### A warning about folder names
41+
42+
`Unwrap` protection is case-sensitive and will not protect folders that have a space, dash
43+
or other special character in their name. It will, however, work fine with
44+
folders that have an underscore in their name.
45+

unwrap.plugin.zsh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/zsh
2+
# Copies the contents of the current directory into the parent
3+
# directory and then deletes the current directory
4+
unwrap() {
5+
local this_dir=`basename $PWD`;
6+
local sys_dirs=`ls /`;
7+
local in_sys_dir=`echo ${sys_dirs[@]} | grep -o $this_dir | wc -w`;
8+
local user_protected_dirs=`cat ~/.unwrap`
9+
local in_user_protected_dirs=`echo ${user_protected_dirs[@]} | grep -o $this_dir | wc -w`;
10+
11+
# Check for dangerous directories
12+
if [[ $this_dir == '/' ]]; then
13+
echo "\033[38;5;124mRoot directory detected. Aborting.";
14+
return 0;
15+
elif [[ in_sys_dir -gt 0 ]]; then
16+
echo "\033[38;5;124mSystem directory detected. Aborting.";
17+
return 0;
18+
elif [[ in_user_protected_dirs -gt 0 ]]; then
19+
echo "\033[38;5;124mUser protected directory detected. Aborting.";
20+
return 0;
21+
elif [[ $this_dir == `basename $HOME` ]]; then
22+
echo "\033[38;5;124mHome directory detected. Aborting.";
23+
return 0;
24+
else
25+
# The usual suspect have been eliminated. Proceed with a warning.
26+
echo "\n\033[38;5;124mWarning:\n"
27+
echo "\033[38;5;024mThis will move all files into the parent directory"
28+
echo "and then delete this folder.\n"
29+
echo "\033[38;5;124mExisting files will be overwritten.\n"
30+
31+
echo -n "\033[38;5;024mDo you wish to proceed? (y/N)\n\033[0m";
32+
33+
read response;
34+
35+
# Check to see if the directory is empty
36+
if [[ $response == "y" ]]; then
37+
if [ -z "$(ls -A)" ]; then
38+
# Empty directory. Move into parent directory and delete
39+
echo "\n⚙️ \033[38;5;144m No files to move. Proceeding.\033[0m";
40+
else
41+
echo "\n⚙️ \033[38;5;144m Moving files"
42+
cp -r * ../
43+
fi
44+
echo "\n✅ \033[38;5;118mFiles moved, removing directory"
45+
cd ..
46+
rm -rf $this_dir
47+
fi
48+
return 1
49+
fi
50+
}

0 commit comments

Comments
 (0)