[PR #4171] Dispatch more C0 control characters from the VT state machine #25677

Closed
opened 2026-01-31 09:11:03 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/4171

State: closed
Merged: Yes


Summary of the Pull Request

This PR moves the handling of the BEL, BS, TAB, and CR controls characters into the state machine (when in VT mode), instead of forwarding them on to the default string writer, which would otherwise have to parse them out all over again.

This doesn't cover all the control characters, but ESC, SUB, and CAN are already an integral part of the StateMachine itself; NUL is filtered out by the OutputStateMachineEngine; and LF, FF, and VT are due to be implemented as part of PR #3271.

Once all of these controls are handled at the state machine level, we can strip out all the VT-specific code from the WriteCharsLegacy function, which should simplify it considerably. This would also let us simplify the Terminal::_WriteBuffer implementation, and the planned replacement stream writer for issue #780.

References

#3271, #780, #3628, #4046

PR Checklist

  • Closes #xxx
  • CLA signed. If not, go over here and sign the CLA
  • Tests added/passed
  • Requires documentation to be updated
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: https://github.com/microsoft/terminal/issues/780#issuecomment-570287435

Detailed Description of the Pull Request / Additional comments

On the conhost side, the implementation is handled as follows:

  • The BS control is dispatched to the existing CursorBackward method, with a distance of 1.
  • The TAB control is dispatched to the existing ForwardTab method, with a tab count of 1.
  • The CR control required a new dispatch method, but the implementation was a simple call to the new _CursorMovePosition method from PR #3628.
  • The BEL control also required a new dispatch method, as well as an additional private API in the ConGetSet interface. But that's mostly boilerplate code - ultimately it just calls the SendNotifyBeep method.

On the Windows Terminal side, not all dispatch methods are implemented.

  • There is an existing CursorBackward implementation, so BS works OK.
  • There isn't a ForwardTab implementation, but TAB isn't currently required by the conpty protocol.
  • I had to implement the CarriageReturn dispatch method, but that was a simple call to Terminal::SetCursorPosition.
  • The WarningBell method I've left unimplemented, because that functionality wasn't previously supported anyway, and there's an existing issue for that (#4046).

Validation Steps Performed

I've added a state machine test to confirm that the updated control characters are now forwarded to the appropriate dispatch handlers. But since the actual implementation is mostly relying on existing functionality, I'm assuming that code is already adequately tested elsewhere. That said, I have also run various manual tests of my own, and confirmed that everything still worked as well as before.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/4171 **State:** closed **Merged:** Yes --- ## Summary of the Pull Request This PR moves the handling of the `BEL`, `BS`, `TAB`, and `CR` controls characters into the state machine (when in VT mode), instead of forwarding them on to the default string writer, which would otherwise have to parse them out all over again. This doesn't cover all the control characters, but `ESC`, `SUB`, and `CAN` are already an integral part of the `StateMachine` itself; `NUL` is filtered out by the `OutputStateMachineEngine`; and `LF`, `FF`, and `VT` are due to be implemented as part of PR #3271. Once all of these controls are handled at the state machine level, we can strip out all the VT-specific code from the `WriteCharsLegacy` function, which should simplify it considerably. This would also let us simplify the `Terminal::_WriteBuffer` implementation, and the planned replacement stream writer for issue #780. ## References #3271, #780, #3628, #4046 ## PR Checklist * [ ] Closes #xxx * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [x] Tests added/passed * [ ] Requires documentation to be updated * [x] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: https://github.com/microsoft/terminal/issues/780#issuecomment-570287435 ## Detailed Description of the Pull Request / Additional comments On the conhost side, the implementation is handled as follows: * The `BS` control is dispatched to the existing `CursorBackward` method, with a distance of 1. * The `TAB` control is dispatched to the existing `ForwardTab` method, with a tab count of 1. * The `CR` control required a new dispatch method, but the implementation was a simple call to the new `_CursorMovePosition` method from PR #3628. * The `BEL` control also required a new dispatch method, as well as an additional private API in the `ConGetSet` interface. But that's mostly boilerplate code - ultimately it just calls the `SendNotifyBeep` method. On the Windows Terminal side, not all dispatch methods are implemented. * There is an existing `CursorBackward` implementation, so `BS` works OK. * There isn't a `ForwardTab` implementation, but `TAB` isn't currently required by the conpty protocol. * I had to implement the `CarriageReturn` dispatch method, but that was a simple call to `Terminal::SetCursorPosition`. * The `WarningBell` method I've left unimplemented, because that functionality wasn't previously supported anyway, and there's an existing issue for that (#4046). ## Validation Steps Performed I've added a state machine test to confirm that the updated control characters are now forwarded to the appropriate dispatch handlers. But since the actual implementation is mostly relying on existing functionality, I'm assuming that code is already adequately tested elsewhere. That said, I have also run various manual tests of my own, and confirmed that everything still worked as well as before.
claunia added the pull-request label 2026-01-31 09:11:03 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#25677