mirror of
https://github.com/notwa/rc
synced 2024-11-05 06:39:02 -08:00
overhaul e script
This commit is contained in:
parent
5648ee5c92
commit
74bde1eea1
1 changed files with 42 additions and 10 deletions
52
sh/e
52
sh/e
|
@ -1,17 +1,49 @@
|
|||
#!/bin/zsh
|
||||
# http://unix.stackexchange.com/a/37887
|
||||
#[ -n "${ZSH_VERSION:-}" ] && local -A EDITOR ${=EDITOR}
|
||||
# TODO: find actually working way of portably splitting word cmds, zsh/bash
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
$=EDITOR
|
||||
return
|
||||
local editor=(${=EDITOR})
|
||||
local running=0
|
||||
|
||||
if [ -n "$MSYSTEM" ]; then
|
||||
if ps -f | awk '{print $6}' | grep -qF "/usr/bin/${editor[1]}"; then
|
||||
running=1
|
||||
fi
|
||||
elif ps -o comm | grep -qFx "${editor[1]}"; then
|
||||
running=1
|
||||
fi
|
||||
|
||||
if [ "$running" -eq 1 ]; then
|
||||
printf "%s\n" "${editor[1]} is already running" >&2
|
||||
read -q '?Continue? [y/N] ' _ || { echo; return; }
|
||||
echo
|
||||
fi
|
||||
|
||||
if [ $# -eq 0 ] || [ -n "$MSYSTEM" ]; then
|
||||
$=EDITOR
|
||||
return
|
||||
fi
|
||||
|
||||
local f needroot=0
|
||||
for f in $@; do
|
||||
[ -e "$f" ] && { [ -w "$f" ] || needroot=1; }
|
||||
# TODO: check directory permissions too
|
||||
for f in "$@"; do
|
||||
# easy: file exists, we have write permissions
|
||||
[ -w "$f" ] && continue
|
||||
|
||||
# easy: file exists, but no write permissions
|
||||
[ -e "$f" ] && { needroot=1; break }
|
||||
|
||||
# hard: file may be in a directory that we can't inspect
|
||||
local d="$f"
|
||||
while expr index "$d" / >/dev/null; do
|
||||
d="${d%/*}"
|
||||
# NOTE: this gets weird with the root directory, not sure how to handle
|
||||
[ -e "$d" ] && [ ! -w "$d" ] && { needroot=1 && break; }
|
||||
done
|
||||
[ $needroot -eq 1 ] && break
|
||||
|
||||
# easy: file just doesn't exist
|
||||
done
|
||||
|
||||
if [ $needroot -eq 0 ]; then $=EDITOR $@; else sudo -e $@; fi
|
||||
if [ $needroot -eq 0 ]; then
|
||||
$=EDITOR "$@"
|
||||
else
|
||||
sudo -e "$@"
|
||||
fi
|
||||
|
|
Loading…
Reference in a new issue