| 1 |
with AAA.Strings; |
| 2 |
|
| 3 |
package CLIC.User_Input is |
| 4 |
|
| 5 |
------------------- |
| 6 |
-- Interactivity -- |
| 7 |
------------------- |
| 8 |
|
| 9 |
Not_Interactive : aliased Boolean := False; |
| 10 |
-- When not Interactive, instead of asking the user something, use default. |
| 11 |
|
| 12 |
User_Interrupt : exception; |
| 13 |
-- Raised when the user hits Ctrl-D and no further input can be obtained as |
| 14 |
-- stdin is closed. |
| 15 |
|
| 16 |
type Answer_Kind is (Yes, No, Always); |
| 17 |
|
| 18 |
type Answer_Set is array (Answer_Kind) of Boolean; |
| 19 |
|
| 20 |
function Query (Question : String; |
| 21 |
Valid : Answer_Set; |
| 22 |
Default : Answer_Kind) |
| 23 |
return Answer_Kind; |
| 24 |
-- If interactive, ask the user for one of the valid answer. |
| 25 |
-- Otherwise return the Default answer. |
| 26 |
|
| 27 |
function Query_Multi (Question : String; |
| 28 |
Choices : AAA.Strings.Vector; |
| 29 |
Page_Size : Positive := 10) |
| 30 |
return Positive |
| 31 |
with Pre => Page_Size >= 2 and then Page_Size < 36; |
| 32 |
-- Present the Choices in a numbered list 1-9-0-a-z, with paging if |
| 33 |
-- Choices.Length > Page_Size. Default is always First of Choices. |
| 34 |
|
| 35 |
type Answer_With_Input (Length : Natural) is record |
| 36 |
Input : String (1 .. Length); |
| 37 |
Answer : Answer_Kind; |
| 38 |
end record; |
| 39 |
|
| 40 |
function Validated_Input |
| 41 |
(Question : String; |
| 42 |
Prompt : String; |
| 43 |
Valid : Answer_Set; |
| 44 |
Default : access function (User_Input : String) return Answer_Kind; |
| 45 |
Confirm : String := "Is this information correct?"; |
| 46 |
Is_Valid : access function (User_Input : String) return Boolean) |
| 47 |
return Answer_With_Input; |
| 48 |
-- Interactive prompt for information from the user, with confirmation: |
| 49 |
-- Put_Line (Question) |
| 50 |
-- loop |
| 51 |
-- Put (Prompt); Get_Line (User_Input); |
| 52 |
-- if Is_Valid (User_Input) then |
| 53 |
-- exit when Query (Confirm, Valid, Default (User_Input)) /= No; |
| 54 |
-- end if; |
| 55 |
-- end loop |
| 56 |
|
| 57 |
function Img (Kind : Answer_Kind) return String; |
| 58 |
|
| 59 |
type String_Validation_Access is |
| 60 |
access function (Str : String) return Boolean; |
| 61 |
|
| 62 |
function Query_String (Question : String; |
| 63 |
Default : String; |
| 64 |
Validation : String_Validation_Access) |
| 65 |
return String |
| 66 |
with Pre => Validation = null or else Validation (Default); |
| 67 |
-- If interactive, ask the user to provide a valid string. |
| 68 |
-- Otherwise return the Default value. |
| 69 |
-- |
| 70 |
-- If Validation is null, any input is accepted. |
| 71 |
-- |
| 72 |
-- The Default value has to be a valid input. |
| 73 |
|
| 74 |
procedure Continue_Or_Abort; |
| 75 |
-- If interactive, ask the user to press Enter or Ctrl-C to stop. |
| 76 |
-- Output a log trace otherwise and continue. |
| 77 |
|
| 78 |
end CLIC.User_Input; |