getada_1.0.1_e3e93ee6/src/prompts.adb

 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
--    Copyright (C) 2022 A.J. Ianozi <aj@ianozi.com>
--
--    This file is part of GetAda: the Unofficial Alire Installer
--
--    This program is free software: you can redistribute it and/or modify
--    it under the terms of the GNU General Public License as published by
--    the Free Software Foundation, either version 3 of the License, or
--    (at your option) any later version.
--
--    This program 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 General Public License for more details.
--
--    You should have received a copy of the GNU General Public License
--    along with this program.  If not, see <https://www.gnu.org/licenses/>.

with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Strings.Fixed;       use Ada.Strings.Fixed;
with Ada.Text_IO;             use Ada.Text_IO;

package body Prompts is

   function Get_Answer
     (Prompt        : String; Default_Answer : Answer := DisableDefault;
      Provided_Text : String := "") return Answer
   is
      Question : constant String :=
        Prompt & " [" &
        (case Default_Answer is when Yes => "Y/n", when No => "y/N",
           when others                   => "y/n") &
        "] " &
        (if Provided_Text'Length > 0 then " (" & Provided_Text & ")" else "") &
        " >";
   begin
      loop
         Put (Question);
         declare
            Response : constant String :=
              Trim (To_Lower (Get_Line), Ada.Strings.Both);
         begin
            if Response'Length = 0 and then Default_Answer /= DisableDefault
            then
               return Default_Answer;
            elsif Response = "y" or else Response = "yes" then
               return Yes;
            elsif Response = "n" or else Response = "no" then
               return No;
            else
               Put_Line ("Invalid response.");
            end if;
         end;
      end loop;
   end Get_Answer;

   function Get_Answer
     (Prompt        : String; Default_Answer : String := "";
      Provided_Text : String := "") return String
   is
      Question : constant String :=
        Prompt &
        (if Default_Answer'Length > 0 then " [" & Default_Answer & "]"
         else "") &
        (if Provided_Text'Length > 0 then " (" & Provided_Text & ")" else "") &
        " >";
   begin
      Put (Question);
      declare
         Response : constant String :=
           Trim (To_Lower (Get_Line), Ada.Strings.Both);
      begin
         if Response'Length = 0 and then Default_Answer'Length > 0 then
            return Default_Answer;
         else
            return Response;
         end if;
      end;
   end Get_Answer;

end Prompts;