So it removes '\n’s from clipboard buffer, so I can paste the content to any translation software later. If I don’t remove the NEWLINEs, the translator softwares could give me wrong translations
let () = print_endline "Removing NEWLINE from clipboard..."
let run cmd: string =
let fd = Unix.open_process_in cmd in
let () = Printf.printf "[i] Running command '%s'\n" cmd in
let text = In_channel.input_all fd in
let _ = Unix.close_process_in fd in
text
let remove_newline str: string =
str
|> String.map
@@ fun c -> if c = '\n' then ' ' else c
let () =
let () = print_endline "[i] Initializing main" in
let global_buffer: string ref = ref "(empty)" in
while true do
let () = Unix.sleep 1 in
run "pbpaste"
|> remove_newline
|> fun str -> if str = !global_buffer then
let () = print_endline " No change found." in
()
else
let () = global_buffer := str in
Printf.printf "[i] Buffer updated.\n\n<><><>Content<><><>\n%s\n" !global_buffer
done
I’m using macOS Big Sur so I used pbcopy
and pbpaste