From 6a4071b5d213c52631fe2e73f6f8f51b40ac2e0b Mon Sep 17 00:00:00 2001 From: Andy Cedilnik Date: Thu, 16 Sep 2004 10:58:56 -0400 Subject: [PATCH] ENH: Add method to retrieve the terminal width --- Source/kwsys/SystemTools.cxx | 34 +++++++++++++++++++++++++++++++++ Source/kwsys/SystemTools.hxx.in | 7 +++++++ 2 files changed, 41 insertions(+) diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 3f6ee3783..1643c1635 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -35,6 +35,8 @@ #include #include #include +#include +#include #endif #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)) @@ -1846,6 +1848,38 @@ bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& l return haveData; } +int SystemTools::GetTerminalWidth() +{ + int width = -1; +#ifndef _WIN32 + struct winsize ws; + char *columns; /* Unix98 environment variable */ + if(ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col>0 && ws.ws_row>0) + { + width = ws.ws_col; + } + if(!isatty(STDOUT_FILENO)) + { + width = -1; + } + columns = getenv("COLUMNS"); + if(columns && *columns) + { + long t; + char *endptr; + t = strtol(columns, &endptr, 0); + if(endptr && !*endptr && (t>0) && (t<1000)) + { + width = (int)t; + } + } + if ( width < 9 ) + { + width = -1; + } +#endif + return width; +} } // namespace KWSYS_NAMESPACE #if defined(_MSC_VER) && defined(_DEBUG) diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in index 7c6e2ce0a..02caf2a1c 100644 --- a/Source/kwsys/SystemTools.hxx.in +++ b/Source/kwsys/SystemTools.hxx.in @@ -276,6 +276,13 @@ public: */ static bool GetLineFromStream(kwsys_ios::istream& istr, kwsys_stl::string& line, bool* has_newline=0); + + /** + * Get the width of the terminal window. The code may or may not work, so + * make sure you have some resonable defaults prepared if the code returns + * some bogus size. + */ + static int GetTerminalWidth(); protected: // these two functions can be called from ConvertToOutputPath