From 23e3ab6d3b3f8b049fd201f3e3f85722a640cf6d Mon Sep 17 00:00:00 2001 From: none23 Date: Sat, 22 Aug 2026 10:26:55 +0400 Subject: [PATCH] Avoid wtype keycode collisions --- baml_src/delivery.baml | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/baml_src/delivery.baml b/baml_src/delivery.baml index 94b6a43..8e58386 100644 --- a/baml_src/delivery.baml +++ b/baml_src/delivery.baml @@ -17,9 +17,40 @@ function run_delivery_command(program: string, args: string[], stdin: string?) - output.ok() } +// Chromium treats wtype's fourteenth generated keycode as Backspace. +function wtype_chunks(transcript: string) -> string[] { + let chunks: string[] = []; + let chunk = ""; + let characters: string[] = []; + for (let character in transcript.chars()) { + if (!characters.includes(character) && characters.length() == 13) { + chunks.push(chunk); + chunk = ""; + characters = []; + } + chunk += character; + if (!characters.includes(character)) { + characters.push(character) + } + } + if (chunk != "") { + chunks.push(chunk) + } + chunks +} + +function type_text(transcript: string) -> bool { + for (let chunk in wtype_chunks(transcript)) { + if (!run_delivery_command("wtype", ["-"], chunk)) { + return false; + } + } + true +} + function deliver_text(transcript: string, mode: DeliveryMode) -> bool { match (mode) { - DeliveryMode.Type => run_delivery_command("wtype", [transcript], null), + DeliveryMode.Type => type_text(transcript), DeliveryMode.Copy => { run_delivery_command("wl-copy", [], transcript) || run_delivery_command("xclip", ["-selection", "clipboard"], transcript) @@ -27,3 +58,15 @@ function deliver_text(transcript: string, mode: DeliveryMode) -> bool { }, } } + +test "wtype chunks avoid the Chromium backspace keycode" { + assert.equal( + wtype_chunks("Okay, I think we're ready to do steps three and four."), + ["Okay, I think we", "'re ready to do steps three and ", "four."], + ) +} + +test "wtype chunks split only for a fourteenth distinct character" { + assert.equal(wtype_chunks("abcdefghijklmmlkjihgfedcba"), ["abcdefghijklmmlkjihgfedcba"]); + assert.equal(wtype_chunks("abcdefghijklmn"), ["abcdefghijklm", "n"]) +}