Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/tui.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ const ask = (rl) => new Promise((res) => rl.question(PROMPT(), res));
export function splitCommandLine(line) {
const parts = [];
let value = "", quote = null, escaped = false, started = false;
for (const char of String(line)) {
const input = String(line);
for (let i = 0; i < input.length; i++) {
const char = input[i];
if (escaped) {
value += char;
escaped = false;
started = true;
} else if (char === "\\" && quote !== "'") {
const next = input[i + 1];
if (quote === '"' && next !== '"' && next !== "\\") {
value += char;
started = true;
continue;
}
escaped = true;
started = true;
} else if (quote) {
Expand Down
7 changes: 7 additions & 0 deletions test/tui.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ test("TUI command parsing supports escaped whitespace and empty arguments", () =
]);
});

test("TUI command parsing preserves Windows paths inside double quotes", () => {
assert.deepEqual(splitCommandLine('/run "C:\\Users\\mosh\\script.mosh"'), [
"/run",
"C:\\Users\\mosh\\script.mosh",
]);
});

test("TUI command parsing rejects incomplete quoting", () => {
assert.throws(() => splitCommandLine('/coinpay --description "unfinished'), /unterminated/);
assert.throws(() => splitCommandLine("/ugig trailing\\"), /trailing escape/);
Expand Down
Loading