Настройка приглашения в обычном доске OCaml

В обычном toplevel OCaml существует ли способ программно установить приглашение от # на что-то еще? Я хотел бы иметь возможность изменить его в ответ на последнюю из моих пользовательских функций (вроде как в bash, как вы можете установить PS1). Я даже не могу найти #directive, чтобы изменить его. Спасибо!

Ответ 1

В toplevel/toploop.ml:

let prompt =
  if !Clflags.noprompt then ""
  else if !first_line then "# "
  else if Lexer.in_comment () then "* "
  else "  "
in

Но подождите! Вычисленное приглашение передается в !read_interactive_input, и эта ссылка экспортируется:

В toplevel/toploop.mli:

(* Hooks for external line editor *)

val read_interactive_input : (string -> string -> int -> int * bool) ref

Итак, все, что вам нужно сделать, это изменить значение Toploop.read_interactive_input по умолчанию на функцию, которая игнорирует переданное приглашение и печатает тот, который вы хотите.

Значение по умолчанию для read_interactive_input:

let read_input_default prompt buffer len =
  output_string Pervasives.stdout prompt; flush Pervasives.stdout;
  let i = ref 0 in
  try
    while true do
      if !i >= len then raise Exit;
      let c = input_char Pervasives.stdin in
      buffer.[!i] <- c;
      incr i;
      if c = '\n' then raise Exit;
    done;
    (!i, false)
  with
  | End_of_file ->
      (!i, true)
  | Exit ->
      (!i, false)

Итак, вы можете использовать:

# let my_read_input prompt buffer len =
  output_string Pervasives.stdout  "%%%" ; flush Pervasives.stdout;
  let i = ref 0 in
  try
    while true do
      if !i >= len then raise Exit;
      let c = input_char Pervasives.stdin in
      buffer.[!i] <- c;
      incr i;
      if c = '\n' then raise Exit;
    done;
    (!i, false)
  with
  | End_of_file ->
      (!i, true)
  | Exit ->
      (!i, false)
                                  ;;
val my_read_input : 'a -> string -> int -> int * bool = <fun>
# Toploop.read_interactive_input := my_read_input ;;
- : unit = ()
%%%