Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put the number of shown (filtered) and received advertisements in title #36

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ If you press the **F** key, an input widget appears where you can start typing a

When you click outside the filter widget or press **Tab** to bring the focus to the next visible widget, the filter widget disappears, but the filter is still applied to limit the shown advertisements. Just press **F** again to change the filter, for instance by removing the filter with **Backspace** or changing the Bluetooth address part to filter on.

The number of filtered and received advertisements are always shown in the app's title.

Changing settings
-----------------

Expand All @@ -77,7 +79,7 @@ If you press the **S** key, you can choose which advertising data types are show
Starting and stopping the scan
------------------------------

If you press the **T** key, you stop the scan if it's running and you start the scan if it's stopped.
If you press the **T** key, you stop the scan if it's running and you start the scan if it's stopped. The scanning status is always shown in the app's title.

When the scan is running and autoscrolling is enabled in the settings, the program continuously scrolls the table with advertisements so you are always seeing the most recent results. When the scan isn't running or autoscrolling is disabled, you can scroll through the history of received advertisements with the scroll wheel, by dragging the scroll bar, or by pressing **PgUp**, **PgDown** or the arrow keys up and down when the table widget is focused.

Expand Down
29 changes: 15 additions & 14 deletions src/humble_explorer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
from rich.style import Style
from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.widgets import Checkbox, DataTable, Footer, Header, Input
Expand All @@ -27,8 +26,6 @@
__copyright__ = "Koen Vervloesem"
__license__ = "MIT"

_PAUSE_STYLE = Style(color="red", bgcolor="grey50")


class BLEScannerApp(App[None]):
"""A Textual app to scan for Bluetooth Low Energy advertisements."""
Expand Down Expand Up @@ -83,13 +80,16 @@ def __init__(self, cli_args: Namespace) -> None:

super().__init__()

def set_title(self, scanning_description: str) -> None:
"""Set the title of the app with a description of the scanning status.
def set_title(self) -> None:
"""Set the title of the app with a description of the scanning status."""
if self.scanning:
scanning_description = "Scanning"
else:
scanning_description = "Stopped"

Args:
scanning_description (str): Description of the scanning status.
"""
self.title = f"HumBLE Explorer {__version__} ({scanning_description})"
shown_advertisements = self.query_one(DataTable).row_count
all_advertisements = len(self.advertisements)
self.title = f"HumBLE Explorer {__version__} - {shown_advertisements} / {all_advertisements} ({scanning_description})"

def action_toggle_settings(self) -> None:
"""Enable or disable settings widget."""
Expand Down Expand Up @@ -256,17 +256,18 @@ def add_advertisement_to_table(
)
self.scroll_if_autoscroll()

# Always update the title: the total number of advertisements also changes if
# the advertisement isn't shown.
self.set_title()

async def start_scan(self) -> None:
"""Start BLE scan."""
self.scanning = True
self.set_title("Scanning")
self.set_title()
await self.scanner.start()

async def stop_scan(self) -> None:
"""Stop BLE scan."""
self.scanning = False
self.set_title("Stopped")
self.set_title()
await self.scanner.stop()
table = self.query_one(DataTable)
table.add_row(RichTime(datetime.now(), style=_PAUSE_STYLE))
self.scroll_if_autoscroll()
12 changes: 4 additions & 8 deletions src/humble_explorer/renderables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ class RichTime:
All times within the same second are rendered in the same color.
"""

def __init__(self, time: datetime, style: Style | None = None) -> None:
def __init__(self, time: datetime) -> None:
"""Create a RichTime object.

Args:
time (datetime): The time to show.
style (Style): The Rich style to use.
"""
self.full_time = time.strftime("%H:%M:%S.%f")
if style:
self.style = style
else:
self.style = Style(
color=EIGHT_BIT_PALETTE[hash8(time.strftime("%H:%M:%S"))].hex
)
self.style = Style(
color=EIGHT_BIT_PALETTE[hash8(time.strftime("%H:%M:%S"))].hex
)

def __rich__(self) -> Text:
"""Render the RichTime object.
Expand Down