linenoise_ada_1.1.1_09aa2452/src/example/callbacks.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
-- 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.Strings.Fixed;  use Ada.Strings.Fixed;

package body Callbacks is

	---------------
	-- Completer --
	---------------

	function Completer
		(Line : String) return Linenoise.String_Vectors.Vector
	is
		O : Linenoise.String_Vectors.Vector :=
			Linenoise.String_Vectors.Empty_Vector;
	begin
		case To_Lower(Line(Line'First)) is
			when 'c' => O.Append("clear");
			when 'e' => O.Append("exit");
			when 'h' =>
				O.Append("hello");
				O.Append("hello world");
			when 'm' =>
				O.Append("multiline");
				O.Append("mask");
			when 'n' => O.Append("nomultiline");
			when others => null;
		end case;
		return O;
	end Completer;


	------------
	-- Hinter --
	------------

	function Hinter
		(Line : String; Color : out Linenoise.Color_Codes; Bold : out Boolean)
		return String
	is
		function Begins_With (S : String; Prefix : String) return Boolean is
		begin
			return S'Length >= Prefix'Length and then
				Head(S, Prefix'Length) = Prefix;
		end Begins_With;

		Downcased : constant String := To_Lower(Line);
	begin
		Color := Linenoise.Yellow;
		Bold := False;
		if Begins_With(Downcased, "hello") then
			return " world";
		elsif Begins_With(Downcased, "mask") then
			return "  prompt to enter a masked ""password""";
		elsif Begins_With(Downcased, "multiline") then
			return "  enable multiline editing";
		elsif Begins_With(Downcased, "nomultiline") then
			return "  disable multiline editing";
		elsif Begins_With(Downcased, "clear") then
			return "  clear the screen";
		elsif Begins_With(Downcased, "exit") then
			return "  exit the example program";
		end if;
		-- Return the empty string when there's no hint
		return "";
	end Hinter;

end Callbacks;