Modern Apps git

← xtool.git

fix: strip trailing carriage return in console prompt on crlf terminals (#328)

Commit
b50173f7b1f7d13297b9c8229079038d47a2bb1d
Author
Bit Wolf <[email protected]>
When
2026-10-14 04:44 UTC (1 month ago)
Parents
65bf3cc
### Summary
In `Console.prompt`, incoming line input from standard input is split on
`\n` without stripping any preceding carriage return (`\r`). On Windows
environments or CRLF terminals, user input terminates with `\r\n`,
leaving a trailing `\r` on decoded input strings.

When downstream helper methods such as `Console.chooseNumber` attempt to
parse numerical selections using `Int(prompt(...))`, the trailing `\r`
(e.g., `"1\r"`) causes the conversion to return `nil`, trapping the
terminal in an infinite prompt loop.

### Changes
- In `Console.prompt` (`Sources/XToolSupport/Console.swift`), stripped
any trailing `\r` from the decoded string before returning.
- Verified that all interactive helper methods (`promptRequired`,
`getPassword`, `chooseNumber`, `choose`, and `confirm`) delegate their
standard input reading to `Console.prompt`, ensuring all input paths are
protected against CRLF leakages.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Console input no longer includes a trailing carriage return; all other
input remains unchanged.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Diff

Sources/XToolSupport/Console.swift
diff --git a/Sources/XToolSupport/Console.swift b/Sources/XToolSupport/Console.swiftindex 8dc420c..ba6136e 100644--- a/Sources/XToolSupport/Console.swift+++ b/Sources/XToolSupport/Console.swift@@ -37,7 +37,8 @@ enum Console {                     data += view                 }             }-            return String(decoding: data, as: UTF8.self)+            let str = String(decoding: data, as: UTF8.self)+            return str.hasSuffix("\r") ? String(str.dropLast()) : str         }     }