Beispiel #1
0
    def __init__(self,
                 title,
                 regs,
                 read_callback=None,
                 read_callback_params=None,
                 value_format_str=None,
                 extend_label_width=False,
                 label_col="cyan",
                 reg_val_bad=None,
                 reg_val_good=None,
                 reg_val_warn=None,
                 reg_val_enum=None,
                 is_progress_bar=False,
                 progress_bar_range=100):
        super().__init__(title,
                         extend_label_width=extend_label_width,
                         label_col=label_col)
        self.value_format_str = value_format_str
        self.read_callback = read_callback
        self.read_callback_params = read_callback_params

        def my_get_node(reg_name, idx):
            node = rw.get_node(reg_name)
            if reg_val_bad is not None:
                node.sw_val_bad = reg_val_bad[idx] if isinstance(
                    reg_val_bad, list) else reg_val_bad
            if reg_val_good is not None:
                node.sw_val_good = reg_val_good[idx] if isinstance(
                    reg_val_good, list) else reg_val_good
            if reg_val_warn is not None:
                node.sw_val_warn = reg_val_warn[idx] if isinstance(
                    reg_val_warn, list) else reg_val_warn
            if reg_val_enum is not None:
                node.sw_enum = reg_val_enum[idx] if isinstance(
                    reg_val_enum, list) else reg_val_enum

            return node

        if read_callback is None:
            if isinstance(regs, list):
                self.regs = []
                for i in range(len(regs)):
                    reg = regs[i]
                    self.regs.append(my_get_node(reg, i))
            elif isinstance(regs, str):
                self.regs = [my_get_node(regs, 0)]
            else:
                raise ValueError(
                    "reg must be either a list of strings or a string")

        self.value_label = Label(text="")

        self.is_progress_bar = is_progress_bar
        self.progress_bar_range = progress_bar_range
        if is_progress_bar:
            self.value_label = ProgressBar()

        self.update()
Beispiel #2
0
def progress_dialog(
    title: AnyFormattedText = "",
    text: AnyFormattedText = "",
    run_callback: Callable[[Callable[[int], None], Callable[[str], None]], None] = (
        lambda *a: None
    ),
    style: Optional[BaseStyle] = None,
) -> Application[None]:
    """
    :param run_callback: A function that receives as input a `set_percentage`
        function and it does the work.
    """
    loop = get_event_loop()
    progressbar = ProgressBar()
    text_area = TextArea(
        focusable=False,
        # Prefer this text area as big as possible, to avoid having a window
        # that keeps resizing when we add text to it.
        height=D(preferred=10 ** 10),
    )

    dialog = Dialog(
        body=HSplit(
            [
                Box(Label(text=text)),
                Box(text_area, padding=D.exact(1)),
                progressbar,
            ]
        ),
        title=title,
        with_background=True,
    )
    app = _create_app(dialog, style)

    def set_percentage(value: int) -> None:
        progressbar.percentage = int(value)
        app.invalidate()

    def log_text(text: str) -> None:
        loop.call_soon_threadsafe(text_area.buffer.insert_text, text)
        app.invalidate()

    # Run the callback in the executor. When done, set a return value for the
    # UI, so that it quits.
    def start() -> None:
        try:
            run_callback(set_percentage, log_text)
        finally:
            app.exit()

    def pre_run() -> None:
        run_in_executor_with_context(start)

    app.pre_run_callables.append(pre_run)

    return app
def progress_dialog(title='',
                    text='',
                    run_callback=None,
                    style=None,
                    async_=False):
    """
    :param run_callback: A function that receives as input a `set_percentage`
        function and it does the work.
    """
    assert callable(run_callback)

    progressbar = ProgressBar()
    text_area = TextArea(
        focusable=False,

        # Prefer this text area as big as possible, to avoid having a window
        # that keeps resizing when we add text to it.
        height=D(preferred=10**10))

    dialog = Dialog(body=HSplit([
        Box(Label(text=text)),
        Box(text_area, padding=D.exact(1)),
        progressbar,
    ]),
                    title=title,
                    with_background=True)
    app = _create_app(dialog, style)

    def set_percentage(value):
        progressbar.percentage = int(value)
        app.invalidate()

    def log_text(text):
        text_area.buffer.insert_text(text)
        app.invalidate()

    # Run the callback in the executor. When done, set a return value for the
    # UI, so that it quits.
    def start():
        try:
            run_callback(set_percentage, log_text)
        finally:
            app.exit()

    run_in_executor(start)

    if async_:
        return app.run_async()
    else:
        return app.run()
Beispiel #4
0
animal_completer = WordCompleter([
    'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
    'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin',
    'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangaroo',
    'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
    'turtle', ], ignore_case=True)

root_container = HSplit([
    VSplit([
        Frame(body=Label(text='Left frame\ncontent')),
        Dialog(title='The custom window',
               body=Label('hello\ntest')),
        textfield,
    ], height=D()),
    VSplit([
        Frame(body=ProgressBar(),
              title='Progress bar'),
        Frame(title='Checkbox list',
              body=HSplit([
                  checkbox1,
                  checkbox2,
              ])),
        Frame(title='Radio list', body=radios),
    ], padding=1),
    Box(
        body=VSplit([
            yes_button,
            no_button,
        ], align='CENTER', padding=3),
        style='class:button-bar',
        height=3,
Beispiel #5
0
    'snake',
    'spider',
    'turkey',
    'turtle',
],
                                 ignore_case=True)

root_container = HSplit([
    VSplit([
        Frame(body=Label(text='Left frame\ncontent')),
        Dialog(title='The custom window', body=Label('hello\ntest')),
        textfield,
    ],
           height=D()),
    VSplit([
        Frame(body=ProgressBar(), title='Progress bar'),
        Frame(title='Checkbox list', body=HSplit([
            checkbox1,
            checkbox2,
        ])),
        Frame(title='Radio list', body=radios),
    ],
           padding=1),
    Box(
        body=VSplit([
            yes_button,
            no_button,
        ], align='CENTER', padding=3),
        style='class:button-bar',
        height=3,
    ),
Beispiel #6
0
def progress_log_dialog(
    title: AnyFormattedText = "",
    text: AnyFormattedText = "",
    wait_text: str = "Wait",
    quit_text: str = "Quit",
    status_text: AnyFormattedText = "",
    run_callback: Callable[[
        Callable[[int], None], Callable[[str], None], Callable[
            [str], None], Callable[[dict], None], Callable[[], bool]
    ], None] = (lambda *a: None),
    style: Optional[BaseStyle] = None,
) -> Application[None]:
    """
    :param run_callback: A function that receives as input a `set_percentage`
        function and it does the work.
    """
    loop = get_event_loop()

    def wait_handler() -> None:
        pass

    def quit_handler() -> None:
        app = get_app()
        if not app.exited:
            app.exited = True
            app.exit(result=app.result)

    wait_button = Button(text=wait_text, handler=wait_handler)
    quit_button = Button(text=quit_text, handler=quit_handler)

    progressbar = ProgressBar()
    text_area = TextArea(
        focusable=False,
        # Prefer this text area as big as possible, to avoid having a window
        # that keeps resizing when we add text to it.
        height=D(preferred=10**10),
        width=D(preferred=10**10))
    status = Label(text=status_text)

    dialog = Dialog(
        title=title,
        body=HSplit([
            Box(Label(text=text)),
            Box(text_area, padding=D.exact(1)),
            Box(status, padding=D.exact(1)),
            progressbar,
        ]),
        buttons=[wait_button, quit_button],
        with_background=True,
    )
    app = _create_app(dialog, style)
    app.result = None
    app.exited = False

    def set_percentage(value: int) -> None:
        progressbar.percentage = int(value)
        app.invalidate()

    def log_text(text: str) -> None:
        loop.call_soon_threadsafe(text_area.buffer.insert_text, text)
        app.invalidate()

    def change_status(text: str) -> None:
        status.formatted_text_control.text = text
        app.invalidate()

    def set_result(new_result: dict) -> None:
        app.result = new_result

    def get_exited() -> bool:
        return app.exited

    # Run the callback in the executor. When done, set a return value for the
    # UI, so that it quits.
    def start() -> None:
        result = None
        try:
            result = run_callback(set_percentage, log_text, change_status,
                                  set_result, get_exited)
        finally:
            if not app.exited:
                app.exited = True
                app.exit(result=result)

    def pre_run() -> None:
        run_in_executor_with_context(start)

    app.pre_run_callables.append(pre_run)

    return app
Beispiel #7
0
    ],
    ignore_case=True,
)

root_container = HSplit([
    VSplit(
        [
            Frame(body=Label(text="Left frame\ncontent")),
            Dialog(title="The custom window", body=Label("hello\ntest")),
            textfield,
        ],
        height=D(),
    ),
    VSplit(
        [
            Frame(body=ProgressBar(), title="Progress bar"),
            Frame(
                title="Checkbox list",
                body=HSplit([checkbox1, checkbox2]),
            ),
            Frame(title="Radio list", body=radios),
        ],
        padding=1,
    ),
    Box(
        body=VSplit([yes_button, no_button], align="CENTER", padding=3),
        style="class:button-bar",
        height=3,
    ),
])