def run(self): self.session = Session(self.mud, self.session_callback) self.w_session = SessionWidget(self.session) self.w_status = StatusWidget() self.w_map = MapWidget(self.session.mapper) self.w_frame = urwid.Frame(self.w_session, None, self.w_status) self.current_overlay = None # TODO: Make user_input, info and error customizable palette = [ ('default', 'default', 'default'), ('user_input', 'brown', 'default'), ('info', 'dark blue', 'default'), ('error', 'dark red', 'default'), ("00", 'default', 'default'), ("10", 'black', 'default'), ("20", 'dark red', 'default'), ("30", 'dark green', 'default'), ("40", 'brown', 'default'), ("50", 'dark blue', 'default'), ("60", 'dark magenta', 'default'), ("70", 'dark cyan', 'default'), ("80", 'white', 'default'), ("01", 'default', 'default'), ("11", 'black', 'default'), ("21", 'light red', 'default'), ("31", 'light green', 'default'), ("41", 'yellow', 'default'), ("51", 'light blue', 'default'), ("61", 'light magenta', 'default'), ("71", 'light cyan', 'default'), ("81", 'white', 'default'), ] self.command_chain = CommandChain() self.command_chain.chain = [self.mud, self, self.session, self.session.mapper] if options.screen == "raw": self.screen = urwid.raw_display.Screen() elif options.screen == "curses": self.screen = urwid.curses_display.Screen() self.loop = urwid.MainLoop(self.w_frame, palette, self.screen, handle_mouse=False, input_filter=self.master_input) self.writer = WindowWriter(self.loop, self.w_session) self.session.connect() self.loop.run()
class Interface(CommandObject): def __init__(self, mud = None): global master master = self self.mud = mud self.current_overlay = None def run(self): self.session = Session(self.mud, self.session_callback) self.w_session = SessionWidget(self.session) self.w_status = StatusWidget() self.w_map = MapWidget(self.session.mapper) self.w_frame = urwid.Frame(self.w_session, None, self.w_status) self.current_overlay = None # TODO: Make user_input, info and error customizable palette = [ ('default', 'default', 'default'), ('user_input', 'brown', 'default'), ('info', 'dark blue', 'default'), ('error', 'dark red', 'default'), ("00", 'default', 'default'), ("10", 'black', 'default'), ("20", 'dark red', 'default'), ("30", 'dark green', 'default'), ("40", 'brown', 'default'), ("50", 'dark blue', 'default'), ("60", 'dark magenta', 'default'), ("70", 'dark cyan', 'default'), ("80", 'white', 'default'), ("01", 'default', 'default'), ("11", 'black', 'default'), ("21", 'light red', 'default'), ("31", 'light green', 'default'), ("41", 'yellow', 'default'), ("51", 'light blue', 'default'), ("61", 'light magenta', 'default'), ("71", 'light cyan', 'default'), ("81", 'white', 'default'), ] self.command_chain = CommandChain() self.command_chain.chain = [self.mud, self, self.session, self.session.mapper] if options.screen == "raw": self.screen = urwid.raw_display.Screen() elif options.screen == "curses": self.screen = urwid.curses_display.Screen() self.loop = urwid.MainLoop(self.w_frame, palette, self.screen, handle_mouse=False, input_filter=self.master_input) self.writer = WindowWriter(self.loop, self.w_session) self.session.connect() self.loop.run() def master_input(self, keys, raw): outk = [] for k in keys: if k == "esc": self.end_overlay() self.w_status.stop_edit() self.w_frame.set_focus('body') elif k == options.prefix and self.w_session.input.get_edit_text() == "" and self.w_frame.focus_part != 'footer': self.w_status.start_edit() self.w_frame.set_focus('footer') elif k in self.mud.keys: line = self.mud.keys[k] if line[0] == options.prefix: line = line[1:].split() self.command(line) else: self.writer.write(line + "\n", Colors.INPUT) self.w_session.session.stdin.writeln(line) else: outk.append(k) return outk def session_callback(self, ob, typ, arg): if typ == Event.STDIO: for o in ob.out: if ob.out[o].has_data(): self.writer.write(ob.out[o].read()) if typ == Event.INFO: self.writer.write(ob.info.read(), Colors.INFO) elif typ == Event.CLOSED: for o in ob.out: if ob.out[o].has_data(): self.writer.write(ob.out[o].read()) self.writer.write("Session closed.\n", Colors.INFO) elif typ == Event.CONNECTED: self.writer.write("Session started.\n", Colors.INFO) elif typ == Event.ERROR: self.writer.write(ob.stderr.read(), Colors.ERROR) elif typ == Event.STATUS: pass elif typ == Event.MAP: if self.current_overlay == self.w_map: self.w_map.update_map() self.update_status() def update_status(self, loop=None, data=None): self.w_status.set_middle(self.mud.get_middle_status()) self.w_status.set_right(self.mud.get_right_status()) def set_status(self, msg): self.w_status.set_left(msg) def start_overlay(self, widget, halign='center', hsize=('relative',80), valign='middle', vsize=('relative',80)): self.w_overlay = DynamicOverlay(urwid.LineBox(widget), self.w_session, halign, hsize, valign, vsize) self.w_frame.set_body(self.w_overlay) self.current_overlay = widget def end_overlay(self): self.w_frame.set_body(self.w_session) self.current_overlay = None def command(self, cmd): try: ret = self.command_chain.run_command(cmd) if ret: if isinstance(ret, str): self.set_status(ret) else: self.set_status("") else: self.set_status("Command not found.") except TypeError: self.set_status("Syntax Error") self.update_status() def cmd_quit(self): raise urwid.ExitMainLoop() def cmd_reload(self): from mudblood.session import load_mud_definition if self.mud.path != "": try: newmud = load_mud_definition(self.mud.path) except Exception, e: self.w_session.append_data(traceback.format_exc(), 'error') return "Error in definition file." self.mud = newmud self.session.mud = newmud self.command_chain.chain = [self.mud, self, self.session, self.session.mapper] self.mud.connect(self.session) return "Ok." else: