Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/gnatcoll-terminal.adb
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,21 @@ package body GNATCOLL.Terminal is
return Internal (Boolean'Pos (Self.FD = Stderr));
end if;
end Get_Width;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra blank lines - there should be at most one blank line as separation. (Same goes for lines 452-453.)

---------------
-- Get_Lines --
---------------

function Get_Lines (Self : Terminal_Info) return Integer is
function Internal (Stderr : Integer) return Integer;
pragma Import (C, Internal, "gnatcoll_terminal_lines");
begin
if Self.FD = File or else Self.Colors = Unsupported then
return -1;
else
return Internal (Boolean'Pos (Self.FD = Stderr));
end if;
end Get_Lines;

end GNATCOLL.Terminal;

4 changes: 4 additions & 0 deletions src/gnatcoll-terminal.ads
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ package GNATCOLL.Terminal is
-- Return the width of the terminal, or -1 if that width is either
-- unknown or does not apply (as is the case for files for instance).

function Get_Lines (Self : Terminal_Info) return Integer;
-- Return the height of the terminal, or -1 if that height is either
-- unknown or does not apply (as is the case for files for instance).

-----------
-- Utils --
-----------
Expand Down
25 changes: 23 additions & 2 deletions src/terminals.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ int gnatcoll_terminal_width(int forStderr) {
GetStdHandle (forStderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
if (GetConsoleScreenBufferInfo (handle, &csbiInfo)) {
return (int)csbiInfo.dwSize.X;
return (int)(csbiInfo.srWindow.Right-csbiInfo.srWindow.Left + 1); // window width
}
return -1;

#else
#ifdef TIOCGWINSZ
struct winsize w;
Expand All @@ -135,3 +135,24 @@ int gnatcoll_terminal_width(int forStderr) {
#endif
#endif
}

int gnatcoll_terminal_lines(int forStderr) {
#ifdef _WIN32 // MsWin
const HANDLE handle =
GetStdHandle (forStderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
if (GetConsoleScreenBufferInfo (handle, &csbiInfo)) {
return (int)(csbiInfo.srWindow.Bottom-csbiInfo.srWindow.Top + 1); // window height
}
return -1;

#else
#ifdef TIOCGWINSZ // Linux/OSX
struct winsize w;
ioctl(forStderr ? 1 : 0, TIOCGWINSZ, &w);
return w.ws_row; // == lines
#else
return -1;
#endif
#endif
}
44 changes: 44 additions & 0 deletions testsuite/tests/terminal/testlines.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
------------------------------------------------------------------------------
-- --
-- G N A T C O L L --
-- --
-- Copyright (C) 2006-2019, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------


with GNATCOLL.Terminal; use GNATCOLL.Terminal;
with Ada.Text_IO; use Ada.Text_IO;

procedure testlines is
winrows, wincols: integer;
info: terminal_info;
begin
info.init_for_stdout(auto);
wincols:=get_width(info);
winrows:=get_lines(info);

put("Lines:");
put(integer'image(winrows));
put(", Columns:");
put(integer'image(wincols));
new_line;

end testlines;