def help_menu(root: Container) -> None: """A menu to display various commands""" commands = [ ("help, ?", "show help menu"), ("search, /", "search for a person "), ("convert, $", "convert between USD and BEZO"), ] help_root = outer_container() help_root += Label("[bold 72]Help Menu") help_inner = Container() help_root += help_inner for name, text in commands: row = Splitter() row.arrangement = "20;30" row += Label("[157]" + name, align=Label.ALIGN_LEFT) row += Label("[italic 239]" + text, align=Label.ALIGN_RIGHT) help_inner += row fade_widget(root) help_root.center() help_root.print() getch() fade_widget(root, True)
with alt_buffer(cursor=False): Container.set_char("border", ["│ ", "─", " │", "─"]) Container.set_char("corner", ["╭", "╮", "╯", "╰"]) Splitter.set_char("separator", " ") main = Container(horiz_align=Container.HORIZ_ALIGN_LEFT) header = Splitter() header += Label("color picker one:", Label.ALIGN_LEFT) # header += Label("one", Label.ALIGN_CENTER) # header += Label("onne", Label.ALIGN_CENTER) # header += Label("onne", Label.ALIGN_CENTER) header += Label("color picker two:", Label.ALIGN_RIGHT) splitter = Splitter() splitter += ColorPicker(16) splitter += ColorPicker(16) main += header main += splitter main += ColorPicker(20) main += Label("color picker three:") main.center() main.print() while getch(): pass
right = create_container(30) splitter += right main += Container() + splitter main.set_char("border", ["|x| ", "=", " |x|", "="]) main.forced_width = 120 fields = [left, right] selected = left field().focus() main.center() main.print() while True: key = getch() if field().has_selection(): if key in ["x", keys.BACKSPACE, "d"]: field().clear_value() field().clear_selected() elif key == keys.TAB: field().blur() selected = fields[len(fields) - 1 - fields.index(selected)] field().focus() elif key == keys.CTRL_A: field().select_range((0, len(field().value) - 1))
def main(): """Where the magic happens""" with open(to_local(LAYOUT_DIR + "/bezos.ptg"), "r") as datafile: root = serializer.load_from_file(datafile) field = get_widget("field") field.set_style("cursor", MarkupFormatter("[inverse 72]{item}")) title = get_widget("header_title") title.value = "[bold 208]What are they worth?" inner = get_widget("inner") loading = outer_container() + Label(LOADING_ART) loading.center() loading.print() people = get_people() bezo = 0 for person in people: if person.name == "Jeff Bezos": bezo = person.worth loading.wipe() for row, person in zip(inner[2:], people): markups = [ "[221]{item}", "[157 bold]$ [/bold 246]{item} billions", "[214 bold]B [/bold 246]{item}", ] for label, markup in zip(row, markups): label.set_style("value", MarkupFormatter(markup)) name, dollars, bezos = row name.value = person.name dollars.value = f"{person.worth:0<7,}" bezos.value = f"{round(person.worth/bezo, 2):0<4,}" root.focus() root.center() root.print() while True: root.print() key = getch(interrupts=False) if key == chr(3): break command = field.get_value() if not key == keys.RETURN: if key == "?": help_menu(root) continue elif key == "$": convert_menu(root, bezo) continue field.send(key) root.print() if not command.startswith("/"): continue if command == "exit": break if command in ["help", "?"]: help_menu(root) field.clear_value() elif command == "convert": convert_menu(root, bezo) field.clear_value() elif command.startswith("/"): search_menu(people, field, bezo)
def search_menu(people: list[Person], field: InputField, bezo: int) -> None: """A menu that overlays root to show filtered results""" def _update_search(value: str, inner: Container, root: Container) -> None: """Update inner values based on search""" ratios = [] for person in people: ratios.append([person, fuzz.ratio(value, person.name)]) ratios.sort(key=lambda item: item[1], reverse=True) data = [person for person, ratio in ratios if ratio > 30] # the first line is the title, second is padding for row in inner[2:]: for label in row: label.value = "" for row, person in zip(inner[2:], data): markups = [ "[221]{item}", "[157 bold]$ [/bold 246]{item} billions", "[214 bold]B [/bold 246]{item}", ] for label, markup in zip(row, markups): label.set_style("value", MarkupFormatter(markup)) name, usd, bezo_label = row name.value = name_pre + person.name usd.value = usd_pre + f"{person.worth:0<7,}" bezo_label.value = bezo_pre + f"{round(person.worth/bezo, 2):0<4,}" root.print() with open(to_local(LAYOUT_DIR + "/bezos.ptg"), "r") as file: s_root = serializer.load_from_file(file) # Note: this overwrites the "field" id established in main() # there should be an object-local get to combat this. s_inner = get_widget("inner") s_field = get_widget("field") s_title = get_widget("header_title") s_field.set_style("cursor", lambda depth, item: background(item, 72)) s_field.prompt = " /" s_field.value = field.value[1:] s_field.cursor = field.cursor s_title.value = "[bold 208]Search for a person!" name_pre, usd_pre, bezo_pre = [label.value.strip() for label in s_inner[3]] s_root.focus() s_root.center() s_root.print() while True: key = getch() if key == keys.RETURN: return if real_length(s_field.get_value()) == 0 and key == keys.BACKSPACE: field.clear_value() break if key == keys.ESC: field.clear_value() break s_field.send(key) value = s_field.get_value() _update_search(value, s_inner, s_root)
def convert_menu(root: Container, bezo: int) -> None: """A menu to convert between BEZO and USD""" convert_root = outer_container() convert_root.forced_width = 70 convert_root += Label("[bold 72]Convert Menu") convert_inner = Container() convert_root += convert_inner for name in ["[157]$USD", "[208]BEZO"]: row = Splitter("6;50") row += Label(name) row += InputField() convert_inner += row result_row = Splitter("6;50") result_row += Label("[bold 214]Result") result_row += Label() fade_widget(root) fields = [row[1] for row in convert_inner] focused = fields[0] focused.focus() convert_root.center() convert_root.print() while True: key = getch() if key == keys.TAB: focused.blur() focused = fields[len(fields) - fields.index(focused) - 1] focused.focus() elif key == keys.RETURN: value = focused.clear_value() if not value.isdigit() or int(value) == 0: return # USD -> BEZO if focused == fields[0]: converted = int(value) / (bezo * 10**6) text = f"[249]${value} converts to B{simplify_float(converted):,}!" # BEZO -> USD elif focused == fields[1]: converted = bezo * int(value) * 10**6 text = f"[249]B{value} converts to ${simplify_float(converted):,}!" focused.blur() result_row[1].value = text convert_inner += result_row convert_root.print() getch() focused.focus() convert_root.wipe() convert_inner.pop(-1) elif key == keys.ESC: break else: focused.send(key) convert_root.print() fade_widget(root, True)
try: ints = [int(num) for num in nums] except ValueError: # this gets triggered when the parameter for `code` is # not valid return ValueError, False ints[0] -= 1 return tuple(ints), is_pressed with alt_buffer(echo=False, cursor=False): report_mouse("press") pos = (0, 0) while key := getch(): clear() pos, pressed = parse_mouse(key) if pos is None: print_to((0, 0), "key: " + key) elif pos is ValueError: continue else: print_to(pos, color(("." if pressed else "x"), 210)) report_mouse("movement", action="stop")