Skip to content

feat: Handle CPR and colors in shell output#23

Merged
abaczek merged 1 commit intomainfrom
cpr_and_colors_in_shell
Apr 14, 2026
Merged

feat: Handle CPR and colors in shell output#23
abaczek merged 1 commit intomainfrom
cpr_and_colors_in_shell

Conversation

@adrianlasota
Copy link
Copy Markdown
Contributor

Some platforms can use CPR (Cursor Position Report) to report the cursor position in the terminal. This can cause issues when processing shell output, as it may include CPR sequences. Additionally, some platforms may include color codes in the shell output, which can also interfere with processing.
This pull request significantly improves the robustness and flexibility of Telnet login handling, especially for terminals emitting ANSI/VT control sequences in prompts. The changes introduce fallback prompt detection, enhanced handling of cursor position requests, and improved output cleaning. The test suite is updated to reflect the new logic and ensure reliability.

Telnet login and prompt handling improvements:

  • Added fallback prompt detection for ANSI/VT escape sequence prompts using new patterns and regexes, allowing the Telnet client to correctly detect shell prompts even when they are colored or contain control sequences. (mfd_connect/telnet/telnet.py, mfd_connect/util/serial_utils.py, mfd_connect/util/__init__.py) [1] [2] [3]
  • Implemented a recovery mechanism for login prompt detection, including handling cursor position requests (CPR) and retrying prompt detection with configurable retries and timeouts. (mfd_connect/telnet/telnet.py, mfd_connect/util/serial_utils.py) [1] [2]
  • Updated the Telnet login flow to support optional passwords, improved pattern matching for login and shell prompts, and added detailed debug logging for each step. (mfd_connect/telnet/telnet.py) [1] [2]

Output cleaning and usability:

  • Added stripping of ANSI escape sequences and carriage returns from command output to ensure clean and readable logs. (mfd_connect/telnet/telnet.py, mfd_connect/util/serial_utils.py) [1] [2]

Test updates:

  • Updated and expanded unit tests to match the new login and prompt detection logic, including handling of fallback prompts, retries, and improved assertion strategies. (tests/unit/test_mfd_connect/test_telnet/test_telnet.py) [1] [2] [3] [4] [5]

These changes make the Telnet connection process more robust against a wider variety of shell environments and improve maintainability and observability through better logging and test coverage.

Some platforms can use CPR (Cursor Position Report) to report the cursor position in the terminal. This can cause issues when processing shell output, as it may include CPR sequences. Additionally, some platforms may include color codes in the shell output, which can also interfere with processing.

Signed-off-by: Lasota, Adrian <adrian.lasota@intel.com>
Copilot AI review requested due to automatic review settings April 10, 2026 09:48
@mfd-intel-bot
Copy link
Copy Markdown
Contributor

We don't publish DEVs .whl.
To build .whl, run 'pip install git+https://github.com/intel/mfd-connect@cpr_and_colors_in_shell'

@coveralls-official
Copy link
Copy Markdown

coveralls-official bot commented Apr 10, 2026

Coverage Status

coverage: 77.914% (+0.1%) from 77.816% — cpr_and_colors_in_shell into main

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves Telnet login/prompt detection robustness by handling ANSI/VT control sequences (including CPR) and cleaning command output (ANSI + carriage returns), with corresponding test updates.

Changes:

  • Add ANSI-aware shell prompt fallback matching and CPR-response recovery logic during login/prompt detection.
  • Strip ANSI escape sequences and \r from captured command output for cleaner logs/processing.
  • Update unit tests to reflect the new login/prompt flow and add coverage for ANSI stripping / recovery logic.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
mfd_connect/telnet/telnet.py Adds ANSI fallback prompt matching, CPR recovery during login, and output cleaning.
mfd_connect/util/serial_utils.py Introduces shared regex/constants for ANSI escape handling, CPR detection/response, and login recovery tuning.
mfd_connect/util/__init__.py Re-exports newly added serial/telnet-related constants for consumption across the package.
tests/unit/test_mfd_connect/test_telnet/test_telnet.py Updates login/prompt tests and adds new tests for ANSI stripping, CPR recovery, and output cleaning.
mfd_connect/serial.py Adjusts type hint for serial_logs_path to properly include None.
Comments suppressed due to low confidence (1)

mfd_connect/telnet/telnet.py:181

  • In _establish_telnet_connection(), the login is skipped when password is None (self._password is not None). This conflicts with the new “optional password” flow: if a platform requires only a username (or a blank password), the code will never call _login() and will remain at the login prompt. Consider triggering _login() whenever a username is provided (and not in pre-OS), and let _enter_credentials() decide whether to wait for a password prompt based on self._password.
        for _ in range(self._execution_retries):
            try:
                logger.log(level=log_levels.MODULE_DEBUG, msg="Trying to connect to target...")
                self._connect()
                if self.console.is_connected():
                    if not self._in_pre_os() and (self._username and self._password is not None):
                        logger.log(level=log_levels.MODULE_DEBUG, msg="Trying to login to console...")
                        self._login()
                        return
                    else:
                        logger.log(
                            level=log_levels.MODULE_DEBUG, msg="Credentials weren't provided. Skipping login part"
                        )

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/unit/test_mfd_connect/test_telnet/test_telnet.py
Comment thread tests/unit/test_mfd_connect/test_telnet/test_telnet.py
@abaczek abaczek merged commit f273e2e into main Apr 14, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants