c44b99e901d7ef58f60247995152de1b937e2e9c

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

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).

Diff

 1diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh
 2index 24f285df555de97a46abce8906a985d83718e36d..40ec5c46fdbff5c54a7717a19aa1910494902b54 100644
 3--- a/plugins/dotenv/dotenv.plugin.zsh
 4+++ b/plugins/dotenv/dotenv.plugin.zsh
 5@@ -11,41 +11,51 @@
 6 ## Functions
 7 
 8 source_env() {
 9-  if [[ -f $ZSH_DOTENV_FILE ]]; then
10-    if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
11-      local confirmation dirpath="${PWD:A}"
12-
13-      # make sure there is an (dis-)allowed file
14-      touch "$ZSH_DOTENV_ALLOWED_LIST"
15-      touch "$ZSH_DOTENV_DISALLOWED_LIST"
16-
17-      # early return if disallowed
18-      if grep -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
19-        return;
20-      fi
21-
22-      # check if current directory's .env file is allowed or ask for confirmation
23-      if ! grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
24-        # print same-line prompt and output newline character if necessary
25-        echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
26-        read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
27-
28-        # check input
29-        case "$confirmation" in
30-          [nN]) return ;;
31-          [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
32-          [eE]) echo "$dirpath" >> "$ZSH_DOTENV_DISALLOWED_LIST"; return ;;
33-          *) ;; # interpret anything else as a yes
34-        esac
35-      fi
36+  if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then
37+    return
38+  fi
39+
40+  if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
41+    local confirmation dirpath="${PWD:A}"
42+
43+    # make sure there is an (dis-)allowed file
44+    touch "$ZSH_DOTENV_ALLOWED_LIST"
45+    touch "$ZSH_DOTENV_DISALLOWED_LIST"
46+
47+    # early return if disallowed
48+    if command grep -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
49+      return
50     fi
51 
52-    # test .env syntax
53-    zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
54+    # check if current directory's .env file is allowed or ask for confirmation
55+    if ! command grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
56+      # get cursor column and print new line before prompt if not at line beginning
57+      local column
58+      echo -ne "\e[6n" > /dev/tty
59+      read -t 1 -s -d R column < /dev/tty
60+      column="${column##*\[*;}"
61+      [[ $column -eq 1 ]] || echo
62 
63-    setopt localoptions allexport
64-    source $ZSH_DOTENV_FILE
65+      # print same-line prompt and output newline character if necessary
66+      echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
67+      read -k 1 confirmation
68+      [[ "$confirmation" = $'\n' ]] || echo
69+
70+      # check input
71+      case "$confirmation" in
72+        [nN]) return ;;
73+        [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
74+        [eE]) echo "$dirpath" >> "$ZSH_DOTENV_DISALLOWED_LIST"; return ;;
75+        *) ;; # interpret anything else as a yes
76+      esac
77+    fi
78   fi
79+
80+  # test .env syntax
81+  zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
82+
83+  setopt localoptions allexport
84+  source $ZSH_DOTENV_FILE
85 }
86 
87 autoload -U add-zsh-hook