Screen/tmux in ocaml?

I don’t know if I am using the right terminology here. In situations here “what is being described” conflicts with “term being used”; the former should take precedence.

I am wondering if there is a way to control/script screen/tmux in OCaml.

In particular, I want an OCaml program to maintain the following:

  • a list of processes (each with their own stdin, stdout, stderr)
  • a number of “desktops”, where only one is visible at any given time
  • each “desktop” is a list of (window, process_id), where a “window” is rowxcol of text, and process_id indexes into the list of processes above

Then, I want all keyboard input to get routed to the OCaml library; and the OCaml library decides to whether (1) interpret the key itself or (2) which process it forwards the key to.

Thanks!

I’m not sure I fully understand your requirements, but the basic techniques for building a (simple) terminal multiplexer in OCaml are described in the free book Unix system programming in OCaml chapter 5.6 Input/output multiplexing.

1 Like

The select syscall is a key for multiplexing, but to build such a screenalternative, we also have to interpret terminal escape commands and reconstitute a screen when switching. Some syscalls may be needed like terminal handling (making Ctrl-C or Ctrl-Z affect the child process not the screen process). I guess that making a screen alternative could be quite challenging. All of these (and more) could explain that the screen sources has 75 kloc (counting only *.c files).

1 Like

Boy howdy: there’s process-groups, pseudo-ttys, both tty and pty, and all that signal-handling. And then terminfo stuff. Lots and lots of stuff to get right.