Skip to main content

resq-tui

Version: v0.1.8 · License: Apache-2.0 · Crate: crates.io · API docs: docs.rs
Crates.io License Shared TUI component library for all ResQ developer tools. Provides a unified theme system, console formatters, table rendering, progress bars, spinners, and terminal lifecycle management built on Ratatui and Crossterm.

Overview

resq-tui ensures every ResQ tool (resq-logs, resq-perf, resq-flame, resq-health, etc.) shares a consistent visual identity and interaction model. It provides two tiers of output:
  • Full-screen TUI — Ratatui-based widgets (header, footer, tabs, popups) with a standardized theme for interactive terminal applications.
  • Non-TUI CLI — Styled console formatters, tables, progress bars, and spinners for traditional command-line output that gracefully degrade when piped or redirected.
All styling is gated through environment detection so ANSI codes never bleed into pipes, redirects, or screen-reader environments.

Architecture

Module dependency flow

Installation

Add to your Cargo.toml:
Or from crates.io:

Module Reference

lib.rs — Core Widgets and Utilities

The root module re-exports crossterm and ratatui for convenience and provides the original Theme struct alongside shared TUI drawing functions.

Theme (root)

The original hardcoded dark-palette theme struct, retained for backward compatibility. For new code, prefer theme::Theme::adaptive() (see below).

Widget Functions

draw_header
Renders a standardized header bar with service name, status badge, PID, and URL.
Signature:
draw_footer
Renders a keyboard-shortcut footer bar.
Signature:
draw_tabs
Renders a tab bar with selection highlight. Uses the default theme internally.
Signature:
draw_popup
Renders a centered modal overlay for help dialogs or error messages.
Signature:
centered_rect
Helper that computes a centered Rect given percentage dimensions. Used internally by draw_popup.

Utility Functions

format_bytes
Converts a byte count to a human-readable string using binary units (KiB, MiB, GiB).
format_duration
Converts seconds to a human-readable duration string.
SPINNER_FRAMES
Braille animation frames for TUI spinner widgets:

theme — Adaptive Color System

The theme module provides a Dracula-inspired adaptive color palette that switches between light and dark variants based on terminal environment detection.

AdaptiveColor

A color pair with light and dark variants that resolves at runtime via detect_color_mode().

Palette Constants

Theme (theme module)

Extended theme struct with adaptive color support.

detect — Terminal Environment Detection

Detects TTY status, color support, and accessibility mode. All detection is cached per-process via OnceLock.

Environment Variables

ColorMode

Public Functions


console — Styled Message Formatters

TTY-gated console formatters for non-TUI CLI output. Diagnostics go to stderr, structured data to stdout. All styling respects detect::should_style().

Format Functions (return String)

Convenience wrappers that call the corresponding format_* function and print to stderr:

table — CLI Table Renderer

Renders styled tables to stderr with zebra-striped rows, auto-computed column widths, and adaptive colors. Falls back to plain aligned text when styling is disabled.

Align

Column

Builder for table column definitions.

render_table

Renders a complete table to stderr.
Output (styled):

progress — CLI Progress Bar

Non-TUI progress bar rendered to stderr with adaptive gradient colors. Falls back to plain ASCII in non-TTY mode.

ProgressBar

TTY output: Downloading ████████████████░░░░░░░░░░░░░░░░░░░░░░░░ 40% Non-TTY output: Downloading [################------------------------] 40%

spinner — Threaded CLI Spinner

Thread-safe stderr spinner that respects TTY and accessibility settings. Uses braille animation by default with a plain-dots fallback.

SPINNER_FRAMES

Braille frames used by both the TUI spinner constant and the non-TUI Spinner:

Spinner

The spinner is also stopped automatically on Drop.
In non-TTY mode, start() prints "Fetching service health..." once and returns immediately.

terminal — Terminal Lifecycle Management

Manages raw mode, alternate screen, and provides a standard event loop for Ratatui applications.

Type Alias

init() -> anyhow::Result<Term>

Enables raw mode, enters the alternate screen, and returns an initialized Term.

restore()

Leaves the alternate screen and disables raw mode. Safe to call even in a partially-initialized state.

TuiApp Trait

Implement this trait on your application state to use run_loop.
Return false from handle_key to exit the event loop. Ctrl+C always exits.

run_loop

Runs a standard TUI event loop. poll_ms controls input polling frequency.

Integration Guide

Building a new ResQ TUI tool

  1. Add the dependency to your crate’s Cargo.toml:
  1. Implement TuiApp on your application state:
  1. Run the event loop in main:

Using non-TUI console output

For CLI tools that do not need a full-screen TUI:

Accessibility

resq-tui respects the following standards:
  • NO_COLOR (no-color.org) — disables all ANSI color codes
  • TERM=dumb — plain text output only
  • ACCESSIBLE — activates screen-reader-friendly output (plain dot spinners, no animation)
  • Non-TTY pipes and redirects receive unstyled output automatically

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Modules

Click through to each module for the full rustdoc-rendered API surface (types, traits, functions, methods).