1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-18 09:53:22 -07:00

rewrite e, fixing bugs and improving compatibility

this also adds a `running` command that isn't quite finished.
This commit is contained in:
Connor Olding 2021-10-11 15:03:24 -07:00
parent 99fa691e1b
commit d2c686123e
2 changed files with 70 additions and 30 deletions

62
sh/e
View File

@ -1,8 +1,8 @@
#!/usr/bin/env zsh
#!/usr/bin/env sh
# YES_ZSH
# NO_BASH
# NO_DASH
# NO_ASH
# YES_BASH
# YES_DASH
# YES_ASH
# YES_BB_AWK
e() { ### @-
@ -13,40 +13,34 @@ e() { ### @-
### $ e /etc/sudoers
### [sudo] password for notwa:
### ```
local editor=(${=EDITOR})
local running=0
if [ -n "$MSYSTEM" ]; then
if ps -f | awk '{print $6}' \
| grep -o '[^/]*$' | grep -qFx "${editor[1]}"; then
running=1
[ -z "$ZSH_OPTIONS" ] || setopt local_options sh_word_split
local f= d= running= editor= needroot=0
editor="${EDITOR%% *}"
(running pid cmd | while read -r pid cmd; do
if [ "$cmd" = "$editor" ]; then
printf "%s (%s)\n" "$editor is already running" "$pid" >&2
confirm
exit
fi
elif ps -o comm | grep -qFx "${editor[1]}"; then
running=1
fi
done) || return
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
f="$(readlink -f "$f")" || continue
[ -z "$MSYSTEM" ] || f="$(cygpath -u "$f")" || continue
# easy: file exists, we have write permissions
[ -w "$f" ] && continue
# easy: file exists, but no write permissions
[ -e "$f" ] && { needroot=1; break }
[ -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="$f"
while [ "${d%/*}" != "$d" ]; do
d="${d%/*}"
# NOTE: this gets weird with the root directory, not sure how to handle
[ -e "$d" ] && [ ! -w "$d" ] && { needroot=1 && break; }
@ -56,11 +50,19 @@ e() { ### @-
# easy: file just doesn't exist
done
if [ $needroot -eq 0 ]; then
$=EDITOR "$@"
if [ $needroot -eq 1 ] && [ -n "$MSYSTEM" ]; then
# this pretty much never happens, because permissions are so busted, but...
printf "NOTE: you need root permissions, but this is Windows." >&2
printf " this probably isn't going to work." >&2
pause
$EDITOR "$@"
elif [ $needroot -eq 0 ]; then
$EDITOR "$@"
else
sudo -e "$@"
fi
}
[ -n "${preload+-}" ] || . ~/sh/preload || exit 2
eval ${preload:-preload} running confirm pause
[ -n "${preload+-}" ] || e "$@"

38
sh/running Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
# YES_BB_AWK
# probably not compatible with busybox ps because its column 2 isn't PID.
running() { ### @- WIP
local cmd=0 pid=0
local usage='usage: running {cmd|pid} ...'
[ $# -gt 0 ] || { printf '%s\n' "$usage" >&2; return 2; }
for col; do
case "$col" in
(cmd) cmd=1;;
(pid) pid=1;;
(*) printf '%s\n' "$usage" >&2; return 2;;
esac
done
ps -f | awk -v cmd=$cmd -v pid=$pid '
NR==1{
s=index($0,"COMMAND")
s=s?s:index($0,"CMD")
}
NR>1{
l=substr($0,s)
sub(" .*","",l)
sub(".*[/\\\\]","",l)
if(pid)printf "%s", $2
if(cmd&&pid)printf "\t"
if(cmd)printf "%s", l
printf "\n"
}
'
}
[ -n "${preload+-}" ] || running "$@"