daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Dec 11th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ find | grep -i hint
  2.    
  3. $ hi
  4. ./hi
  5. ./hindi
  6. ./hint
  7.  
  8. $ hint
  9. ./hint
  10.    
  11. #!/bin/bash
  12. # Set MINLEN to the minimum number of characters needed to start the    
  13. # search.
  14. MINLEN=2
  15. clear
  16. echo "Start typing (minimum $MINLEN characters)..."
  17. # get one character without need for return
  18. while read -n 1 -s i
  19. do
  20.     # get ascii value of character to detect backspace
  21.     n=`echo -n $i|od -i -An|tr -d " "`
  22.     if (( $n == 127 )) # if character is a backspace...
  23.     then
  24.         if (( ${#in} > 0 )) # ...and search string is not empty
  25.         then
  26.             in=${in:0:${#in}-1} # shorten search string by one
  27.             # could use ${in:0:-1} for bash >= 4.2
  28.         fi
  29.     elif (( $n == 27 )) # if character is an escape...
  30.     then
  31.         exit 0 # ...then quit
  32.     else # if any other char was typed...
  33.         in=$in$i # add it to the search string
  34.     fi
  35.     clear
  36.     echo "Search: ""$in""" # show search string on top of screen
  37.     if (( ${#in} >= $MINLEN )) # if search string is long enough...
  38.     then    
  39.         find "$@" -iname "*$in*" # ...call find, pass it any parameters given
  40.     fi
  41. done
  42.    
  43. #Produce basic output, dynamically filter it in the terminal,
  44. #and output the final, confirmed results to stdout
  45. vi `find . | terminalFilter`
  46.    
  47. #!/usr/bin/env bash
  48.  
  49. ##terminalFilter
  50.  
  51. del=`printf "x7f"` #backspace character
  52.  
  53. input="`cat`"       #create initial set from all input
  54. #take the filter macro from the first argument or use
  55. # 'grep -F "$pattern"'
  56. filter=${1:-'grep -F "$pattern"'}  
  57. pattern=  #what's inputted by the keyboard at any given time
  58.  
  59. printSelected(){
  60.   echo "$input" | eval "$filter"
  61. }
  62. printScreen(){
  63.   clear
  64.   printSelected
  65.   #Print search pattern at the bottom of the screen
  66.   tput cup $(tput lines);  echo -n "PATTERN: $pattern"
  67. } >/dev/tty  
  68. #^only the confirmed results go `stdout`, this goes to the terminal only
  69.  
  70. printScreen
  71. #read from the terminal as `cat` has already consumed the `stdin`
  72. exec 0</dev/tty
  73. while IFS=$'n' read -s -n1 key; do
  74.   case "$key" in
  75.     "$del") pattern="${pattern%?}";;  #backspace deletes the last character
  76.         "") break;; #enter breaks the loop
  77.          *) pattern="$pattern$key";; #everything else gets appended
  78.                                      #to the pattern string
  79.   esac
  80.   printScreen
  81. done
  82.  
  83. clear
  84. printSelected
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top