mikedacre / careful_rm Goto Github PK
View Code? Open in Web Editor NEWA safe wrapper for rm that adds useful warnings and an optional recycle/trash mode
License: MIT License
A safe wrapper for rm that adds useful warnings and an optional recycle/trash mode
License: MIT License
The code works fine on Mac, but it is slow because applescript is slow, I think there should be a way to bypass applescript by editing the .DS_Store
files, but I haven't bothered yet as there isn't good documentation on how this even works.
Passing in -d
still prompts to include the -r
flag despite the target directory being empty. For example:
$ mkdir empty
$ rm -d empty
Directories ['empty'] included but -r not sent
Add -r, ignore dirs, or cancel? [add/ignore/CANCEL] ignore
No files or folders to delete
It should be pretty easy to add a --shred
mode that forces off recycling, calls shred
and then rm.
With the plugin in my zshrc my startup timings look something like this:
num calls time self name
-----------------------------------------------------------------------------------
1) 1 336.87 336.87 41.83% 336.87 336.87 41.83% chpwd_trash
2) 1 778.28 778.28 96.65% 108.21 108.21 13.44% zgen-init
3) 1 83.84 83.84 10.41% 83.69 83.69 10.39% _zsh_nvm_lazy_load
4) 1 59.25 59.25 7.36% 59.25 59.25 7.36% _zgen-check-for-updates
5) 4 131.80 32.95 16.37% 47.98 12.00 5.96% pmodload
6) 2 47.20 23.60 5.86% 47.20 23.60 5.86% compinit
7) 3 26.29 8.76 3.26% 26.29 8.76 3.26% mkdir
8) 1 21.75 21.75 2.70% 21.52 21.52 2.67% _zsh_highlight_load_highlighters
9) 1 18.32 18.32 2.28% 18.32 18.32 2.28% prompt_pure_state_setup
10) 2 18.14 9.07 2.25% 18.14 9.07 2.25% promptinit
11) 1 11.00 11.00 1.37% 11.00 11.00 1.37% _zsh_highlight_bind_widgets
12) 2 14.02 7.01 1.74% 7.11 3.55 0.88% async
13) 1 6.91 6.91 0.86% 6.91 6.91 0.86% async_init
14) 2 37.33 18.66 4.64% 3.68 1.84 0.46% prompt_pure_setup
15) 14 3.24 0.23 0.40% 3.24 0.23 0.40% (anon)
16) 12 2.64 0.22 0.33% 2.64 0.22 0.33% add-zsh-hook
17) 3 1.12 0.37 0.14% 1.12 0.37 0.14% is-at-least
18) 3 0.78 0.26 0.10% 0.68 0.23 0.08% add-zle-hook-widget
19) 22 0.44 0.02 0.05% 0.44 0.02 0.05% put_template
20) 1 38.37 38.37 4.77% 0.29 0.29 0.04% set_prompt
21) 1 38.60 38.60 4.79% 0.23 0.23 0.03% prompt
22) 1 0.20 0.20 0.02% 0.17 0.17 0.02% zgen
23) 1 0.15 0.15 0.02% 0.15 0.15 0.02% _zsh_nvm_has
24) 2 0.05 0.02 0.01% 0.05 0.02 0.01% put_template_var
25) 1 0.03 0.03 0.00% 0.03 0.03 0.00% is-callable
26) 1 0.03 0.03 0.00% 0.03 0.03 0.00% zgen-saved
27) 1 0.02 0.02 0.00% 0.02 0.02 0.00% put_template_custom
That first column and %-column is interesting to look at. Looking deeper zgen-init
also spends time mostly on chpwd_trash
.
With a simple alias to the python script, instead of the full plugin, my startup looks like this:
num calls time self name
-----------------------------------------------------------------------------------
1) 4 216.78 54.20 44.67% 86.15 21.54 17.75% pmodload
2) 1 83.53 83.53 17.21% 83.42 83.42 17.19% _zsh_nvm_lazy_load
3) 1 75.15 75.15 15.49% 75.15 75.15 15.49% _zgen-check-for-updates
4) 2 47.83 23.92 9.86% 47.83 23.92 9.86% compinit
5) 1 467.79 467.79 96.40% 43.60 43.60 8.99% zgen-init
6) 1 27.63 27.63 5.69% 27.63 27.63 5.69% prompt_pure_state_setup
7) 2 24.82 12.41 5.12% 24.82 12.41 5.12% promptinit
8) 2 36.88 18.44 7.60% 20.36 10.18 4.20% async
9) 1 16.53 16.53 3.41% 16.53 16.53 3.41% async_init
10) 3 16.46 5.49 3.39% 16.46 5.49 3.39% mkdir
11) 1 14.10 14.10 2.91% 13.87 13.87 2.86% _zsh_highlight_load_highlighters
12) 1 12.97 12.97 2.67% 12.97 12.97 2.67% _zsh_highlight_bind_widgets
13) 14 6.32 0.45 1.30% 6.32 0.45 1.30% (anon)
14) 12 3.21 0.27 0.66% 3.21 0.27 0.66% add-zsh-hook
15) 2 70.64 35.32 14.56% 2.88 1.44 0.59% prompt_pure_setup
16) 3 1.79 0.60 0.37% 1.58 0.53 0.32% add-zle-hook-widget
17) 3 0.78 0.26 0.16% 0.78 0.26 0.16% is-at-least
18) 22 0.75 0.03 0.16% 0.75 0.03 0.16% put_template
19) 1 71.87 71.87 14.81% 0.40 0.40 0.08% set_prompt
20) 1 72.06 72.06 14.85% 0.19 0.19 0.04% prompt
21) 1 0.19 0.19 0.04% 0.16 0.16 0.03% zgen
22) 1 0.11 0.11 0.02% 0.11 0.11 0.02% _zsh_nvm_has
23) 2 0.04 0.02 0.01% 0.04 0.02 0.01% put_template_var
24) 1 0.03 0.03 0.01% 0.03 0.03 0.01% zgen-saved
25) 1 0.02 0.02 0.00% 0.02 0.02 0.00% is-callable
26) 1 0.01 0.01 0.00% 0.01 0.01 0.00% put_template_custom
Much better! In milliseconds, for me, it made ~800ms -> ~500ms.
I have added the following lines in my .zshrc:
if hash careful_rm.py 2>/dev/null; then
alias rm="$(command -v careful_rm.py)"
else
alias rm="rm -I"
fi
as described in the help file ($ rm -h).
Now
$ rm -cviI
and
$ careful_rm.py -cviI
work fine but I would like to have these options by default when I simply run $ rm.
How can it be done?
The alias
alias rm="careful_rm.py -cviI"
does not work.
We should test for the presence of trash
on MacOS and swap out the applescript for trash
if it exists, as it is likely to be at least a little faster.
trash
is here: https://github.com/ali-rantakari/trash
The code should hypothetically work fine on a Windows *nix environment like Cygwin or the new Windows bash shell, but I don't have Windows and can't test it. If anyone wants to take a whack at adding Windows support, that would be great.
Just tried installing this in Oh-My-Zsh on macOS 10.13.6 (17G65) but starting a new shell shows a Python stack trace...
OSA = check_output('command -pv osascript', shell=True).strip()
File "/Users/andy/.oh-my-zsh/custom/plugins/careful_rm/careful_rm.py", line 163, in check_output
_, stdout, _ = run(cmd, shell=shell, check=True, get=True)
File "/Users/andy/.oh-my-zsh/custom/plugins/careful_rm/careful_rm.py", line 130, in run
'get must be one of {0} is {1}'.format(get_options, get)
ValueError: get must be one of ['all', 'stdout', 'stderr', 'code', None] is True```
Digging through the code, it looks like `check_output` is passing `get=True` to the run command: https://github.com/MikeDacre/careful_rm/blob/master/careful_rm.py#L161 which immediately raises the ValueError.
The output of `command -pv osascript` on my system is `/usr/bin/osascript` but I can't quite work out what we should be passing to run's `get` param, otherwise I'd have done a PR for you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.