Added item search

This commit is contained in:
Muaz Ahmad 2022-11-07 03:14:37 +05:00
parent d14ae17ad8
commit b2f0b55be4

36
tui.py
View file

@ -2,6 +2,8 @@ import curses
import curses.textpad import curses.textpad
import curses.ascii import curses.ascii
from api import Session
class GUI(): class GUI():
def __init__(self, root, h, w, y, x): def __init__(self, root, h, w, y, x):
self.root = root self.root = root
@ -9,38 +11,50 @@ class GUI():
def create_gui(self): def create_gui(self):
self.create_search_bar() self.create_search_bar()
self.create_search_results() self.create_item_search_results()
def create_search_bar(self): def create_search_bar(self):
self.search_box = self.window.subwin(3, self.root.getmaxyx()[1], 0, 0) self.search_box = self.window.subwin(3, self.window.getmaxyx()[1], 0, 0)
self.search_box.border() self.search_box.border()
self.search_bar = self.window.subwin(1, self.root.getmaxyx()[1] - 2, 1, 1) self.search_bar = self.window.subwin(1, self.window.getmaxyx()[1] - 2, 1, 1)
self.search_in = curses.textpad.Textbox(self.search_bar) self.search_in = curses.textpad.Textbox(self.search_bar)
def create_search_results(self): def create_item_search_results(self):
self.results_box = self.window.subwin(self.root.getmaxyx()[0] - 3, self.root.getmaxyx()[1] // 3, 3, 0) self.results_box = self.window.subwin(self.window.getmaxyx()[0] - 3, self.window.getmaxyx()[1] // 3, 3, 0)
self.results_box.border() self.results_box.border()
class App(): class App():
def __init__(self, root): def __init__(self, root):
self.root = root self.root = root
self.gui = GUI(self.root, *self.root.getmaxyx(), 0, 0) self.gui = GUI(self.root, *self.root.getmaxyx(), 0, 0)
self.client = Session()
def show_gui(self): def show_gui(self):
self.root.clear() self.root.clear()
self.gui.create_gui() self.gui.create_gui()
def update_item_search_results(self):
search_str = self.gui.search_in.gather()
items = self.client.api_request(url = '/items').get('payload').get('items')
items = [item for item in items if search_str.strip().lower() in item.get('item_name').lower()][:self.gui.results_box.getmaxyx()[0] - 2]
self.gui.results_box.clear()
self.gui.results_box.border()
for i in range(len(items)):
try:
self.gui.results_box.addstr(i + 1, 1, items[i]['item_name'][:self.gui.results_box.getmaxyx()[1] - 2])
except:
pass
self.gui.results_box.refresh()
def main(w): def main(w):
app = App(w) app = App(w)
app.show_gui() app.show_gui()
w.refresh() w.refresh()
app.gui.search_in.edit() app.gui.search_in.edit()
search_string = app.gui.search_in.gather() app.update_item_search_results()
app.gui.results_box.addstr(1, 1, search_string)
app.gui.results_box.refresh()
w.getch() w.getch()
return search_string
print(curses.wrapper(main)) curses.wrapper(main)