wf-market/tui.py

61 lines
1.8 KiB
Python
Raw Normal View History

2022-11-04 13:16:10 +05:00
import curses
2022-11-04 15:50:51 +05:00
import curses.textpad
import curses.ascii
2022-11-04 13:16:10 +05:00
2022-11-07 03:14:37 +05:00
from api import Session
2022-11-04 15:50:51 +05:00
class GUI():
def __init__(self, root, h, w, y, x):
self.root = root
self.window = root.subwin(h, w, y, x)
def create_gui(self):
self.create_search_bar()
2022-11-07 03:14:37 +05:00
self.create_item_search_results()
2022-11-04 15:50:51 +05:00
def create_search_bar(self):
2022-11-07 03:14:37 +05:00
self.search_box = self.window.subwin(3, self.window.getmaxyx()[1], 0, 0)
2022-11-04 15:50:51 +05:00
self.search_box.border()
2022-11-07 03:14:37 +05:00
self.search_bar = self.window.subwin(1, self.window.getmaxyx()[1] - 2, 1, 1)
2022-11-04 15:50:51 +05:00
self.search_in = curses.textpad.Textbox(self.search_bar)
2022-11-07 03:14:37 +05:00
def create_item_search_results(self):
self.results_box = self.window.subwin(self.window.getmaxyx()[0] - 3, self.window.getmaxyx()[1] // 3, 3, 0)
2022-11-04 15:50:51 +05:00
self.results_box.border()
class App():
def __init__(self, root):
self.root = root
self.gui = GUI(self.root, *self.root.getmaxyx(), 0, 0)
2022-11-07 03:14:37 +05:00
self.client = Session()
2022-11-04 15:50:51 +05:00
def show_gui(self):
self.root.clear()
self.gui.create_gui()
2022-11-07 03:14:37 +05:00
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()
2022-11-04 15:50:51 +05:00
def main(w):
app = App(w)
app.show_gui()
w.refresh()
app.gui.search_in.edit()
2022-11-07 03:14:37 +05:00
app.update_item_search_results()
2022-11-04 15:50:51 +05:00
w.getch()
2022-11-07 03:14:37 +05:00
curses.wrapper(main)