Kitty Graphics Protocol in OCaml
terminal graphics ocaml
1(*--------------------------------------------------------------------------- 2 Copyright (c) 2025 Anil Madhavapeddy. All rights reserved. 3 SPDX-License-Identifier: ISC 4 ---------------------------------------------------------------------------*) 5 6(* Tmux Passthrough Support - Implementation *) 7 8let is_active () = 9 Option.is_some (Sys.getenv_opt "TMUX") 10 11let write_wrapped buf s = 12 (* DCS passthrough prefix: ESC P tmux ; *) 13 Buffer.add_string buf "\027Ptmux;"; 14 (* Double all ESC characters in the content *) 15 String.iter (fun c -> 16 if c = '\027' then Buffer.add_string buf "\027\027" 17 else Buffer.add_char buf c 18 ) s; 19 (* DCS terminator: ESC \ *) 20 Buffer.add_string buf "\027\\" 21 22let wrap_always s = 23 let buf = Buffer.create (String.length s * 2 + 10) in 24 write_wrapped buf s; 25 Buffer.contents buf 26 27let wrap s = 28 if is_active () then wrap_always s else s