Skip to content

Commit a1cbada

Browse files
committed
paste clipboard in chunks
1 parent 71aae45 commit a1cbada

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ fn call_fn(
735735
info!("Handling paste_clipboard_in_chunks function call.");
736736
let args: serde_json::Value = serde_json::from_str(fn_args).unwrap();
737737
let chunk_size = args["chunk_size"].as_u64().unwrap_or(1000) as usize;
738-
738+
739739
match paste_clipboard_in_chunks(chunk_size) {
740740
Ok(message) => Some(message),
741741
Err(e) => Some(format!("Failed to paste clipboard in chunks: {}", e)),
@@ -1061,59 +1061,59 @@ fn paste_clipboard_in_chunks(chunk_size: usize) -> Result<String, String> {
10611061

10621062
// Get the current clipboard contents
10631063
let clipboard_text = get_clipboard_string()?;
1064-
1064+
10651065
if clipboard_text.is_empty() {
10661066
return Ok("Clipboard is empty, nothing to paste".to_string());
10671067
}
10681068

10691069
let mut clipboard: ClipboardContext =
10701070
ClipboardProvider::new().map_err(|e| format!("Failed to initialize clipboard: {}", e))?;
1071-
1071+
10721072
let mut enigo = Enigo::new();
1073-
1073+
10741074
// Split the text into chunks
10751075
let chars: Vec<char> = clipboard_text.chars().collect();
10761076
let total_chars = chars.len();
10771077
let mut chunks_pasted = 0;
1078-
1078+
10791079
let mut i = 0;
10801080
while i < total_chars {
10811081
let end = std::cmp::min(i + chunk_size, total_chars);
10821082
let chunk: String = chars[i..end].iter().collect();
1083-
1083+
10841084
// Set the clipboard to this chunk
10851085
clipboard
10861086
.set_contents(chunk.clone())
10871087
.map_err(|e| format!("Failed to set clipboard contents: {}", e))?;
1088-
1088+
10891089
// Wait a moment for the clipboard to be set
10901090
std::thread::sleep(std::time::Duration::from_millis(50));
1091-
1091+
10921092
// Emulate Ctrl+V
10931093
enigo.key_down(enigo::Key::Control);
10941094
enigo.key_click(enigo::Key::Layout('v'));
10951095
enigo.key_up(enigo::Key::Control);
1096-
1096+
10971097
// Wait a moment for the paste to complete
10981098
std::thread::sleep(std::time::Duration::from_millis(50));
1099-
1099+
11001100
// Press Enter to send the message
11011101
enigo.key_click(enigo::Key::Return);
1102-
1102+
11031103
chunks_pasted += 1;
11041104
i = end;
1105-
1105+
11061106
// Wait at least 100ms before next chunk
11071107
if i < total_chars {
11081108
std::thread::sleep(std::time::Duration::from_millis(100));
11091109
}
11101110
}
1111-
1111+
11121112
// Restore the original clipboard contents
11131113
clipboard
11141114
.set_contents(clipboard_text)
11151115
.map_err(|e| format!("Failed to restore clipboard contents: {}", e))?;
1116-
1116+
11171117
Ok(format!(
11181118
"Successfully pasted and sent {} characters as {} separate message(s) with chunks of up to {} characters each",
11191119
total_chars, chunks_pasted, chunk_size

0 commit comments

Comments
 (0)