Skip to content

Instantly share code, notes, and snippets.

@andylshort
Created April 13, 2020 16:20
Show Gist options
  • Save andylshort/73c321f1ce2f7cdea0547e53e6ef5526 to your computer and use it in GitHub Desktop.
Save andylshort/73c321f1ce2f7cdea0547e53e6ef5526 to your computer and use it in GitHub Desktop.
python curses library - interactive dictionary search
import sys
import curses
def filter_words(words, start):
return list(filter(lambda s: s.startswith(start), words))
if __name__ == "__main__":
words = []
with open("words.txt", "r") as f:
words = [l.strip() for l in f.readlines()]
stdscr = curses.initscr()
curses.noecho()
k = 0
l = []
while (k != 27):
stdscr.addstr(0, 0, f"?: {''.join(l)}")
stdscr.clrtoeol()
stdscr.refresh()
k = stdscr.getch()
if k != 0:
if k == curses.KEY_BACKSPACE or k == 127:
l = l[:-1]
else:
l.append(chr(k))
start = ''.join(l)
for i, match in enumerate(filter_words(words, start)[0:10]):
stdscr.addstr(1 + i, 0, match)
stdscr.clrtoeol()
stdscr.clrtobot()
stdscr.refresh()
curses.endwin()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment