Kitty Graphics Protocol in OCaml
terminal graphics ocaml
1(* Debug: Output animation escape sequences for comparison with Go *) 2 3module K = Kitty_graphics 4 5let solid_color_rgba ~width ~height ~r ~g ~b ~a = 6 let pixels = Bytes.create (width * height * 4) in 7 for i = 0 to (width * height) - 1 do 8 let idx = i * 4 in 9 Bytes.set pixels idx (Char.chr r); 10 Bytes.set pixels (idx + 1) (Char.chr g); 11 Bytes.set pixels (idx + 2) (Char.chr b); 12 Bytes.set pixels (idx + 3) (Char.chr a) 13 done; 14 Bytes.to_string pixels 15 16let send cmd ~data = 17 let s = K.Command.to_string cmd ~data in 18 (* Print escaped version for debugging *) 19 String.iter (fun c -> 20 let code = Char.code c in 21 if code = 27 then print_string "\\x1b" 22 else if code < 32 || code > 126 then Printf.printf "\\x%02x" code 23 else print_char c 24 ) s; 25 print_newline () 26 27let () = 28 let width, height = 80, 80 in 29 let image_id = 300 in 30 31 print_endline "=== OCaml Animation Debug ===\n"; 32 33 (* Step 1: Transmit base frame *) 34 print_endline "1. Transmit base frame (a=t):"; 35 let red_frame = solid_color_rgba ~width ~height ~r:255 ~g:0 ~b:0 ~a:255 in 36 send 37 (K.Command.transmit 38 ~image_id 39 ~format:`Rgba32 40 ~width ~height 41 ~quiet:`Errors_only 42 ()) 43 ~data:red_frame; 44 print_newline (); 45 46 (* Step 2: Add frame *) 47 print_endline "2. Add frame (a=f):"; 48 let orange_frame = solid_color_rgba ~width ~height ~r:255 ~g:165 ~b:0 ~a:255 in 49 send 50 (K.Command.frame 51 ~image_id 52 ~format:`Rgba32 53 ~width ~height 54 ~frame:(K.Frame.make ~gap_ms:100 ~composition:`Overwrite ()) 55 ~quiet:`Errors_only 56 ()) 57 ~data:orange_frame; 58 print_newline (); 59 60 (* Step 3: Put/display placement *) 61 print_endline "3. Create placement (a=p):"; 62 send 63 (K.Command.display 64 ~image_id 65 ~placement:(K.Placement.make 66 ~placement_id:1 67 ~cell_x_offset:0 68 ~cell_y_offset:0 69 ~cursor:`Static 70 ()) 71 ~quiet:`Errors_only 72 ()) 73 ~data:""; 74 print_newline (); 75 76 (* Step 4: Set root frame gap *) 77 print_endline "4. Set root frame gap (a=a,r=1,z=100):"; 78 send 79 (K.Command.animate ~image_id (K.Animation.set_gap ~frame:1 ~gap_ms:100)) 80 ~data:""; 81 print_newline (); 82 83 (* Step 5: Animate *) 84 print_endline "5. Start animation (a=a,s=3,v=1):"; 85 send 86 (K.Command.animate ~image_id (K.Animation.set_state ~loops:1 `Run)) 87 ~data:""; 88 print_newline (); 89 90 (* Step 6: Stop animation *) 91 print_endline "6. Stop animation:"; 92 send 93 (K.Command.animate ~image_id (K.Animation.set_state `Stop)) 94 ~data:""