Skip to content

feat: detach option to leave comchan make changes to code, build/flash it and come back to comchan without exiting it #106

Description

@Vaishnav-Sabari-Girish

Is there an existing issue for this?

  • I have searched the existing issues

Problem Statement (The Pain)

So the problem is that to edit code, build/flash it, I should exit comchan, do the things and re-enter comchan.

Suggested Solution

A configurable keybind should allow the user to temporarily detach from the current comchan session and return to their shell without terminating or suspending the comchan process.

This should work regardless of whether comchan is currently running in Monitor mode (CLI) or Plotter mode (TUI).

The intended workflow would be:

  1. Press the detach keybind while inside comchan.
  2. comchan should:
    • Preserve the current session and active mode.
    • Restore the terminal state if currently in Plotter/TUI mode.
    • Temporarily relinquish control of the terminal and return the user to their shell.
    • Continue running in the background.
    • Continue monitoring the serial/debug connection where possible.
  3. The user can then make changes to their firmware, rebuild it, and flash it to the target.
  4. Press the reattach keybind to return directly to the existing comchan session without requiring fg.
  5. comchan should:
    • Return to whichever mode was active before detaching.
    • Restore the terminal/TUI state when returning to Plotter mode.
    • Continue displaying logs from the target.
    • Recover/reconnect to the serial device if it disconnected or re-enumerated during flashing.

The existing Ctrl+P behavior should remain unchanged and continue to switch between Monitor and Plotter modes.

Expected behavior:

                         Ctrl+P
                ┌──────────────────────┐
                ▼                      │
        ┌───────────────┐      ┌───────────────┐
        │    Monitor    │      │    Plotter    │
        │     (CLI)     │      │     (TUI)     │
        └───────────────┘      └───────────────┘
                │                      │
                └──────────┬───────────┘
                           │
                     Detach keybind
                           ▼
                     ┌───────────┐
                     │   Shell   │
                     └───────────┘
                           │
                    Reattach keybind
                           ▼
                  Previous ComChan mode

Example workflow

$ comchan -p /dev/ttyACM0

[INFO] Starting...
[INFO] Sensor initialized
[INFO] Sampling...

<detach keybind>

$ nvim src/main.c
$ west build ...
$ west flash

<reattach keybind>

[INFO] Booting newly flashed firmware...
[INFO] Sensor initialized
[INFO] New firmware running...

Useful Resources

  • Trix Player / TerminalMusicPlayer

    • Rust + Ratatui terminal music player that implements similar terminal/session behavior. Useful as a reference for investigating how the application relinquishes and restores terminal control while continuing to run.
  • Ratatui — Terminal Initialization

    • Covers terminal initialization, raw mode, alternate screen handling, and restoring terminal state when leaving a Ratatui interface.
  • Ratatui Documentation

    • API reference for managing and redrawing the Plotter TUI when detaching and reattaching.
  • Crossterm — Terminal Module

    • APIs for enabling/disabling raw mode and entering/leaving the alternate screen.
  • Crossterm — Event Module

    • Keyboard and terminal event handling. Relevant for implementing the detach keybind and ComChan's existing Ctrl+P mode switching.
  • Rust std::process

    • Standard Rust process-management functionality. Useful when investigating how ComChan can coexist with an interactive shell while remaining alive.
  • nix Crate

    • Rust bindings for Unix/POSIX APIs, including processes, file descriptors, signals, sessions, process groups, and terminal-related functionality.
  • nix::unistd

    • APIs around Unix processes, process groups, sessions, and file descriptors that may be required for implementing detach/reattach behavior.
  • nix::sys::termios

    • Low-level terminal configuration and terminal-state management.
  • POSIX — General Terminal Interface

    • Defines controlling terminals, foreground/background process groups, terminal access, and job-control behavior. Important for understanding the constraints around allowing the shell and ComChan to interact with the same terminal.
  • GNU Bash — Job Control

    • Useful background on how shells manage foreground and background processes and why a custom detach/reattach mechanism differs from conventional Ctrl+Z + fg.
  • VecDeque — Rust Standard Library

    • Suitable for implementing a bounded FIFO buffer for logs received while ComChan is detached.
  • Tokio broadcast Channel

    • Potentially useful if ComChan's architecture needs multiple consumers of incoming telemetry/log data, such as the monitor, plotter, and detached-session buffering logic.

Additional Information

No response

Self Grab

  • I'm ready to work on this issue!

Metadata

Metadata

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions