Skip to content
Merged
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
45 changes: 44 additions & 1 deletion baml_src/delivery.baml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,56 @@ 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)
|| run_delivery_command("xsel", ["--clipboard", "--input"], transcript)
},
}
}

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"])
}