def search(): search_str = '' sel_index = 0 init_line = cio.getcurpos().y res_limit = 10 matches = list(cur.execute("select id,name,path from app order by last_launch_time desc limit ?", (res_limit,))) while True: update_display(cio.get_console_size()[0], res_limit + 3, search_str, matches, sel_index) k = cio.wait_key() if ord(k) == 8: # BACKSPACE, erase last char search_str = search_str[:-1] elif ord(k) == 13: # ENTER, launch id,name,path = matches[sel_index] with conn: cur.execute("update app set launch_cnt=launch_cnt+1, last_launch_time=strftime('%s', 'now') WHERE id=?", (id,)) os.startfile(path) break elif ord(k) == 27: # ESC, quit break elif ord(k) == 224: other_k = cio.wait_key() if ord(other_k) == 72: if sel_index > 0: sel_index -= 1 elif ord(other_k) == 80: if sel_index < len(matches) - 1: sel_index += 1 else: search_str += k sel_index = 0 matches = list(cur.execute("select id,name,path from app where name like ? order by launch_cnt desc limit ?", ('%{}%'.format(search_str), res_limit))) cio.setcurpos(0, cio.getcurpos().y + res_limit + 3)
def update_display(width, height, search_str, matches, sel_index): assert width > 5 assert height > 0 width -= 1 search_str_prefix = 'app: ' cio.setcurpos(0, cio.getcurpos().y) print '{}{}'.format(search_str_prefix, cio.str_fill(search_str, width - len(search_str_prefix))) print line_count = 2 for i, (id, name, path) in enumerate(matches): if line_count >= height: break fmt_name = cio.str_fill(name, 30) fmt_path = cio.str_fill(path, width - len(fmt_name) - 3) # The 3 is for the two spaces and the * print '{} {} {}'.format('*' if sel_index == i else ' ', fmt_name, fmt_path) line_count += 1 for _ in range(height - line_count): print ' '*width cio.setcurpos(len(search_str_prefix) + len(search_str), cio.getcurpos().y - height)