| 1 |
with Ada.Text_IO; |
| 2 |
with Ada.Characters.Handling; |
| 3 |
|
| 4 |
with Simple_Logging; |
| 5 |
|
| 6 |
with CLIC.TTY; |
| 7 |
|
| 8 |
package body CLIC.User_Input is |
| 9 |
|
| 10 |
package TIO renames Ada.Text_IO; |
| 11 |
package Char renames Ada.Characters.Handling; |
| 12 |
|
| 13 |
User_Input_Error : exception; |
| 14 |
|
| 15 |
Answer_Char : constant array (Answer_Kind) of Character := |
| 16 |
(Yes => 'Y', |
| 17 |
No => 'N', |
| 18 |
Always => 'A'); |
| 19 |
|
| 20 |
----------------- |
| 21 |
-- Answer_Kind -- |
| 22 |
----------------- |
| 23 |
|
| 24 |
function Answer_String (Kind : Answer_Kind) return String |
| 25 |
is (case Kind is |
| 26 |
when Yes => "yes", |
| 27 |
when No => "no", |
| 28 |
when Always => "always"); |
| 29 |
|
| 30 |
--------------- |
| 31 |
-- Flush_TTY -- |
| 32 |
--------------- |
| 33 |
|
| 34 |
procedure Flush_TTY is |
| 35 |
C : Character; |
| 36 |
Available : Boolean; |
| 37 |
begin |
| 38 |
loop |
| 39 |
TIO.Get_Immediate (C, Available); |
| 40 |
exit when not Available; |
| 41 |
end loop; |
| 42 |
exception |
| 43 |
when TIO.End_Error => null; |
| 44 |
end Flush_TTY; |
| 45 |
|
| 46 |
------------------------- |
| 47 |
-- Print_Valid_Answers -- |
| 48 |
------------------------- |
| 49 |
|
| 50 |
procedure Print_Valid_Answers (Valid : Answer_Set; Default : Answer_Kind) is |
| 51 |
begin |
| 52 |
for Kind in Answer_Kind loop |
| 53 |
if Valid (Kind) then |
| 54 |
TIO.Put ("[" |
| 55 |
& (if Kind = Default |
| 56 |
then TTY.Bold ("" & Answer_Char (Kind)) |
| 57 |
else "" & Answer_Char (Kind)) |
| 58 |
& "] " & Img (Kind) & " "); |
| 59 |
end if; |
| 60 |
end loop; |
| 61 |
TIO.Put ("(default is " & TTY.Bold (Img (Default)) & ") "); |
| 62 |
end Print_Valid_Answers; |
| 63 |
|
| 64 |
----------- |
| 65 |
-- Query -- |
| 66 |
----------- |
| 67 |
|
| 68 |
function Query (Question : String; |
| 69 |
Valid : Answer_Set; |
| 70 |
Default : Answer_Kind) |
| 71 |
return Answer_Kind |
| 72 |
is |
| 73 |
----------------- |
| 74 |
-- Use_Default -- |
| 75 |
----------------- |
| 76 |
|
| 77 |
function Use_Default return Answer_Kind is |
| 78 |
begin |
| 79 |
TIO.Put_Line ("Using default: " & Img (Default)); |
| 80 |
return Default; |
| 81 |
end Use_Default; |
| 82 |
|
| 83 |
begin |
| 84 |
loop |
| 85 |
TIO.Put_Line (Question); |
| 86 |
|
| 87 |
if Not_Interactive or else not TTY.Is_TTY then |
| 88 |
return Use_Default; |
| 89 |
end if; |
| 90 |
|
| 91 |
-- Flush the input that the user may have entered by mistake before |
| 92 |
-- the question is asked. |
| 93 |
Flush_TTY; |
| 94 |
|
| 95 |
Print_Valid_Answers (Valid, Default); |
| 96 |
|
| 97 |
-- Get user input |
| 98 |
declare |
| 99 |
Input : constant String := TIO.Get_Line; |
| 100 |
begin |
| 101 |
|
| 102 |
-- Empty line means the user pressed enter without any answer |
| 103 |
if Input'Length = 0 then |
| 104 |
return Use_Default; |
| 105 |
end if; |
| 106 |
|
| 107 |
if Input'Length = 1 then |
| 108 |
|
| 109 |
for Kind in Answer_Kind loop |
| 110 |
if Valid (Kind) |
| 111 |
and then |
| 112 |
Char.To_Upper (Input (Input'First)) = Answer_Char (Kind) |
| 113 |
then |
| 114 |
-- We got a valid answer |
| 115 |
return Kind; |
| 116 |
end if; |
| 117 |
end loop; |
| 118 |
end if; |
| 119 |
|
| 120 |
--- Check if the user input the whole answer |
| 121 |
for Kind in Answer_Kind loop |
| 122 |
if Valid (Kind) |
| 123 |
and then |
| 124 |
Char.To_Lower (Input) = Answer_String (Kind) |
| 125 |
then |
| 126 |
return Kind; |
| 127 |
end if; |
| 128 |
end loop; |
| 129 |
|
| 130 |
TIO.Put_Line ("Invalid answer."); |
| 131 |
end; |
| 132 |
end loop; |
| 133 |
exception |
| 134 |
when TIO.End_Error => |
| 135 |
-- This happens on the user hitting Ctrl-D, and no further |
| 136 |
-- input can be obtained as stdin is closed |
| 137 |
raise User_Interrupt; |
| 138 |
end Query; |
| 139 |
|
| 140 |
----------------- |
| 141 |
-- Query_Multi -- |
| 142 |
----------------- |
| 143 |
|
| 144 |
function Query_Multi (Question : String; |
| 145 |
Choices : AAA.Strings.Vector; |
| 146 |
Page_Size : Positive := 10) |
| 147 |
return Positive |
| 148 |
is |
| 149 |
Answers : constant array (Positive range <>) of Character := |
| 150 |
('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', |
| 151 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
| 152 |
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
| 153 |
'u', 'v', 'w', 'x', 'y', 'z'); |
| 154 |
pragma Assert (Answers'First = Positive'First); |
| 155 |
|
| 156 |
Use_Pager : constant Boolean := Natural (Choices.Length) > Page_Size; |
| 157 |
Page_Start : Positive := 1; |
| 158 |
Page_End : Positive; |
| 159 |
-- Points always to the last valid choice; there can be an extra choice |
| 160 |
-- if Use_Pager, to move forward the list. |
| 161 |
|
| 162 |
------------------- |
| 163 |
-- Print_Choices -- |
| 164 |
------------------- |
| 165 |
|
| 166 |
procedure Print_Choices is |
| 167 |
begin |
| 168 |
Page_End := Positive'Min (Choices.Last_Index, |
| 169 |
Page_Start + Page_Size - 1); |
| 170 |
|
| 171 |
-- Print the choices proper |
| 172 |
|
| 173 |
for I in Page_Start .. Page_End loop |
| 174 |
TIO.Put_Line |
| 175 |
(" " |
| 176 |
& (if I = Page_Start |
| 177 |
then TTY.Bold ("" & Answers (I - Page_Start + 1)) |
| 178 |
else TTY.Emph ("" & Answers (I - Page_Start + 1))) |
| 179 |
& ". " & Choices (I)); |
| 180 |
end loop; |
| 181 |
|
| 182 |
-- And the pager if needed |
| 183 |
|
| 184 |
if Use_Pager then |
| 185 |
TIO.Put_Line (TTY.Emph (" " & Answers (Page_End - Page_Start + 2)) |
| 186 |
& ". (See more choices...)"); |
| 187 |
end if; |
| 188 |
end Print_Choices; |
| 189 |
|
| 190 |
begin |
| 191 |
loop |
| 192 |
begin |
| 193 |
TIO.Put_Line (Question); |
| 194 |
|
| 195 |
if Not_Interactive then |
| 196 |
Simple_Logging.Info |
| 197 |
("Using default choice in non-interactive mode: " |
| 198 |
& Choices.First_Element); |
| 199 |
Simple_Logging.Warning (TTY.Is_TTY'Img); |
| 200 |
return Choices.First_Index; |
| 201 |
end if; |
| 202 |
|
| 203 |
-- Flush the input that the user may have entered by mistake |
| 204 |
-- before the question is asked. |
| 205 |
Flush_TTY; |
| 206 |
|
| 207 |
Print_Choices; |
| 208 |
TIO.Put_Line ("Enter your choice index (first is default): "); |
| 209 |
TIO.Put ("> "); |
| 210 |
|
| 211 |
declare |
| 212 |
Answer_Line : constant String := TIO.Get_Line; |
| 213 |
Answer_Char : Character; |
| 214 |
Answer_Pos : Natural := 0; |
| 215 |
Extra : constant Natural := (if Use_Pager then 1 else 0); |
| 216 |
-- We have an extra entry in the list in this case |
| 217 |
begin |
| 218 |
if Answer_Line = "" then |
| 219 |
return Page_Start; |
| 220 |
elsif Answer_Line'Length > 1 then |
| 221 |
raise User_Input_Error with "answer too long"; |
| 222 |
end if; |
| 223 |
|
| 224 |
Answer_Char := Answer_Line (Answer_Line'First); |
| 225 |
|
| 226 |
-- Find the user's choice, and correct it with the actual page |
| 227 |
-- we are showing to them. |
| 228 |
|
| 229 |
for I in Answers'Range loop |
| 230 |
if Answer_Char = Answers (I) then |
| 231 |
Answer_Pos := I; |
| 232 |
end if; |
| 233 |
end loop; |
| 234 |
|
| 235 |
if Answer_Pos = 0 then |
| 236 |
raise User_Input_Error with "Choice out of range"; |
| 237 |
end if; |
| 238 |
|
| 239 |
Answer_Pos := Answer_Pos + Page_Start - 1; |
| 240 |
|
| 241 |
if Answer_Pos not in Page_Start .. Page_End + Extra |
| 242 |
then |
| 243 |
raise User_Input_Error with "Choice out of range"; |
| 244 |
end if; |
| 245 |
|
| 246 |
-- We have a valid choice; either change pages or return choice |
| 247 |
if Answer_Pos = Page_End + 1 then |
| 248 |
Page_Start := Page_Start + Page_Size; |
| 249 |
if Page_Start > Choices.Last_Index then |
| 250 |
Page_Start := Choices.First_Index; |
| 251 |
end if; |
| 252 |
else |
| 253 |
return Answer_Pos; |
| 254 |
end if; |
| 255 |
end; |
| 256 |
exception |
| 257 |
when TIO.End_Error => |
| 258 |
-- This happens on the user hitting Ctrl-D, and no further |
| 259 |
-- input can be obtained as stdin is closed |
| 260 |
Simple_Logging.Debug ("End_Error caught."); |
| 261 |
raise User_Interrupt; |
| 262 |
|
| 263 |
when User_Input_Error => |
| 264 |
Simple_Logging.Info |
| 265 |
(TTY.Error ("✗ ") & |
| 266 |
"Not a valid choice, please use a line index."); |
| 267 |
|
| 268 |
when E : others => |
| 269 |
Simple_Logging.Warning ("Unexpected input, exiting."); |
| 270 |
raise User_Interrupt; |
| 271 |
end; |
| 272 |
end loop; |
| 273 |
end Query_Multi; |
| 274 |
|
| 275 |
--------- |
| 276 |
-- Img -- |
| 277 |
--------- |
| 278 |
|
| 279 |
function Img (Kind : Answer_Kind) return String |
| 280 |
is (case Kind is |
| 281 |
when Yes => "Yes", |
| 282 |
when No => "No", |
| 283 |
when Always => "Always"); |
| 284 |
|
| 285 |
------------------ |
| 286 |
-- Query_String -- |
| 287 |
------------------ |
| 288 |
|
| 289 |
function Query_String (Question : String; |
| 290 |
Default : String; |
| 291 |
Validation : String_Validation_Access) |
| 292 |
return String |
| 293 |
is |
| 294 |
----------------- |
| 295 |
-- Use_Default -- |
| 296 |
----------------- |
| 297 |
|
| 298 |
function Use_Default return String is |
| 299 |
begin |
| 300 |
TIO.Put_Line ("Using default: '" & Default & "'"); |
| 301 |
return Default; |
| 302 |
end Use_Default; |
| 303 |
|
| 304 |
-------------- |
| 305 |
-- Is_Valid -- |
| 306 |
-------------- |
| 307 |
|
| 308 |
function Is_Valid (Str : String) return Boolean |
| 309 |
is (Validation = null or else Validation (Str)); |
| 310 |
|
| 311 |
begin |
| 312 |
loop |
| 313 |
TIO.Put_Line (Question & " (" & "default: '" & Default & "')"); |
| 314 |
|
| 315 |
if Not_Interactive or else not TTY.Is_TTY then |
| 316 |
return Use_Default; |
| 317 |
end if; |
| 318 |
|
| 319 |
-- Print a prompt |
| 320 |
TIO.Put ("> "); |
| 321 |
|
| 322 |
-- Flush the input that the user may have entered by mistake before |
| 323 |
-- the question is asked. |
| 324 |
Flush_TTY; |
| 325 |
|
| 326 |
-- Get user input |
| 327 |
declare |
| 328 |
Input : constant String := TIO.Get_Line; |
| 329 |
begin |
| 330 |
|
| 331 |
-- Empty line means the user pressed enter without any answer |
| 332 |
if Input'Length = 0 and then Is_Valid (Default) then |
| 333 |
return Use_Default; |
| 334 |
end if; |
| 335 |
|
| 336 |
if Is_Valid (Input) then |
| 337 |
|
| 338 |
-- We got a valid answer |
| 339 |
return Input; |
| 340 |
end if; |
| 341 |
|
| 342 |
TIO.Put_Line ("Invalid answer."); |
| 343 |
end; |
| 344 |
end loop; |
| 345 |
exception |
| 346 |
when TIO.End_Error => |
| 347 |
-- This happens on the user hitting Ctrl-D, and no further |
| 348 |
-- input can be obtained as stdin is closed |
| 349 |
raise User_Interrupt; |
| 350 |
end Query_String; |
| 351 |
|
| 352 |
----------------------- |
| 353 |
-- Continue_Or_Abort -- |
| 354 |
----------------------- |
| 355 |
|
| 356 |
procedure Continue_Or_Abort is |
| 357 |
Foo : String := "bar"; |
| 358 |
Bar : Integer; |
| 359 |
begin |
| 360 |
if Not_Interactive then |
| 361 |
Simple_Logging.Detail ("Non-interactive session, continuing"); |
| 362 |
else |
| 363 |
Flush_TTY; |
| 364 |
TIO.Put_Line ("Press Enter to continue or Ctrl-C to abort"); |
| 365 |
TIO.Get_Line (Foo, Bar); |
| 366 |
end if; |
| 367 |
end Continue_Or_Abort; |
| 368 |
|
| 369 |
--------------------- |
| 370 |
-- Validated_Input -- |
| 371 |
--------------------- |
| 372 |
|
| 373 |
function Validated_Input |
| 374 |
(Question : String; |
| 375 |
Prompt : String; |
| 376 |
Valid : Answer_Set; |
| 377 |
Default : access function (User_Input : String) return Answer_Kind; |
| 378 |
Confirm : String := "Is this information correct?"; |
| 379 |
Is_Valid : access function (User_Input : String) return Boolean) |
| 380 |
return Answer_With_Input |
| 381 |
is |
| 382 |
begin |
| 383 |
TIO.Put_Line (Question); |
| 384 |
loop |
| 385 |
TIO.Put (Prompt); |
| 386 |
declare |
| 387 |
Input : constant String := TIO.Get_Line; |
| 388 |
begin |
| 389 |
if Is_Valid (Input) then |
| 390 |
declare |
| 391 |
Result : Answer_With_Input := (Input'Length, Input, No); |
| 392 |
begin |
| 393 |
Result.Answer := Query (Confirm, Valid, Default (Input)); |
| 394 |
if Result.Answer /= No then |
| 395 |
return Result; |
| 396 |
end if; |
| 397 |
end; |
| 398 |
end if; |
| 399 |
end; |
| 400 |
end loop; |
| 401 |
end Validated_Input; |
| 402 |
|
| 403 |
end CLIC.User_Input; |