Verifying how colored terminal logs work with xterm.js

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article is created using an automated generation workflow leveraging generative AI. We reconciled existing material implementations with official documentation, organizing operation examples, constraints, and reference test results.

Verification status: Partially reconciled with official documentation and tested. Browser behavior for the fixed demo version remains unverified.

Colored terminal logs contain instructions that are not displayed on screen. We observe the instruction to start red and the instruction to reset the color separately.

Open live demo

Terminology to know first

  • Control sequencesare character sequences that control terminal display.

  • Resetis an instruction to revert colors and other settings to their initial state.

Understanding through packet diagrams

We examine a continuous byte stream by sections. The range in the diagram represents bit numbers. If color cancellation is omitted, the red color persists until the next character.

packet-beta
    0-39: "ESC[31m 赤を開始"
    40-79: "ERROR"
    80-111: "ESC[0m 色を解除"
    112-135: "空白とOK" 

Live demo operations

Execute using on and off. Observe the color of OK.

How to interpret results

If cancellation is not sent, the red color remains on OK as well.

The ESC passed to the terminal is an invisible character. Escape notation appears in the results field. Observe which characters exist between the red color instruction and its cancellation.

Key points of the code

Display the result of Terminal.write alongside the string with visible control characters.

The implementation can be found in the Implementation Code section of the demo screen.

When using in production

Separate screen colors from recorded content when saving logs.

Compare the presence or absence of the command to reset red.

The initial value of the input field is on. Clicking the run button displays ERROR and OK side by side in the terminal-like display field. ERROR is expected to be red, and OK is expected to be the default color. Next, enter off and run. Since the control sequence to reset red is not sent, the red display attribute should remain through the subsequent space and OK. The message itself is identical as ERROR OK in both cases, and the success or failure of the program does not change. The terminal used here has disableStdin:true and is not a shell that executes entered commands.

The result field displays a string formatted with JSON.stringify. This allows you to verify that ESC does not act directly as a control code by rendering it in a visible notation. Hexadecimal escapes written in JavaScript string literals and Unicode escapes appearing in JSON output use different representation formats. Both may represent the actual ESC character, and just because backslashes are visible on the screen does not mean those six characters are being sent to the terminal.

Divide the byte sequence into four parts

This demo prefixes a 5-byte control sequence combining ESC, left bracket, the number 31, and m. The subsequent ERROR is 5 bytes, the reset consisting of ESC, left bracket, 0, and m is 4 bytes, and the space and OK are 3 bytes. This totals 17 bytes. With off, the 4-byte reset is omitted, resulting in 13 bytes. In this local test, the same JavaScript string as the implementation was constructed to verify the byte count and hexadecimal notation.

PartByte countContent
ESC[31m5Set foreground color to red
ERROR5Characters to display
ESC[0m4Reset SGR attributes to default
Whitespace and OK3Next character

The packet diagram in the article represents this ASCII example in terms of bit positions. It is not an experiment to receive network packets. Since logs containing Japanese characters or emojis do not have the same character count and UTF-8 byte count, the segment lengths in this diagram are not applied as-is.

Colors are retained as terminal states

SGR is a type of control sequence that specifies character attributes for the terminal. 31 specifies red foreground color, and 0 specifies resetting SGR attributes to default. If you send the 31 specification and then fail to switch to another color or send a reset, subsequent characters received will also have the same attributes applied. Since 0 resets not only the foreground color but also other SGR attributes, consider alternative specifications if you only want to revert the foreground color.

The phenomenon of colors persisting is not necessarily limited to a single string. Even if write is called multiple times, the terminal retains its state. Since this demo creates a new terminal for each execution, it is not a test that carries over state from the previous execution. In enterprise log screens, be mindful of the start and end of each output to prevent colors or formatting from bleeding into other messages.

Method for saving enterprise logs

Simply saving colored logs to a file may record invisible control characters as well. This can cause string searches to fail to match or display unnecessary symbols in other viewers. If the design separates the generation of logs into independent data for timestamp, severity, and message, and applies colors on the screen display side, you can separate searchability from presentation. Do not rely solely on red text for error determination; instead, use explicit fields such as severity.

Control sequences do not deal solely with color; they also include cursor movement and other functions. Designing a system to directly replay untrusted logs into a terminal may cause the display to change unintentionally. Consider restricting accepted control sequences and separating the original text from the display data. Public educational materials generate only fixed fictional strings. Do not input production logs or confidential information.

If the terminal column does not appear, check the loading of modules and CSS. If colors differ from expectations, check the theme and display settings as well. This actual measurement only covers string generation and does not verify the rendering results of the demo version.

Verification scope

Generated the same strings as the implementation using Node.js 22.16.0. on=17 bytes, hex 1b5b33316d4552524f521b5b306d204f4b, off=13 bytes, hex 1b5b33316d4552524f52204f4b. Terminal rendering not executed.

Target is current browsers and the instructional material's specified xterm.js 5.5.0. Communication for external CDNs and Mermaid retrieval is required; actual display of educational materials, actual rendering of diagrams, and re-verification of the pinned library version remain. Because official documentation also includes descriptions of other versions, distinguish this from proof of pinned-version execution.

References

GitHub sample

Implementation and experimental procedure

Document information

Article title
Verifying how colored terminal logs work with xterm.js
Published
Updated
Source
https://papanda925.com/?p=18989&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL