How pythops/bluetui works
Project type and purpose
• bluetui is a single-binary, terminal user interface (TUI) application written in Rust for managing Bluetooth adapters and devices on Linux.
• It interacts with the OS’s BlueZ stack (via D-Bus) and the rfkill kernel interface to scan, pair, trust, and connect/disconnect devices.
File structure
• Top-level Rust project (Cargo.toml, src/)
• src/cli.rs – parses command-line flags (e.g. custom config path)
• src/config.rs – loads and validates TOML configuration from $HOME/.config/bluetui/config.toml
• src/bluetooth.rs – wraps BlueZ D-Bus calls for scan, pair, connect, etc.
• src/rfkill.rs – interfaces with rfkill to toggle adapter power/discovery
• src/event.rs – defines events for user input, BlueZ callbacks, rfkill notifications
• src/handler.rs – dispatches events to action functions in bluetooth.rs and rfkill.rs
• src/app.rs – wires together CLI, config, event loop, handlers, UI and notifications
• src/ui.rs – high-level layout of TUI sections (Adapters, Paired, New devices)
• src/tui.rs – lower-level terminal-drawing engine (based on tui-rs crate)
• src/notification.rs– triggers desktop notifications via libnotify or similar
• src/spinner.rs – manages “scanning” spinner animations
• src/confirmation.rs– pop-up dialog for rename, unpair confirmation
• src/main.rs – entry point: calls app::run()
README
• Confirms dependencies: BlueZ, optional nerdfonts
• Describes keybindings and features (scan, pair, trust, power toggle)
Components & interactions
1. CLI Parser
2. Config Loader
3. Event Loop & Dispatcher
4. Bluetooth Controller (BlueZ D-Bus)
5. RFKill Controller (kernel rfkill)
6. TUI Renderer (tui-rs)
7. UI Widgets / Dialogs
8. Notification Service (libnotify)
9. Spinner Manager
10. Confirmation Dialog
• CLI Parser → Config Loader (passes config path)
• Config Loader → App Core (supplies user settings)
• App Core ↔ Event Loop (registers input sources and handlers)
• Event Loop → Bluetooth Controller (on “scan”, “pair”, “connect”)
• Event Loop → RFKill Controller (on “power on/off”, “discovery toggle”)
• Bluetooth Controller → Event Loop (on device found/paired state changes)
• RFKill Controller → Event Loop (on adapter state change)
• Event Loop → UI Widgets (triggers UI state update)
• Event Loop → Notification Service (e.g. “device connected”)
• Spinner Manager → TUI Renderer (updates spinner)
• Confirmation Dialog ↔ Event Loop (captures user confirmation)
c. Architectural patterns & design principles
• MVC-style separation:
• Model = current Bluetooth/rfkill state maintained by App Core
• View = TUI Renderer + UI Widgets
• Controller = Event Loop + Handlers
• Event-driven loop for decoupling I/O from business logic
• Single responsibility per module (config, CLI, UI, notifications)
d. Key technologies & libraries
• Rust & Cargo for build
• BlueZ D-Bus API (via dbus-crate or zbus)
• rfkill subsystem (via ioctl or sysfs)
• tui-rs crate for terminal UI
• clap or structopt for CLI parsing
• toml for config parsing
• libnotify or notify-rust for desktop notifications
Detail is critical
• Of the src/*.rs modules as components.
• D-Bus and kernel interfaces externally.
• Event loop as the heart of the MVC pattern.
• Avoid over-simplification by including config loading, CLI parsing, and notifications as first-class parts of the system.