fix(dotenv): draw confirmation prompt in next empty line

Without this fix the confirmation prompt appears wherever the cursor is,
which means that it might appear in the command line when using a widget
that changes the directory without redrawing the prompt (an example of
this are the dircycle and dirhistory plugins).
This commit is contained in:
Marc Cornellà 2021-06-28 15:54:24 +02:00
parent 0e7d7b87f3
commit c44b99e901
No known key found for this signature in database
GPG key ID: 0314585E776A9C1B

View file

@ -11,7 +11,10 @@
## Functions
source_env() {
if [[ -f $ZSH_DOTENV_FILE ]]; then
if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then
return
fi
if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
local confirmation dirpath="${PWD:A}"
@ -20,15 +23,23 @@ source_env() {
touch "$ZSH_DOTENV_DISALLOWED_LIST"
# early return if disallowed
if grep -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
return;
if command grep -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
return
fi
# check if current directory's .env file is allowed or ask for confirmation
if ! grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
if ! command grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
# get cursor column and print new line before prompt if not at line beginning
local column
echo -ne "\e[6n" > /dev/tty
read -t 1 -s -d R column < /dev/tty
column="${column##*\[*;}"
[[ $column -eq 1 ]] || echo
# print same-line prompt and output newline character if necessary
echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
read -k 1 confirmation
[[ "$confirmation" = $'\n' ]] || echo
# check input
case "$confirmation" in
@ -45,7 +56,6 @@ source_env() {
setopt localoptions allexport
source $ZSH_DOTENV_FILE
fi
}
autoload -U add-zsh-hook