package dunolint

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file prompt.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(*******************************************************************************)
(*  Prompt - A library to prompt the user for simple answers in the terminal   *)
(*  Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>          *)
(*                                                                             *)
(*  This file is part of Prompt.                                               *)
(*                                                                             *)
(*  Prompt is free software; you can redistribute it and/or modify it under    *)
(*  the terms of the GNU Lesser General Public License as published by the     *)
(*  Free Software Foundation either version 3 of the License, or any later     *)
(*  version, with the LGPL-3.0 Linking Exception.                              *)
(*                                                                             *)
(*  Prompt is distributed in the hope that it will be useful, but WITHOUT ANY  *)
(*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *)
(*  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and    *)
(*  file `NOTICE.md` at the root of this repository for more details.          *)
(*                                                                             *)
(*  You should have received a copy of the GNU Lesser General Public License   *)
(*  and the LGPL-3.0 Linking Exception along with this library. If not, see    *)
(*  <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.       *)
(*******************************************************************************)

(* This library was inspired by [async_interactive.v0.17.0].

   https://github.com/janestreet/async_interactive

   ----------------------------------------------------------------------------

   The MIT License

   Copyright (c) 2014--2024 Jane Street Group, LLC <opensource-contacts@janestreet.com>

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE. *)

let read_line () =
  match In_channel.input_line In_channel.stdin with
  | Some line -> line
  | None -> raise End_of_file
;;

let aprintf fmt =
  Stdlib.Format.kasprintf
    (fun str ->
       Out_channel.output_string Out_channel.stdout str;
       Out_channel.flush Out_channel.stdout)
    fmt
;;

let read_char () =
  let str = read_line () in
  let len = String.length str in
  if len = 1
  then Ok (Some (Char.lowercase str.[0]))
  else if len = 0
  then Ok None
  else Error ()
;;

let styled style s = Fmt.(str_like stdout "%a" (styled style string) s)

let ask_gen ~prompt ~f =
  let rec loop () =
    aprintf "[%s] %s: " (styled (`Fg `Magenta) "?") prompt;
    let line = read_line () in
    match f line with
    | Ok res -> res
    | Error msg ->
      aprintf "[%s] %s\n" (styled (`Fg `Red) "!") msg;
      loop ()
  in
  loop ()
;;

module Choice = struct
  type 'a t = char * 'a * string

  let create char a ~help:string = char, a, string
  let default (c, a, s) = Char.uppercase c, a, s
end

let choose (type a) ~(choices : (char * a) list) : char option -> (a, unit) Result.t
  = function
  | None ->
    (match List.filter choices ~f:(fun (c, _) -> Char.is_uppercase c) with
     | _ :: _ :: _ as l ->
       raise
         (Invalid_argument
            (Printf.sprintf
               "[Prompt.choose] supplied multiple defaults %S."
               (String.of_char_list (List.map l ~f:fst))))
     | [ (_, a) ] -> Ok a
     | [] -> Error ())
  | Some ch ->
    let filter (reply, _) = Char.equal (Char.lowercase reply) (Char.lowercase ch) in
    (match List.find choices ~f:filter with
     | Some (_, a) -> Ok a
     | None -> Error ())
;;

let ask_internal (type a) ~prompt ~(choices : (char * a) list) =
  let prompt =
    let cs = List.map choices ~f:(fun (c, _) -> Char.to_string c) in
    Printf.sprintf "%s [%s]" prompt (String.concat ~sep:"/" cs)
  in
  let please_answer () =
    let num_choices = List.length choices in
    let choices =
      List.mapi choices ~f:(fun i (char, _value) ->
        let sep = if i = 0 then "" else if i = num_choices - 1 then " or " else ", " in
        Printf.sprintf "%s'%c'" sep (Char.lowercase char))
      |> String.concat
    in
    aprintf "[%s] Please answer %s.\n\n" (styled (`Fg `Red) "!") choices
  in
  let rec loop () =
    aprintf "[%s] %s: " (styled (`Fg `Magenta) "?") prompt;
    match read_char () with
    | Error () ->
      please_answer ();
      loop ()
    | Ok char ->
      (match choose ~choices char with
       | Ok res -> res
       | Error () ->
         please_answer ();
         loop ())
  in
  loop ()
;;

let ask ~prompt ~choices =
  let print_help () =
    aprintf "\nPlease choose among the following options:\n";
    List.iter choices ~f:(fun (char, _value, help) ->
      aprintf "  %c : %s\n" (Char.lowercase char) help);
    aprintf "  ? : Print this help.\n\n"
  in
  let choices =
    List.map choices ~f:(fun (char, value, _help) -> char, `Ok value) @ [ '?', `Help ]
  in
  let rec loop () =
    match ask_internal ~prompt ~choices with
    | `Ok value -> value
    | `Help ->
      print_help ();
      loop ()
  in
  loop ()
;;

let ask_yn ~prompt ~default =
  let y, n =
    match default with
    | None -> 'y', 'n'
    | Some true -> 'Y', 'n'
    | Some false -> 'y', 'N'
  in
  ask_internal ~prompt ~choices:[ y, true; n, false ]
;;

module Arg = struct
  open Command.Std

  let yes = Arg.flag [ "yes" ] ~doc:"do not prompt for confirmation"
end
OCaml

Innovation. Community. Security.