diff --git a/src/gnatcoll-terminal.adb b/src/gnatcoll-terminal.adb index 8f48a0f3..3dc726d5 100644 --- a/src/gnatcoll-terminal.adb +++ b/src/gnatcoll-terminal.adb @@ -432,4 +432,21 @@ package body GNATCOLL.Terminal is return Internal (Boolean'Pos (Self.FD = Stderr)); end if; end Get_Width; + + --------------- + -- 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; + diff --git a/src/gnatcoll-terminal.ads b/src/gnatcoll-terminal.ads index 580b6353..8bde50c5 100644 --- a/src/gnatcoll-terminal.ads +++ b/src/gnatcoll-terminal.ads @@ -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 -- ----------- diff --git a/src/terminals.c b/src/terminals.c index e5150346..68225cd4 100644 --- a/src/terminals.c +++ b/src/terminals.c @@ -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; @@ -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 +} diff --git a/testsuite/tests/terminal/testlines.adb b/testsuite/tests/terminal/testlines.adb new file mode 100644 index 00000000..4338ed67 --- /dev/null +++ b/testsuite/tests/terminal/testlines.adb @@ -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 -- +-- . -- +-- -- +------------------------------------------------------------------------------ + + +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; +