package console
Source file terminal.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) let positive n = if n > 0 then Some n else None external window_size : Unix.file_descr -> int = "console_tty_window_size" [@@noalloc] let window_size fd = match window_size fd with | -1 -> None | size -> Some (size lsr 16, size land 0xffff) let window_size_of = function None -> None | Some fd -> window_size fd let width_of_ioctl fd () = Option.bind (window_size_of fd) (fun (w, _) -> positive w) let first ~default sources = Option.value ~default (List.find_map (fun source -> source ()) sources) (* POSIX XBD 8.3: when set, COLUMNS and LINES override the size of the terminal. There is no [tput] fallback, because it would fork just to read the termcap entry that the shell already read. *) let width fd = first ~default:80 [ (fun () -> Recording.dimension "COLUMNS"); width_of_ioctl fd ] let height_of_ioctl fd () = Option.bind (window_size_of fd) (fun (_, h) -> positive h) let height fd = first ~default:24 [ (fun () -> Recording.dimension "LINES"); height_of_ioctl fd ] let asked () = Option.map (fun w () -> (w, height None)) (Recording.dimension "COLUMNS") let dimensions fd = (width fd, height fd) let descriptor ppf = if ppf == Fmt.stdout then Some Unix.stdout else if ppf == Fmt.stderr then Some Unix.stderr else None let is_terminal fd = Unix.isatty fd && not (Colour.dumb ~getenv:Sys.getenv_opt) let of_ppf ppf = match descriptor ppf with | Some unix when is_terminal unix -> Some unix | _ -> None (* Two descriptors on one terminal have the same device. *) let device unix = match Unix.fstat unix with | st -> Some st.Unix.st_rdev | exception Unix.Unix_error _ -> None (* The standard formatters on the same terminal as [unix]. The display prints their lines above its region. A standard formatter on another terminal writes straight out. *) let routed unix = let on_display std = match of_ppf std with | Some u -> Option.equal Int.equal (device u) (device unix) | None -> false in List.filter on_display [ Fmt.stdout; Fmt.stderr ] (* macOS has hundreds of unused ttyp* to ttyw* devices. So its own ttys* devices come before the other tty* devices, such as OpenBSD's pseudo-terminals. *) let device_names ~dev ~pts = let tty n = n <> "tty" && String.starts_with ~prefix:"tty" n in let macos, others = List.partition (String.starts_with ~prefix:"ttys") (List.filter tty dev) in let in_dir dir = List.map (fun name -> dir ^ "/" ^ name) in ("/dev/tty" :: in_dir "/dev/pts" pts) @ in_dir "/dev" (macos @ others) (* [reopened unix] finds the terminal of [unix] by its device and opens it again. The last-resort sequence is then written without blocking, and without changing the flags of a descriptor that the program shares with its shell. *) let reopened unix = let rdev name = match Unix.stat name with | st when st.Unix.st_kind = Unix.S_CHR -> Some st.st_rdev | _ | (exception Unix.Unix_error _) -> None in let opened d name = match Unix.openfile name [ Unix.O_WRONLY; Unix.O_NONBLOCK; Unix.O_NOCTTY; Unix.O_CLOEXEC ] 0 with | exception Unix.Unix_error _ -> None | tty when (Unix.fstat tty).st_rdev = d -> Some tty | tty -> Unix.close tty; None in let entries dir = match Sys.readdir dir with | names -> Array.to_list names | exception Sys_error _ -> [] in match device unix with | None -> None | Some d -> (* [stat] on /dev/tty gives the device of /dev/tty itself. Only an open descriptor gives the device of the controlling terminal. *) List.find_map (fun name -> if name = "/dev/tty" || rdev name = Some d then opened d name else None) (device_names ~dev:(entries "/dev") ~pts:(entries "/dev/pts")) let file unix = match Unix.fstat unix with | st -> Some (st.Unix.st_dev, st.Unix.st_ino) | exception Unix.Unix_error _ -> None let sharing unix = let std = match (descriptor std, file unix) with | Some u, Some f -> file u = Some f | _ -> false in List.filter shares [ Fmt.stdout; Fmt.stderr ] let cursor unix visible = let s = if visible then "\027[?25h" else "\027[?25l" in try ignore (Unix.write_substring unix s 0 (String.length s)) with Unix.Unix_error _ -> () let size ppf = Option.map (fun unix () -> dimensions (Some unix)) (of_ppf ppf) (* The maximum width that a display draws, whatever the terminal reports. *) let max_width = 4096 let width_of ppf = let first (size : unit -> int * int) = fst (size ()) in if Option.is_some (Cast.file ()) then Some (first Recording.size) else match size ppf with | Some size -> Some (Int.min max_width (first size)) | None -> Option.map first (asked ())