def main(etmdir=""): global item, settings, ampm, style, etmstyle, application ampm = settings['ampm'] terminal_style = settings['style'] if terminal_style == "dark": style = dark_style etmstyle = dark_etmstyle else: style = light_style etmstyle = light_etmstyle agenda_view() application = Application( layout=Layout( root_container, focused_element=text_area, ), key_bindings=bindings, enable_page_navigation_bindings=True, mouse_support=True, style=style, full_screen=True) # Tell prompt_toolkit to use asyncio. use_asyncio_event_loop() # Run application async. loop = get_event_loop() loop.call_later(0, event_handler, loop) loop.run_until_complete( application.run_async().to_asyncio_future())
def main(): app = Application(layout=Layout(root_container.create(), focused_element=servers.content), key_bindings=kb, editing_mode=EditingMode.VI, style=style, mouse_support=True, full_screen=True, after_render=on_startup) asyncio.get_event_loop().run_until_complete( app.run_async().to_asyncio_future())
def radiolist_dialog(title='', values=None, style=None, async_=False): # Add exit key binding. bindings = KeyBindings() @bindings.add('c-d') def exit_(event): """ Pressing Ctrl-d will exit the user interface. """ event.app.exit() radio_list = RadioListFast(values) application = Application( layout=Layout(HSplit([Label(title), radio_list])), key_bindings=merge_key_bindings([load_key_bindings(), bindings]), mouse_support=True, style=style, full_screen=False) if async_: return application.run_async() else: return application.run()
def radiolist_dialog(title='', values=None, style=None, async_=False): # Add exit key binding. kb = KeyBindings() @kb.add('c-c') @kb.add("escape") def exit_(event): """ Pressing Ctrl-c will exit the user interface. """ event.app.exit() radio_list = SingleSelectList(values) application = Application( layout=Layout(HSplit([Label(title), radio_list])), key_bindings=kb, mouse_support=True, style=style, full_screen=False, ) if async_: return application.run_async() else: return application.run()
class Module(): """ The terminal UI, based on prompt-toolkit """ def __init__(self, loop, txClbk, vehListClk, vehObjClk, cmdProcessClk, cmdPrint, dialect, mavversion, isGUI): self.tabs = [] # all the vehicles, one in each tab self.loop = loop # Event actions self.txCallback = txClbk self.vehListCallback = vehListClk self.vehObjCallback = vehObjClk self.commandProcessor = cmdProcessClk self.printer = cmdPrint # Mavlink self.dialect = dialect self.mavversion = mavversion self.mod = getpymavlinkpackage(self.dialect, self.mavversion) # commands self.shortName = "terminal" self.commandDict = {'watch': self.watch} # Tell prompt_toolkit to use asyncio. terminal_use_async() self.tabbar = [] self.style_extensions = { # Tabs 'tabbar': 'noinherit', 'tabbar.tab': '', 'tabbar.tab.active': 'bold noinherit reverse', } self.current_style = Style.from_dict(self.style_extensions) # make the screen self.hscreen = [] self.hscreen.append( Window(height=1, content=FormattedTextControl(self.tabbar, style='class:tabbar'), align=WindowAlign.LEFT)) self.hscreen.append(Window(height=1, char='-', style='class:line')) self.hscreen.append(Window(content=None)) self.hscreen.append(Window(height=1, char='-', style='class:line')) self.hscreen.append(Window(height=1, content=None)) self.root_container = HSplit(self.hscreen) self.layout = Layout(self.root_container) self.application = Application(layout=self.layout, key_bindings=KB, full_screen=True, style=self.current_style) # event linkages self.application.nextTab = self.nextTab # initial layout self.tabbar.append(('class:tabbar.tab', ' {0} '.format("tmp"))) self.tabbar.append(('class:tabbar', ' ')) self.hscreen[0].content = FormattedTextControl(self.tabbar, style='class:tabbar') self.hscreen[2].content = BufferControl(focusable=False) self.hscreen[4].content = BufferControl(focusable=True) self.runUI() def closeModule(self): pass def watch(self, veh: str, cmd): self.printer(veh, "Watching: " + str(cmd)) def runUI(self): self.application.run_async() def changePrompt(self, vehname: str, prompt: str): """ Change the prompt for a vehicle """ for veh in self.tabs: if vehname == veh.name: veh.changePrompt(prompt) self.updateScreen() return def addVehicle(self, name: str): """ Add a new vehicle tab """ self.tabs.append(VehicleTab(name, self.cmdProcessor)) self.setActiveTab(name) self.application._redraw() def cmdProcessor(self, vehname: str, cmd: str): """ Process a user command cmd in tab name """ self.commandProcessor(vehname, cmd) def incomingPacket(self, vehname: str, pkt): """ Incoming packet """ # Send statustext to UI if pkt.get_type() == "STATUSTEXT": self.printVeh(pkt.text, vehname) # Mode change - update prompt if pkt.get_type() == "HEARTBEAT": self.changePrompt(vehname, mode_toString(pkt, self.mod)) self.application._redraw() def removeVehicleTab(self, name: str): """ Remove a vehicle tab """ for veh in self.tabs: if name == veh.name: if veh.active: self.nextTab() # remove self.tabs.remove(veh) self.updateScreen() return def printVeh(self, text: str, name: str): """ Send a string to vehicle <name> output. If <name> is None, send to all """ for veh in self.tabs: if name is None or veh.name == name: veh.output_text(text) def print(self, *args): """ Override of the print() function """ for veh in self.tabs: line = "" for arg in args: line += "{}".format(arg) veh.output_text(line) def updateScreen(self): """ Update the UI after a change """ # generate the tab bar self.tabbar = [] for veh in self.tabs: if veh.active: self.tabbar.append( ('class:tabbar.tab.active', ' {0} '.format(veh.name))) else: self.tabbar.append( ('class:tabbar.tab', ' {0} '.format(veh.name))) self.tabbar.append(('class:tabbar', ' ')) self.hscreen[0].content = FormattedTextControl(self.tabbar, style='class:tabbar') for veh in self.tabs: if veh.active: self.hscreen[2].content = BufferControl(buffer=veh.output, focusable=False) self.hscreen[4].content = BufferControl( buffer=veh.input, focusable=True, input_processors=[veh.prompt]) return def setActiveTab(self, name): """ Set the active vehicle to the named """ for veh in self.tabs: if veh.name == name: veh.active = True else: veh.active = False self.updateScreen() def nextTab(self, reverse=False): """ Navigate to the next tab Or previous tab if reverse=True """ hastab = False for veh in list(reversed(self.tabs)) if reverse else self.tabs: if veh.active: hastab = True elif hastab: self.setActiveTab(veh.name) hastab = False return # if we were at the last tab, circle around to next if hastab: if reverse: self.setActiveTab(self.tabs[-1].name) else: self.setActiveTab(self.tabs[0].name)
def runclient(): pubkey, privkey = get_keys() parser = configparser.ConfigParser() nloop = asyncio.new_event_loop() if os.path.exists("./server.conf") and yes_no_dialog( "Server", "Connect to last server?"): parser.read("server.conf") server = parser["server"]["server"] port = str(int(parser["server"]["port"])) else: server = input_dialog("Server", "Enter Server IP/Hostname:") port = input_dialog("Server", "Enter Port:") try: int(port) except: message_dialog("Server", "Invalid Port") sys.exit(1) parser["server"] = {} parser["server"]["server"] = server parser["server"]["port"] = port # save the server conf to file with open("server.conf", "w") as cfile: parser.write(cfile) # connect to server nick = input_dialog("Nickname", "Enter a nickname:") if nick is None: nick = "" banned = "/." if any([x in banned for x in nick]): message_dialog("Nickname", "Invalid nickname") messages = TextArea("", read_only=True) mes = [] q = queue.Queue() def handle_message(buf): q.put(buf.text) inputbox = TextArea(height=1, multiline=False, accept_handler=handle_message) msgbody = Frame(messages) body = HSplit( [msgbody, Window(height=1, char="#", style="class:line"), inputbox]) root = HSplit([ Window( height=1, content=FormattedTextControl(get_titlebar_text), align=WindowAlign.CENTER, ), Window(height=1, char=" ", style="class:line"), body, ]) kb = KeyBindings() @kb.add("c-c", eager=True) @kb.add("c-q", eager=True) def exitevent(event): q.put("!DISCONNECT") eventquit.set() event.app.exit() nloop.stop() myloop.stop() iscomplete.wait(1) sys.exit(0) app = Application( layout=Layout(root, focused_element=inputbox), key_bindings=kb, mouse_support=True, full_screen=True, ) async def update(serverpipe, qu): while True: while serverpipe.poll(): data = serverpipe.recv() mes.append(data) out = "\n".join(mes) msgbody.body = TextArea(out) get_app().invalidate() try: while True: dt = qu.get_nowait() serverpipe.send(dt) except queue.Empty: pass await asyncio.sleep(0.5) t = threading.Thread(target=startloop, args=(nloop, ), daemon=True) t.start() serversoc, cl = Pipe() iscomplete = threading.Event() eventquit = threading.Event() myloop = asyncio.new_event_loop() asyncio.set_event_loop(myloop) use_asyncio_event_loop(myloop) asyncio.run_coroutine_threadsafe( process_code("ws://{}:{}/{}".format(server, port, nick), cl, eventquit, iscomplete), nloop) asyncio.get_event_loop().create_task(update(serversoc, q)) asyncio.get_event_loop().run_until_complete(app.run_async()) myloop.run_forever()