linenoise_ada_1.1.1_09aa2452/src/example/example.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
81
-- SPDX-License-Identifier: BSD-2-Clause
-- For more license details, see LICENSE.

pragma Ada_2012;

with Ada.Characters.Handling;  use Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Containers;  use type Ada.Containers.Count_Type;
with Ada.Exceptions;
with Ada.Text_IO;  use Ada.Text_IO;
with Callbacks;
with Linenoise;

procedure Example is
	History_Filename : constant String := "history.txt";

	procedure Cleanup is
	begin
		Linenoise.History_Save(History_Filename);
	end Cleanup;
begin
	Linenoise.History_Set_Maximum_Length(100);
	begin
		Linenoise.History_Load(History_Filename);
	exception
		-- History file likely hasn't been created yet so just ignore it
		when Linenoise.History_File_Error => null;
	end;

	Linenoise.Register_Completion_Callback(Callbacks.Completer'Access);
	Linenoise.Register_Hint_Callback(Callbacks.Hinter'Access);

	loop
		declare
			Raw : constant String := Linenoise.Get_Line("> ");
			Line : constant Linenoise.String_Vectors.Vector :=
				Linenoise.Split(To_Lower(Raw));
		begin
			if Line.Length > 0 then
				Linenoise.History_Add(Raw);
			end if;
			if Line.Element(1) = "clear" then
				Linenoise.Clear_Screen;
			elsif Line.Element(1) = "multiline" then
				Linenoise.Multiline_Mode(True);
				Put_Line("Multiline mode Enabled");
			elsif Line.Element(1) = "nomultiline" then
				Linenoise.Multiline_Mode(False);
				Put_Line("Multiline mode disabled");
			elsif Line.Element(1) = "exit" then
				Put_Line("Goodbye.");
				exit;
			elsif Line.Element(1) = "mask" then
				-- You can prompt the user again again arbitrarily
				Linenoise.Mask_Mode(True);
				declare
					Password : constant String := Linenoise.Get_Line("passwd> ");
				begin
					Put_Line("Your password '" & Password & "' will be stored 100% securely, you can trust us.");
				end;
				Linenoise.Mask_Mode(False);
			else
				Put_Line("Entered: " & Raw);
			end if;
		end;
	end loop;

	Cleanup;


exception
	when Linenoise.End_Error =>
		Cleanup;
	when Linenoise.History_File_Error =>
		Put_Line("Unable to save command history file.");
		Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
	when E : Linenoise.Malformed_Command =>
		Put_Line("Malformed command: " & Ada.Exceptions.Exception_Message(E));
		Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
		Cleanup;
end Example;