在OCaml自定义顶层设置提示 [英] Setting the prompt in an OCaml custom toplevel

查看:72
本文介绍了在OCaml自定义顶层设置提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OCaml自定义顶层中,是否可以通过编程方式将提示从#设置为其他内容?我希望能够更改它以响应用户的自定义功能的最后一个(有点像bash中的设置PS1的方式).我什至找不到更改它的#指令.谢谢!

In an OCaml custom toplevel, is there a way to programatically set the prompt from # to something else? I would like to be able to change it in response to the user's last one of my custom functions (kinda like in bash how you can set PS1). I can't even find a #directive to change it. Thanks!

推荐答案

在toplevel/toploop.ml中:

In 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, 并且此引用已导出:

But wait! The computed prompt is passed to !read_interactive_input, and this reference is exported:

在toplevel/toploop.mli中:

In toplevel/toploop.mli:

(* Hooks for external line editor *)

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

因此,您似乎所要做的就是将Toploop.read_interactive_input的值从默认值更改为忽略传递的提示并打印所需的提示的函数.

So it would seem that all you have to do is change the value of Toploop.read_interactive_input from its default to a function that ignores the passed prompt and prints the one you want instead.

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 = ()
%%%

这篇关于在OCaml自定义顶层设置提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆