1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
5 -- ncurses2.trace_set --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 2000-2011,2014 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 with ncurses2.util; use ncurses2.util;
44 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
45 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
46 with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
48 with Ada.Strings.Bounded;
50 -- interactively set the trace level
52 procedure ncurses2.trace_set is
54 function menu_virtualize (c : Key_Code) return Key_Code;
55 function subset (super, sub : Trace_Attribute_Set) return Boolean;
56 function trace_or (a, b : Trace_Attribute_Set) return Trace_Attribute_Set;
57 function trace_num (tlevel : Trace_Attribute_Set) return String;
58 function tracetrace (tlevel : Trace_Attribute_Set) return String;
59 function run_trace_menu (m : Menu; count : Integer) return Boolean;
61 function menu_virtualize (c : Key_Code) return Key_Code is
64 when Character'Pos (newl) | Key_Exit =>
65 return Menu_Request_Code'Last + 1; -- MAX_COMMAND? TODO
66 when Character'Pos ('u') =>
67 return M_ScrollUp_Line;
68 when Character'Pos ('d') =>
69 return M_ScrollDown_Line;
70 when Character'Pos ('b') | Key_Next_Page =>
71 return M_ScrollUp_Page;
72 when Character'Pos ('f') | Key_Previous_Page =>
73 return M_ScrollDown_Page;
74 when Character'Pos ('n') | Key_Cursor_Down =>
76 when Character'Pos ('p') | Key_Cursor_Up =>
77 return M_Previous_Item;
78 when Character'Pos (' ') =>
88 type string_a is access String;
89 type tbl_entry is record
91 mask : Trace_Attribute_Set;
94 t_tbl : constant array (Positive range <>) of tbl_entry :=
96 (new String'("Disable"),
98 (new String'("Times"),
99 Trace_Attribute_Set'(Times => True, others => False)),
100 (new String'("Tputs"),
101 Trace_Attribute_Set'(Tputs => True, others => False)),
102 (new String'("Update"),
103 Trace_Attribute_Set'(Update => True, others => False)),
104 (new String'("Cursor_Move"),
105 Trace_Attribute_Set'(Cursor_Move => True, others => False)),
106 (new String'("Character_Output"),
107 Trace_Attribute_Set'(Character_Output => True, others => False)),
108 (new String'("Ordinary"),
110 (new String'("Calls"),
111 Trace_Attribute_Set'(Calls => True, others => False)),
112 (new String'("Virtual_Puts"),
113 Trace_Attribute_Set'(Virtual_Puts => True, others => False)),
114 (new String'("Input_Events"),
115 Trace_Attribute_Set'(Input_Events => True, others => False)),
116 (new String'("TTY_State"),
117 Trace_Attribute_Set'(TTY_State => True, others => False)),
118 (new String'("Internal_Calls"),
119 Trace_Attribute_Set'(Internal_Calls => True, others => False)),
120 (new String'("Character_Calls"),
121 Trace_Attribute_Set'(Character_Calls => True, others => False)),
122 (new String'("Termcap_TermInfo"),
123 Trace_Attribute_Set'(Termcap_TermInfo => True, others => False)),
124 (new String'("Maximium"),
128 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (300);
130 function subset (super, sub : Trace_Attribute_Set) return Boolean is
133 (super.Times or not sub.Times) and
134 (super.Tputs or not sub.Tputs) and
135 (super.Update or not sub.Update) and
136 (super.Cursor_Move or not sub.Cursor_Move) and
137 (super.Character_Output or not sub.Character_Output) and
138 (super.Calls or not sub.Calls) and
139 (super.Virtual_Puts or not sub.Virtual_Puts) and
140 (super.Input_Events or not sub.Input_Events) and
141 (super.TTY_State or not sub.TTY_State) and
142 (super.Internal_Calls or not sub.Internal_Calls) and
143 (super.Character_Calls or not sub.Character_Calls) and
144 (super.Termcap_TermInfo or not sub.Termcap_TermInfo) and
153 function trace_or (a, b : Trace_Attribute_Set) return Trace_Attribute_Set is
154 retval : Trace_Attribute_Set := Trace_Disable;
156 retval.Times := (a.Times or b.Times);
157 retval.Tputs := (a.Tputs or b.Tputs);
158 retval.Update := (a.Update or b.Update);
159 retval.Cursor_Move := (a.Cursor_Move or b.Cursor_Move);
160 retval.Character_Output := (a.Character_Output or b.Character_Output);
161 retval.Calls := (a.Calls or b.Calls);
162 retval.Virtual_Puts := (a.Virtual_Puts or b.Virtual_Puts);
163 retval.Input_Events := (a.Input_Events or b.Input_Events);
164 retval.TTY_State := (a.TTY_State or b.TTY_State);
165 retval.Internal_Calls := (a.Internal_Calls or b.Internal_Calls);
166 retval.Character_Calls := (a.Character_Calls or b.Character_Calls);
167 retval.Termcap_TermInfo := (a.Termcap_TermInfo or b.Termcap_TermInfo);
172 -- Print the hexadecimal value of the mask so
173 -- users can set it from the command line.
175 function trace_num (tlevel : Trace_Attribute_Set) return String is
176 result : Integer := 0;
181 result := result + m;
186 result := result + m;
190 if tlevel.Update then
191 result := result + m;
195 if tlevel.Cursor_Move then
196 result := result + m;
200 if tlevel.Character_Output then
201 result := result + m;
206 result := result + m;
210 if tlevel.Virtual_Puts then
211 result := result + m;
215 if tlevel.Input_Events then
216 result := result + m;
220 if tlevel.TTY_State then
221 result := result + m;
225 if tlevel.Internal_Calls then
226 result := result + m;
230 if tlevel.Character_Calls then
231 result := result + m;
235 if tlevel.Termcap_TermInfo then
236 result := result + m;
242 function tracetrace (tlevel : Trace_Attribute_Set) return String is
245 buf : Bounded_String := To_Bounded_String ("");
247 -- The C version prints the hexadecimal value of the mask, we
248 -- won't do that here because this is Ada.
250 if tlevel = Trace_Disable then
251 Append (buf, "Trace_Disable");
255 Trace_Attribute_Set'(Times => True, others => False))
257 Append (buf, "Times");
262 Trace_Attribute_Set'(Tputs => True, others => False))
264 Append (buf, "Tputs");
269 Trace_Attribute_Set'(Update => True, others => False))
271 Append (buf, "Update");
276 Trace_Attribute_Set'(Cursor_Move => True,
279 Append (buf, "Cursor_Move");
284 Trace_Attribute_Set'(Character_Output => True,
287 Append (buf, "Character_Output");
294 Append (buf, "Ordinary");
299 Trace_Attribute_Set'(Calls => True, others => False))
301 Append (buf, "Calls");
306 Trace_Attribute_Set'(Virtual_Puts => True,
309 Append (buf, "Virtual_Puts");
314 Trace_Attribute_Set'(Input_Events => True,
317 Append (buf, "Input_Events");
322 Trace_Attribute_Set'(TTY_State => True,
325 Append (buf, "TTY_State");
330 Trace_Attribute_Set'(Internal_Calls => True,
333 Append (buf, "Internal_Calls");
338 Trace_Attribute_Set'(Character_Calls => True,
341 Append (buf, "Character_Calls");
346 Trace_Attribute_Set'(Termcap_TermInfo => True,
349 Append (buf, "Termcap_TermInfo");
356 Append (buf, "Maximium");
361 if To_String (buf) (Length (buf) - 1) = ',' then
362 Delete (buf, Length (buf) - 1, Length (buf));
365 return To_String (buf);
368 function run_trace_menu (m : Menu; count : Integer) return Boolean is
374 changed := (count /= 0);
375 c := Getchar (Get_Window (m));
376 v := menu_virtualize (c);
377 case Driver (m, v) is
378 when Unknown_Request =>
382 if i = Menus.Items (m, 1) then -- the first item
383 for n in t_tbl'First + 1 .. t_tbl'Last loop
385 Set_Value (i, False);
390 for n in t_tbl'First + 1 .. t_tbl'Last loop
391 p := Menus.Items (m, n);
393 Set_Value (Menus.Items (m, 1), False);
406 nc_tracing, mask : Trace_Attribute_Set;
407 pragma Import (C, nc_tracing, "_nc_tracing");
408 items_a : constant Item_Array_Access :=
409 new Item_Array (t_tbl'First .. t_tbl'Last + 1);
411 mcols : Column_Count;
413 menu_y : constant Line_Position := 8;
414 menu_x : constant Column_Position := 8;
418 newtrace : Trace_Attribute_Set;
420 Add (Line => 0, Column => 0, Str => "Interactively set trace level:");
421 Add (Line => 2, Column => 0,
422 Str => " Press space bar to toggle a selection.");
423 Add (Line => 3, Column => 0,
424 Str => " Use up and down arrow to move the select bar.");
425 Add (Line => 4, Column => 0,
426 Str => " Press return to set the trace level.");
427 Add (Line => 6, Column => 0, Str => "(Current trace level is ");
428 Add (Str => tracetrace (nc_tracing) & " numerically: " &
429 trace_num (nc_tracing));
434 for n in t_tbl'Range loop
435 items_a.all (n) := New_Item (t_tbl (n).name.all);
437 items_a.all (t_tbl'Last + 1) := Null_Item;
439 m := New_Menu (items_a);
441 Set_Format (m, 16, 2);
442 Scale (m, mrows, mcols);
444 Switch_Options (m, (One_Valued => True, others => False), On => False);
445 menuwin := New_Window (mrows + 2, mcols + 2, menu_y, menu_x);
446 Set_Window (m, menuwin);
447 Set_KeyPad_Mode (menuwin, SwitchOn => True);
450 Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
454 for n in t_tbl'Range loop
456 mask := t_tbl (n).mask;
457 if mask = Trace_Disable then
458 Set_Value (ip, nc_tracing = Trace_Disable);
459 elsif subset (sub => mask, super => nc_tracing) then
460 Set_Value (ip, True);
465 while run_trace_menu (m, count) loop
469 newtrace := Trace_Disable;
470 for n in t_tbl'Range loop
473 mask := t_tbl (n).mask;
474 newtrace := trace_or (newtrace, mask);
479 Trace_Put ("trace level interactively set to " &
480 tracetrace (nc_tracing));
482 Move_Cursor (Line => Lines - 4, Column => 0);
483 Add (Str => "Trace level is ");
484 Add (Str => tracetrace (nc_tracing));
486 Pause; -- was just Add(); Getchar
489 -- menuwin has subwindows I think, which makes an error.
492 exception when Curses_Exception => null; end;
496 end ncurses2.trace_set;