Пример #1
0
def main():
    # Create a big layout of many text areas, then wrap them in a `ScrollablePane`.
    root_container = Frame(
        ScrollablePane(
            HSplit([
                Frame(TextArea(text=f"label-{i}"), width=Dimension())
                for i in range(20)
            ]))
        # ScrollablePane(HSplit([TextArea(text=f"label-{i}") for i in range(20)]))
    )

    layout = Layout(container=root_container)

    # Key bindings.
    kb = KeyBindings()

    @kb.add("c-c")
    def exit(event) -> None:
        get_app().exit()

    kb.add("tab")(focus_next)
    kb.add("s-tab")(focus_previous)

    # Create and run application.
    application = Application(layout=layout, key_bindings=kb, full_screen=True)
    application.run()
Пример #2
0
 def create_layout(self):
     inout_cells = list(
         itertools.chain.from_iterable([(
             VSplit([cell.input_prefix, cell.input]),
             VSplit([cell.output_prefix, ONE_COL, cell.output]),
         ) for cell in self.cells]))
     root_container = ScrollablePane(HSplit(inout_cells))
     self.layout = Layout(root_container)
Пример #3
0
def main():
    # Create a big layout of many text areas, then wrap them in a `ScrollablePane`.
    root_container = VSplit([
        Label("<left column>"),
        HSplit([
            Label("ScrollContainer Demo"),
            Frame(
                ScrollablePane(
                    HSplit([
                        Frame(
                            TextArea(
                                text=f"label-{i}",
                                completer=animal_completer,
                            )) for i in range(20)
                    ])), ),
        ]),
    ])

    root_container = FloatContainer(
        root_container,
        floats=[
            Float(
                xcursor=True,
                ycursor=True,
                content=CompletionsMenu(max_height=16, scroll_offset=1),
            ),
        ],
    )

    layout = Layout(container=root_container)

    # Key bindings.
    kb = KeyBindings()

    @kb.add("c-c")
    def exit(event) -> None:
        get_app().exit()

    kb.add("tab")(focus_next)
    kb.add("s-tab")(focus_previous)

    # Create and run application.
    application = Application(layout=layout,
                              key_bindings=kb,
                              full_screen=True,
                              mouse_support=True)
    application.run()
Пример #4
0
    def create_layout(self):
        inout_cells = list(
            itertools.chain.from_iterable([
                (
                    VSplit([cell.input_prefix, cell.input]),
                    VSplit([cell.output_prefix, ONE_COL, cell.output,
                            ONE_COL]),
                )
                for cell in self.cells[self.top_cell_idx:self.bottom_cell_idx +
                                       1  # noqa
                                       ]
            ]))
        nb_window = ScrollablePane(HSplit(inout_cells), show_scrollbar=False)

        def get_top_bar_text():
            text = ""
            if self.dirty:
                text += "+ "
            text += str(self.nb_path.relative_to(self.kernel_cwd))
            if self.dirty and self.quitting:
                text += (
                    " (no write since last change, please exit again to confirm, "
                    "or save your changes)")
            return text

        def get_bottom_bar_text():
            text = ""
            if self.kd and not self.no_kernel and self.kernel_name:
                if self.executing_cells:
                    kernel_status = "busy"
                else:
                    kernel_status = "idle"
                text += f"{self.kernel_name} ({kernel_status})"
            else:
                text += "[NO KERNEL]"
            text += (
                f" @ {self.kernel_cwd} - {self.current_cell_idx + 1}/{len(self.cells)}"
            )
            return text

        self.top_bar = FormattedTextToolbar(get_top_bar_text,
                                            style="#ffffff bg:#444444")
        self.bottom_bar = FormattedTextToolbar(get_bottom_bar_text,
                                               style="#ffffff bg:#444444")
        root_container = HSplit([self.top_bar, nb_window, self.bottom_bar])
        self.layout = Layout(root_container)
Пример #5
0
    def execute_code(self, frame):
        code = frame.code
        parsed_code = parse_code(frame)
        
        while True:
            inst = Inst(code[frame.ip])
            
            operand_stack_layout = []
            i = 0
            for v in frame.stack[::-1]:
                operand_stack_layout += [Label(f'TOS: {v}' if i == 0 else f'     {v}')]
                i += 1
                if isinstance(v, (np.longlong, np.double)):
                    operand_stack_layout += [Label('')]
                    i += 1
                    
            code_layout = []
            focused_element = None
            for ip, insn in parsed_code.items():
                if ip==frame.ip:
                    focused_element = Label(insn, style='bold fg:red')
                    code_layout += [focused_element]
                else:
                    code_layout += [Label(insn)]

            container = HSplit([
                VSplit([
                    HSplit([
                        PTFrame(
                            HSplit([
                                TextArea(f'NUMSTEPS'),
                                Label(f'Class: {frame.current_class.name()}'),
                                Label(f'Method: {frame.current_class.name()}.{frame.current_method.name}:{frame.current_method.desc}'),
                            ], height=3),
                            title='Context',
                        ),
                        PTFrame(
                            ScrollablePane(HSplit(code_layout, height=len(parsed_code) or 1), 
                                           scroll_offsets=ScrollOffsets(top=5, bottom=5),
                                           show_scrollbar=False),
                            title='ByteCode',
                        ),
                    ]),
                    HSplit([
                        PTFrame(
                            HSplit([Label(f'{i}: {v}') for i, v in enumerate(frame.locals)
                                   ], height=len(frame.locals) or 1),
                            title='Local Variables Stack',
                        ),
                        PTFrame(
                            HSplit(operand_stack_layout, height=len(operand_stack_layout) or 1),
                            title='Operands Stack',
                        )
                    ])
                ]),
                Window(),
                Label('UP/DOWN: step backward/forward. gg/GG: jump to the start/end. v: toggle view. q: quit.')
            ])

            global LAYOUT_STACK
            
            layout = Layout(container=container, focused_element=focused_element)
            LAYOUT_STACK += [layout]
            
            #print(frame.ip, inst.name)

            if len(frame.stack) > frame.max_stack + 1:
                print("MAX STACK")
                break

            if inst in OPCODES:
                OPCODES[inst](frame)
            elif inst == Inst.IASTORE or inst == Inst.AASTORE:
                val = frame.stack.pop()
                index = frame.stack.pop()
                array = frame.stack.pop()

                array[index] = val
            elif inst == Inst.IREM:
                v2 = frame.stack.pop()
                v1 = frame.stack.pop()
                frame.stack.append(v1 % v2)
            elif inst == Inst.IINC:
                index = read_byte(frame)
                const = read_signed_byte(frame)

                frame.set_local(index, frame.get_local(index) + const)
            elif inst == Inst.IFNE:
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if v1 != 0:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IFLT:
                v1 = frame.stack.pop()
                branch = read_signed_short(frame)

                if v1 < 0:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IFGE:
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if v1 >= 0:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IFLE:
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if v1 <= 0:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IF_ICMPLT:
                v2 = frame.stack.pop()
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if type(v1) is str and len(v1) == 1:
                    v1 = ord(v1)

                if type(v2) is str and len(v2) == 1:
                    v2 = ord(v2)

                if v1 < v2:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IF_ICMPGE:
                v2 = frame.stack.pop()
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if type(v1) is str and len(v1) == 1:
                    v1 = ord(v1)

                if type(v2) is str and len(v2) == 1:
                    v2 = ord(v2)

                if v1 >= v2:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IF_ICMPGT:
                v2 = frame.stack.pop()
                v1 = frame.stack.pop()

                branch = read_signed_short(frame)

                if type(v1) is str and len(v1) == 1:
                    v1 = ord(v1)

                if type(v2) is str and len(v2) == 1:
                    v2 = ord(v2)

                if v1 > v2:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.IF_ICMPLE:
                v2 = frame.stack.pop()
                v1 = frame.stack.pop()
                branch = read_signed_short(frame)

                if v1 <= v2:
                    frame.ip -= 3
                    frame.ip += branch
            elif inst == Inst.GOTO:
                branch = read_signed_short(frame)

                frame.ip -= 3
                frame.ip += branch
            elif inst == Inst.IRET or inst == Inst.LRET or inst == Inst.ARETURN or inst == Inst.DRETURN:
                return frame.stack.pop()
            elif inst == Inst.RETURN:
                return
            elif inst == Inst.GETSTATIC:
                index = read_unsigned_short(frame)

                methodRef = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[methodRef.class_index - 1].name
                natIndex = methodRef.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                if name in self.class_files:
                    cl = self.class_files[name]
                    
                    if not cl.static_initialized:
                        cl.static_initialized = True
                        
                        # first parse and initialize all existing static fields
                        if isinstance(cl, ClassFile):
                            for c in cl.const_pool:
                                if c.tag and c.tag.name == 'FIELDREF':
                                    name = cl.const_pool[c.name_and_type_index-1].name
                                    desc = cl.const_pool[c.name_and_type_index-1].desc
                                    cl.set_field(name, DEFAULTS.get(desc, None))
                                    
                        # then run the initializers
                        cl.handleStatic('<clinit>', '()V', frame)
                    frame.stack.append(cl.get_field(nat.name))

                #print(name)
                #print(vars(nat))
                #frame.stack.append(PrintStream())
            elif inst == Inst.PUTSTATIC:
                index = read_unsigned_short(frame)

                methodRef = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[methodRef.class_index - 1].name
                natIndex = methodRef.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                if name in self.class_files:
                    cl = self.class_files[name]
                    if not cl.static_initialized:
                        cl.static_initialized = True
                                    
                        # run the initializers
                        cl.handleStatic('<clinit>', '()V', frame)
                    cl.set_field(nat.name, frame.stack.pop())
            elif inst == Inst.GETFIELD:
                index = read_unsigned_short(frame)

                ref = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[ref.class_index - 1].name
                natIndex = ref.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                #print(vars(nat))

                obj = frame.stack.pop()
                #print(obj)
                frame.stack.append(obj.get_field(nat.name))
            elif inst == Inst.PUTFIELD:
                index = read_unsigned_short(frame)

                ref = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[ref.class_index - 1].name
                natIndex = ref.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                #print(vars(nat))

                value = frame.stack.pop()
                obj = frame.stack.pop()
                obj.set_field(nat.name, value)
            elif inst == Inst.INVOKEVIRTUAL:
                index = read_unsigned_short(frame)

                methodRef = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[methodRef.class_index - 1].name
                natIndex = methodRef.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                #print(name)
                #print(vars(nat))

                if name in self.class_files:
                    cl = self.class_files[name]
                    if cl.canHandleMethod(nat.name, nat.desc):
                        ret = cl.handleMethod(nat.name, nat.desc, frame)
                        if not nat.desc.endswith('V'):
                            frame.push(ret)
                else:
                    for i in range(argumentCount(nat.desc)):
                        frame.stack.pop()
                    frame.stack.pop()
            elif inst == Inst.INVOKESPECIAL:
                index = read_unsigned_short(frame)

                methodRef = frame.current_class.const_pool[index - 1]
                name = frame.current_class.const_pool[methodRef.class_index - 1].name
                natIndex = methodRef.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                #print(vars(methodRef))
                #print(vars(nat))
                #print(name)

                if name in self.class_files:
                    cl = self.class_files[name]
                    if cl.canHandleMethod(nat.name, nat.desc):
                        ret = cl.handleMethod(nat.name, nat.desc, frame)
                    else:
                        frame.stack.pop()
                        
            elif inst == Inst.INVOKESTATIC:
                index = read_unsigned_short(frame)

                methodRef = frame.current_class.const_pool[index - 1]
                cname = frame.current_class.const_pool[methodRef.class_index - 1].name
                natIndex = methodRef.name_and_type_index
                nat = frame.current_class.const_pool[natIndex - 1]

                #print(vars(methodRef))
                #print(vars(nat))
                #print(cname)

                if cname in self.class_files:
                    cl = self.class_files[cname]
                    if cl.canHandleMethod(nat.name, nat.desc):
                        ret = cl.handleStatic(nat.name, nat.desc, frame)
                        if not nat.desc.endswith('V'):
                            frame.push(ret)
            elif inst == Inst.NEW:
                index = read_unsigned_short(frame)
                
                methodRef = frame.current_class.const_pool[index - 1]

                if methodRef.name in self.class_files:
                    obj = self.class_files[methodRef.name].__class__()
                    if self.class_files[methodRef.name].file_path:
                        obj.from_file(self.class_files[methodRef.name].file_path)

                    obj.python_initialize()

                    frame.stack.append(obj)
                else:
                    frame.stack.append(None)

            #print(frame.stack, frame.locals)
            frame.ip += 1
Пример #6
0
    height=1,
    prompt=prompt,
    style="class:input-field",
    multiline=False,
    wrap_lines=False,
    # completer=completion,
    complete_while_typing=True)

placeholder_text = ("Nothing here yet:\n"
                    " - Type \"help\" to see available commands.\n"
                    " - Press \"?\" for the list of keybindings.")
entry_placeholder = Label(
    FormattedText([("class:placeholder", placeholder_text)]))

task_list = HSplit([entry_placeholder])
agenda_pane = ScrollablePane(task_list)

show_kb_help = False
bindings_help = ConditionalContainer(Box(
    Shadow(Frame(Label(""), "Key bindings:"))),
                                     filter=Condition(lambda: show_kb_help))

root_container = FloatContainer(
    content=HSplit([agenda_pane,
                    HorizontalLine(), history_field, input_field]),
    floats=[
        Float(
            xcursor=True,
            ycursor=True,
            content=CompletionsMenu(max_height=16, scroll_offset=1),
        ),