Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ find | grep -i hint
- $ hi
- ./hi
- ./hindi
- ./hint
- $ hint
- ./hint
- #!/bin/bash
- # Set MINLEN to the minimum number of characters needed to start the
- # search.
- MINLEN=2
- clear
- echo "Start typing (minimum $MINLEN characters)..."
- # get one character without need for return
- while read -n 1 -s i
- do
- # get ascii value of character to detect backspace
- n=`echo -n $i|od -i -An|tr -d " "`
- if (( $n == 127 )) # if character is a backspace...
- then
- if (( ${#in} > 0 )) # ...and search string is not empty
- then
- in=${in:0:${#in}-1} # shorten search string by one
- # could use ${in:0:-1} for bash >= 4.2
- fi
- elif (( $n == 27 )) # if character is an escape...
- then
- exit 0 # ...then quit
- else # if any other char was typed...
- in=$in$i # add it to the search string
- fi
- clear
- echo "Search: ""$in""" # show search string on top of screen
- if (( ${#in} >= $MINLEN )) # if search string is long enough...
- then
- find "$@" -iname "*$in*" # ...call find, pass it any parameters given
- fi
- done
- #Produce basic output, dynamically filter it in the terminal,
- #and output the final, confirmed results to stdout
- vi `find . | terminalFilter`
- #!/usr/bin/env bash
- ##terminalFilter
- del=`printf "x7f"` #backspace character
- input="`cat`" #create initial set from all input
- #take the filter macro from the first argument or use
- # 'grep -F "$pattern"'
- filter=${1:-'grep -F "$pattern"'}
- pattern= #what's inputted by the keyboard at any given time
- printSelected(){
- echo "$input" | eval "$filter"
- }
- printScreen(){
- clear
- printSelected
- #Print search pattern at the bottom of the screen
- tput cup $(tput lines); echo -n "PATTERN: $pattern"
- } >/dev/tty
- #^only the confirmed results go `stdout`, this goes to the terminal only
- printScreen
- #read from the terminal as `cat` has already consumed the `stdin`
- exec 0</dev/tty
- while IFS=$'n' read -s -n1 key; do
- case "$key" in
- "$del") pattern="${pattern%?}";; #backspace deletes the last character
- "") break;; #enter breaks the loop
- *) pattern="$pattern$key";; #everything else gets appended
- #to the pattern string
- esac
- printScreen
- done
- clear
- printSelected
Add Comment
Please, Sign In to add comment