-
-
Save bensonk/340902 to your computer and use it in GitHub Desktop.
Fuzzy file lister written in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os, os.path | |
def match(query, item): | |
for c in query: | |
if c in item: item = item[item.find(c)+1:] | |
else: return False | |
return True | |
def finder(query, items): | |
return [ item for item in items if match(query, item) ] | |
def recursive_ls(d): | |
abs = lambda x: d + os.sep + x | |
ret = [] | |
for it in os.listdir(d): | |
ret.append(it) | |
if os.path.isdir(abs(it)): | |
ret.extend([ it + os.sep + f for f in recursive_ls(abs(it)) ]) | |
return ret | |
def fuzzy_ls(query, path="."): | |
return finder(query, recursive_ls(path)) | |
if __name__ == "__main__": | |
if len(sys.argv) == 2 or len(sys.argv) == 3: | |
for item in fuzzy_ls(*sys.argv[1:]): | |
print item | |
else: | |
print "usage: fls <expr> [<path>]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment