]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-util.adb
ncurses 6.1 - patch 20191005
[ncurses.git] / Ada95 / samples / ncurses2-util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                               ncurses2.util                              --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2008,2014 Free Software Foundation, Inc.              --
11 --                                                                          --
12 -- Permission is hereby granted, free of charge, to any person obtaining a  --
13 -- copy of this software and associated documentation files (the            --
14 -- "Software"), to deal in the Software without restriction, including      --
15 -- without limitation the rights to use, copy, modify, merge, publish,      --
16 -- distribute, distribute with modifications, sublicense, and/or sell       --
17 -- copies of the Software, and to permit persons to whom the Software is    --
18 -- furnished to do so, subject to the following conditions:                 --
19 --                                                                          --
20 -- The above copyright notice and this permission notice shall be included  --
21 -- in all copies or substantial portions of the Software.                   --
22 --                                                                          --
23 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
24 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
25 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
26 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
27 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
28 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
29 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
30 --                                                                          --
31 -- Except as contained in this notice, the name(s) of the above copyright   --
32 -- holders shall not be used in advertising or otherwise to promote the     --
33 -- sale, use or other dealings in this Software without prior written       --
34 -- authorization.                                                           --
35 ------------------------------------------------------------------------------
36 --  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
37 --  Version Control
38 --  $Revision: 1.9 $
39 --  $Date: 2014/05/24 21:32:18 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with Ada.Text_IO; use Ada.Text_IO;
43
44 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
45
46 with Interfaces.C;
47 with Interfaces.C.Strings;
48
49 with Ada.Characters.Handling;
50
51 with ncurses2.genericPuts;
52
53 package body ncurses2.util is
54
55    --  #defines from C
56    --  #define CTRL(x)         ((x) & 0x1f)
57    function CTRL (c : Character) return Key_Code is
58    begin
59       return Character'Pos (c) mod 16#20#;
60       --  uses a property of ASCII
61       --  A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
62    end CTRL;
63
64    function CTRL (c : Character) return Character is
65    begin
66       return Character'Val (Character'Pos (c) mod 16#20#);
67       --  uses a property of ASCII
68       --  A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
69    end CTRL;
70
71    save_trace : Trace_Attribute_Set;
72    --  Common function to allow ^T to toggle trace-mode in the middle of a test
73    --  so that trace-files can be made smaller.
74    function Getchar (win : Window := Standard_Window) return Key_Code is
75       c : Key_Code;
76    begin
77       --  #ifdef TRACE
78       c := Get_Keystroke (win);
79       while c = CTRL ('T') loop
80          --  if _nc_tracing  in C
81          if Current_Trace_Setting /= Trace_Disable then
82             save_trace := Current_Trace_Setting;
83             Trace_Put ("TOGGLE-TRACING OFF");
84             Current_Trace_Setting := Trace_Disable;
85          else
86             Current_Trace_Setting := save_trace;
87          end if;
88          Trace_On (Current_Trace_Setting);
89          if Current_Trace_Setting /= Trace_Disable then
90             Trace_Put ("TOGGLE-TRACING ON");
91          end if;
92       end loop;
93       --  #else c := Get_Keystroke;
94       return c;
95    end Getchar;
96
97    procedure Getchar (win : Window := Standard_Window) is
98    begin
99       if Getchar (win) < 0 then
100          Beep;
101       end if;
102    end Getchar;
103
104    procedure Pause is
105    begin
106       Move_Cursor (Line => Lines - 1, Column => 0);
107       Add (Str => "Press any key to continue... ");
108       Getchar;
109    end Pause;
110
111    procedure Cannot (s : String) is
112       use Interfaces.C;
113       use Interfaces.C.Strings;
114       function getenv (x : char_array)  return chars_ptr;
115       pragma Import (C, getenv, "getenv");
116       tmp1 : char_array (0 .. 10);
117       package p is new ncurses2.genericPuts (1024);
118       use p;
119       use p.BS;
120
121       tmpb : BS.Bounded_String;
122
123       Length : size_t;
124    begin
125       To_C ("TERM", tmp1, Length);
126       Fill_String (getenv (tmp1), tmpb);
127       Add (Ch => newl);
128       myAdd (Str => "This " & tmpb & " terminal " & s);
129       Pause;
130    end Cannot;
131
132    procedure ShellOut (message : Boolean) is
133       use Interfaces.C;
134       Txt : char_array (0 .. 10);
135       Length : size_t;
136       procedure system (x : char_array);
137       pragma Import (C, system, "system");
138    begin
139       To_C ("sh", Txt,  Length);
140       if message then
141          Add (Str => "Shelling out...");
142       end if;
143       Save_Curses_Mode (Mode => Curses);
144       End_Windows;
145       system (Txt);
146       if message then
147          Add (Str => "returned from shellout.");
148          Add (Ch => newl);
149       end if;
150       Refresh;
151    end ShellOut;
152
153    function Is_Digit (c : Key_Code) return Boolean is
154    begin
155       if c >= 16#100# then
156          return False;
157       else
158          return Ada.Characters.Handling.Is_Digit (Character'Val (c));
159       end if;
160    end Is_Digit;
161
162    procedure P (s : String) is
163    begin
164       Add (Str => s);
165       Add (Ch => newl);
166    end P;
167
168    function Code_To_Char (c : Key_Code) return Character is
169    begin
170       if c > Character'Pos (Character'Last) then
171          return Character'Val (0);
172          --  maybe raise exception?
173       else
174          return Character'Val (c);
175       end if;
176    end Code_To_Char;
177
178    --  This was untestable due to a bug in GNAT (3.12p)
179    --  Hmm, what bug? I don't remember.
180    function ctoi (c : Character) return Integer is
181    begin
182       return Character'Pos (c) - Character'Pos ('0');
183    end ctoi;
184
185 end ncurses2.util;