-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_console.adb
93 lines (74 loc) · 2.13 KB
/
p_console.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
82
83
84
85
86
87
88
89
90
91
92
93
with Interfaces.C, Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
use Interfaces.C, Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
package body p_console is
procedure execute(command: in string) is
-- {} => {Execute a command in the terminal}
R: integer;
begin
R := execute(command);
end execute;
function execute(command: in string) return integer is
-- {} => {result=return code of the executed command}
function Sys (arg : Char_Array) return integer;
pragma Import(C, Sys, "system");
begin
return Sys(To_C(command));
end execute;
procedure clear is
-- {} => {Clears the console}
begin
execute("clear");
end clear;
procedure write(x, y: integer; text: string) is
-- {} => {Writes the text at position (x;y)}
begin
execute("printf '\33[%d;%dH%s' '"
& Trim(Integer'image(y), BOTH)
& "' '"
& Trim(Integer'image(x), BOTH)
& "' '"
& text
& "'");
end write;
procedure hideCursor is
-- {} => {Hides the cursor}
begin
execute("printf '\e[?25l'");
end hideCursor;
procedure showCursor is
-- {} => {Shows the cursor}
begin
execute("printf '\e[?25h'");
end showCursor;
function getTerminalWidth return integer is
-- {} => {result=number of columns in the terminal}
resultFile: File_type;
consoleWidth: natural;
begin
if width /= 0 then
return width;
end if;
execute("tput cols >> tempConsole");
open(resultFile,In_File,"tempConsole");
consoleWidth := Integer'value(get_Line(resultFile));
delete(resultFile);
Put(Integer'image(consoleWidth));
width := consoleWidth;
return consoleWidth;
end getTerminalWidth;
function getTerminalHeight return integer is
-- {} => {result=number of lines in the terminal}
resultFile: File_type;
consoleHeight: natural;
begin
if height /= 0 then
return height;
end if;
execute("tput lines >> tempConsole");
open(resultFile,In_File,"tempConsole");
consoleHeight := Integer'value(get_Line(resultFile));
delete(resultFile);
height := consoleHeight;
return consoleHeight;
end getTerminalHeight;
end p_console;