Skip to content

Instantly share code, notes, and snippets.

@rchog

rchog/thing.ml Secret

Last active March 24, 2024 12:47
Show Gist options
  • Save rchog/fe2c21a5b2747d153408c1ea70338eb5 to your computer and use it in GitHub Desktop.
Save rchog/fe2c21a5b2747d153408c1ea70338eb5 to your computer and use it in GitHub Desktop.
let read_dsr_nonblock n =
let bytes = Bytes.create n in
let stdin_fd = Unix.descr_of_in_channel stdin in
let ready, _, _ = Unix.select [ stdin_fd ] [] [] 0.0001 in
if Stdlib.(ready = []) then None
else
match Unix.read stdin_fd bytes 0 n with
| exception _ -> None
| _ ->
Some
(Bytes.to_string
@@ Bytes.sub bytes ~pos:2 ~len:(Bytes.length bytes - 2))
let get_max_yx () : (int * int) option =
let parse_yx (s : string) =
String.rsplit2_exn ~on:';' s |> fun (y, x) ->
( Int.of_string y,
Int.of_string
(String.to_list x |> List.take_while ~f:Char.is_digit |> String.of_list)
)
in
Terminal.move_cursor 0 0;
Terminal.move_cursor 999 999;
printf "%s%s%!" Escape_seq.csi "6n";
Out_channel.flush stdout;
match read_dsr_nonblock 10 with None -> None | Some s -> Some (parse_yx s)
(* In raw mode and only on certain terminals: get_max_yx () => Some (24, 80) *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment