Advertisement
elirang

izabera asterisk

May 5th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.46 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. readpasswd () {
  4.   local REPLY i IFS= LANG=C tty asterisks
  5.   tty=$(stty -g)
  6.   # bash sucks
  7.   stty -echo raw
  8.   trap 'stty "$tty"' return
  9.   while
  10.     asterisks=${password//?/*}
  11.     printf '\r%s\e[K\r' "$asterisks"
  12.     ((i)) && printf '\e[%dC' "$i"
  13.     read -rn1 -d ''
  14.   do
  15.     if [[ $REPLY = $'\x1b' ]]; then # first check arrows/del
  16.       read -rn1 -d ''
  17.       if [[ $REPLY = '[' ]]; then
  18.         read -rn1 -d ''
  19.         case $REPLY in
  20.           D) ((i -= i > 0));; # left arrow
  21.           C) ((i += i <= ${#password}-1));; # right arrow
  22.           3) read -rn1 -d ''
  23.              if [[ $REPLY = '~' ]]; then # del
  24.                password=${password:0:i}${password:i+1}
  25.              else continue
  26.              fi ;;
  27.           *) continue
  28.         esac
  29.       elif [[ $REPLY = O ]]; then # some terminals use ^[OC instead of ^[[C
  30.         read -rn1 -d ''
  31.         case $REPLY in
  32.           D) ((i -= i > 0));; # left arrow
  33.           C) ((i += i <= ${#password}));; # right arrow
  34.           *) continue
  35.         esac
  36.       else continue
  37.       fi
  38.     elif [[ $REPLY = [$'\b\177'] ]]; then # backspace
  39.       if ((i)); then
  40.         password=${password:0:i-1}${password:i}
  41.         ((i--))
  42.       fi
  43.     elif [[ $REPLY = [$'\4\r\n'] ]]; then # ^D \r \n
  44.       return
  45.     else
  46.       password=${password:0:i}$REPLY${password:i}
  47.       ((i++))
  48.     fi
  49.   done
  50. }
  51.  
  52. trap 'stty sane' exit
  53. readpasswd
  54. printf '\ryour password is <%s>\e[K\n' "$password"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement