Something went wrong. Try again.
OCaml port of Linenoise
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182{0:manual Manual}The user manual provides some patterns for writing certain kinds of lineeditors.{1:history History}The command {! history} is a simple function whose argument is the currentinput to the line editor and expects a list of strings of historical commands.{! Bruit} does not control saving or restoring command histories, that is up to theend user.A simple history that uses the current input as a prefix filter might look something likethis:{[let h = ref []let history prefix = if prefix <> "" then List.filter (fun s -> String.starts_with ~prefix s) !h else !h]}Within the command loop, you can save successful commands into the reference.{1:hints Hints}Hints are overlayed onto the current line editor. Supplying hints is fairlystraightforward using a {! Bruit.hint} function. This produces a string tooverlay alongside an {! Fmt.style}. Without the style it would be hard todistinguish between what the user has written and what is being hinted at.Often you will want to {! String.sub} the input from your hint:{[let hint s = if s = "" then None else if String.starts_with ~prefix:s "hello" then let len = String.length s in Some (String.sub "hello" len (5 - len), `Fg `Cyan) else None]} {1:colours Using colours}Different parts of the line editor can make use of colours in the terminal usingANSI escape sequences. Depending on what you are trying to achieve, you can do thisin different ways.For example, to style your prompt using ANSI escape sequences you can write them directlyinto the string you pass as the prompt to {! Bruit.run}.{[let prompt = if sys_break then "[\x1b[31m130\x1b[0m] \x1b[33m>>\x1b[0m " else "\x1b[33m>>\x1b[0m "]}If you then link in {{: https://erratique.ch/software/fmt/doc/Fmt_tty}[Fmt_tty]} you can set upthe outputs:{[let () = Fmt_tty.setup_std_outputs (); (* ... *)]}{2 ANSI in the browser}If you are using your line editor in the browser, {b and} you wish to have colours, then you will haveto ensure your HTML page has the {! Ansi.css} stylesheet (or a custom version) loaded.{[let () = El.append_children (Document.head G.document) [ El.style [ El.txt' Ansi.css ] ];]}