|
| 1 | +Linenoise in OCaml |
| 2 | +====================== |
| 3 | + |
| 4 | +# Benefits |
| 5 | +1. BSD licensed |
| 6 | +2. No system dependencies, no need for `readline` on your machine |
| 7 | +3. Related to 2, these bindings are self-contained, the source for |
| 8 | + `linenoise` is in this repo and compiled all together with the |
| 9 | + `OCaml` |
| 10 | +4. Written in OCaml |
| 11 | + |
| 12 | +# Installation |
| 13 | + |
| 14 | +It is easy with `opam` |
| 15 | + |
| 16 | +```shell |
| 17 | +$ opam install linenoise |
| 18 | +``` |
| 19 | + |
| 20 | +# Example code |
| 21 | +This example is also included in the repo under examples: |
| 22 | + |
| 23 | +```OCaml |
| 24 | +let rec user_input prompt cb = |
| 25 | + match LNoise.linenoise prompt with |
| 26 | + | None -> () |
| 27 | + | Some v -> |
| 28 | + cb v; |
| 29 | + user_input prompt cb |
| 30 | +
|
| 31 | +let () = |
| 32 | + LNoise.history_load ~filename:"history.txt" |> ignore; |
| 33 | + LNoise.history_set ~max_length:100 |> ignore; |
| 34 | + LNoise.set_completion_callback begin fun line_so_far ln_completions -> |
| 35 | + if line_so_far <> "" && line_so_far.[0] = 'h' then |
| 36 | + ["Hey"; "Howard"; "Hughes";"Hocus"] |
| 37 | + |> List.iter (LNoise.add_completion ln_completions); |
| 38 | + end; |
| 39 | + ["These are OCaml bindings to linenoise"; |
| 40 | + "get tab completion with <TAB>, type h then hit <TAB>"; |
| 41 | + "type quit to exit gracefully"; |
| 42 | + "By Edgar Aroutiounian\n"] |
| 43 | + |> List.iter print_endline; |
| 44 | + (fun from_user -> |
| 45 | + if from_user = "quit" then exit 0; |
| 46 | + LNoise.history_add from_user |> ignore; |
| 47 | + LNoise.history_save ~filename:"history.txt" |> ignore; |
| 48 | + Printf.sprintf "Got: %s" from_user |> print_endline |
| 49 | + ) |
| 50 | + |> user_input "test_program> " |
| 51 | +``` |
| 52 | + |
| 53 | +# Possible Improvements |
| 54 | +1. Wrap up the int return value of some functions with an Ok | Error |
| 55 | + variant. |
0 commit comments