Skip to content

Commit d20e909

Browse files
committed
Merge branch 'mr/jicquel/github.PR#81' into 'master'
Merge #81 See merge request eng/toolchain/gnatcoll-core!86
2 parents e4c48cf + 724c8db commit d20e909

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

src/gnatcoll-terminal.adb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,21 @@ package body GNATCOLL.Terminal is
432432
return Internal (Boolean'Pos (Self.FD = Stderr));
433433
end if;
434434
end Get_Width;
435+
436+
---------------
437+
-- Get_Lines --
438+
---------------
439+
440+
function Get_Lines (Self : Terminal_Info) return Integer is
441+
function Internal (Stderr : Integer) return Integer;
442+
pragma Import (C, Internal, "gnatcoll_terminal_lines");
443+
begin
444+
if Self.FD = File or else Self.Colors = Unsupported then
445+
return -1;
446+
else
447+
return Internal (Boolean'Pos (Self.FD = Stderr));
448+
end if;
449+
end Get_Lines;
450+
435451
end GNATCOLL.Terminal;
452+

src/gnatcoll-terminal.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ package GNATCOLL.Terminal is
140140
-- Return the width of the terminal, or -1 if that width is either
141141
-- unknown or does not apply (as is the case for files for instance).
142142

143+
function Get_Lines (Self : Terminal_Info) return Integer;
144+
-- Return the height of the terminal, or -1 if that height is either
145+
-- unknown or does not apply (as is the case for files for instance).
146+
143147
-----------
144148
-- Utils --
145149
-----------

src/terminals.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*----------------------------------------------------------------------------
22
-- G N A T C O L L --
33
-- --
4-
-- Copyright (C) 2014-2018, AdaCore --
4+
-- Copyright (C) 2014-2024, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify it --
77
-- under terms of the GNU General Public License as published by the Free --
@@ -121,7 +121,7 @@ int gnatcoll_terminal_width(int forStderr) {
121121
GetStdHandle (forStderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
122122
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
123123
if (GetConsoleScreenBufferInfo (handle, &csbiInfo)) {
124-
return (int)csbiInfo.dwSize.X;
124+
return (int)(csbiInfo.srWindow.Right-csbiInfo.srWindow.Left + 1); // window width
125125
}
126126
return -1;
127127

@@ -135,3 +135,24 @@ int gnatcoll_terminal_width(int forStderr) {
135135
#endif
136136
#endif
137137
}
138+
139+
int gnatcoll_terminal_lines(int forStderr) {
140+
#ifdef _WIN32 // MsWin
141+
const HANDLE handle =
142+
GetStdHandle (forStderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
143+
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
144+
if (GetConsoleScreenBufferInfo (handle, &csbiInfo)) {
145+
return (int)(csbiInfo.srWindow.Bottom-csbiInfo.srWindow.Top + 1); // window height
146+
}
147+
return -1;
148+
149+
#else
150+
#ifdef TIOCGWINSZ // Linux/OSX
151+
struct winsize w;
152+
ioctl(forStderr ? 1 : 0, TIOCGWINSZ, &w);
153+
return w.ws_row; // == lines
154+
#else
155+
return -1;
156+
#endif
157+
#endif
158+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- G N A T C O L L --
4+
-- --
5+
-- Copyright (C) 2023-2024, AdaCore --
6+
-- --
7+
-- This library is free software; you can redistribute it and/or modify it --
8+
-- under terms of the GNU General Public License as published by the Free --
9+
-- Software Foundation; either version 3, or (at your option) any later --
10+
-- version. This library is distributed in the hope that it will be useful, --
11+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
12+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
13+
-- --
14+
-- As a special exception under Section 7 of GPL version 3, you are granted --
15+
-- additional permissions described in the GCC Runtime Library Exception, --
16+
-- version 3.1, as published by the Free Software Foundation. --
17+
-- --
18+
-- You should have received a copy of the GNU General Public License and --
19+
-- a copy of the GCC Runtime Library Exception along with this program; --
20+
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
21+
-- <http://www.gnu.org/licenses/>. --
22+
-- --
23+
------------------------------------------------------------------------------
24+
25+
26+
with GNATCOLL.Terminal; use GNATCOLL.Terminal;
27+
with Ada.Text_IO; use Ada.Text_IO;
28+
29+
procedure testlines is
30+
winrows, wincols: integer;
31+
info: terminal_info;
32+
begin
33+
info.init_for_stdout(auto);
34+
wincols:=get_width(info);
35+
winrows:=get_lines(info);
36+
37+
put("Lines:");
38+
put(integer'image(winrows));
39+
put(", Columns:");
40+
put(integer'image(wincols));
41+
new_line;
42+
43+
end testlines;
44+

0 commit comments

Comments
 (0)