1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""LogPane Info Toolbar classes.""" 15 16from __future__ import annotations 17import functools 18from typing import TYPE_CHECKING 19 20from prompt_toolkit.filters import Condition 21from prompt_toolkit.layout import ( 22 ConditionalContainer, 23 FormattedTextControl, 24 VSplit, 25 Window, 26 WindowAlign, 27 HorizontalAlign, 28) 29 30import pw_console.widgets.checkbox 31import pw_console.widgets.mouse_handlers 32import pw_console.style 33 34if TYPE_CHECKING: 35 from pw_console.log_pane import LogPane 36 37 38class LineInfoBar(ConditionalContainer): 39 """One line bar for showing current and total log lines.""" 40 def get_tokens(self): 41 """Return formatted text tokens for display.""" 42 tokens = ' {} / {} '.format( 43 self.log_pane.log_view.get_current_line() + 1, 44 self.log_pane.log_view.get_total_count(), 45 ) 46 return [('', tokens)] 47 48 def __init__(self, log_pane: 'LogPane'): 49 self.log_pane = log_pane 50 info_bar_control = FormattedTextControl(self.get_tokens) 51 info_bar_window = Window(content=info_bar_control, 52 align=WindowAlign.RIGHT, 53 dont_extend_width=True) 54 55 super().__init__( 56 VSplit([info_bar_window], 57 height=1, 58 style=functools.partial(pw_console.style.get_toolbar_style, 59 self.log_pane, 60 dim=True), 61 align=HorizontalAlign.RIGHT), 62 # Only show current/total line info if not auto-following 63 # logs. Similar to tmux behavior. 64 filter=Condition(lambda: not self.log_pane.log_view.follow)) 65 66 67class TableToolbar(ConditionalContainer): 68 """One line toolbar for showing table headers.""" 69 TOOLBAR_HEIGHT = 1 70 71 def __init__(self, log_pane: 'LogPane'): 72 # FormattedText of the table column headers. 73 table_header_bar_control = FormattedTextControl( 74 log_pane.log_view.render_table_header) 75 # Left justify the header content. 76 table_header_bar_window = Window( 77 content=table_header_bar_control, 78 align=WindowAlign.LEFT, 79 dont_extend_width=False, 80 ) 81 super().__init__( 82 VSplit([table_header_bar_window], 83 height=1, 84 style=functools.partial(pw_console.style.get_toolbar_style, 85 log_pane, 86 dim=True), 87 align=HorizontalAlign.LEFT), 88 filter=Condition(lambda: log_pane.table_view and log_pane.log_view. 89 get_total_count() > 0)) 90