From 909aa53efc36826b634e47a9201ee8b6404d8f33 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 5 Sep 2024 10:57:56 +0200 Subject: [PATCH] added: opm/models/utils/terminal.[ch]pp holds some terminal related methods that used to sit in parametersystem --- CMakeLists_files.cmake | 2 + opm/models/utils/parametersystem.cpp | 75 ++------------------ opm/models/utils/parametersystem.hpp | 6 -- opm/models/utils/start.hh | 8 +-- opm/models/utils/terminal.cpp | 101 +++++++++++++++++++++++++++ opm/models/utils/terminal.hpp | 49 +++++++++++++ 6 files changed, 160 insertions(+), 81 deletions(-) create mode 100644 opm/models/utils/terminal.cpp create mode 100644 opm/models/utils/terminal.hpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index b7188467f..678be2ec4 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -65,6 +65,7 @@ list (APPEND MAIN_SOURCE_FILES opm/models/parallel/tasklets.cpp opm/models/parallel/threadmanager.cpp opm/models/utils/parametersystem.cpp + opm/models/utils/terminal.cpp opm/models/utils/timer.cpp opm/simulators/flow/ActionHandler.cpp opm/simulators/flow/Banners.cpp @@ -720,6 +721,7 @@ list (APPEND PUBLIC_HEADER_FILES opm/models/utils/signum.hh opm/models/utils/simulator.hh opm/models/utils/start.hh + opm/models/utils/terminal.hpp opm/models/utils/timer.hpp opm/models/utils/timerguard.hh opm/simulators/flow/ActionHandler.hpp diff --git a/opm/models/utils/parametersystem.cpp b/opm/models/utils/parametersystem.cpp index 1dba9fa74..c7712118c 100644 --- a/opm/models/utils/parametersystem.cpp +++ b/opm/models/utils/parametersystem.cpp @@ -35,6 +35,8 @@ #include +#include + #if HAVE_QUAD #include #endif @@ -212,7 +214,7 @@ void printParamUsage(std::ostream& os, { std::string paramMessage, paramType, paramDescription; - int ttyWidth = Opm::Parameters::getTtyWidth(); + int ttyWidth = Opm::getTtyWidth(); // convert the CamelCase name to a command line --parameter-name. std::string cmdLineName = "-"; @@ -295,7 +297,7 @@ void printParamUsage(std::ostream& os, } } - paramMessage = Opm::Parameters::breakLines(paramMessage, /*indent=*/52, ttyWidth); + paramMessage = Opm::breakLines(paramMessage, /*indent=*/52, ttyWidth); paramMessage += "\n"; // print everything @@ -479,57 +481,6 @@ void SetDefault_(const std::string& paramName, } // namespace detail -std::string breakLines(const std::string& msg, - int indentWidth, - int maxWidth) -{ - std::string result; - int startInPos = 0; - int inPos = 0; - int lastBreakPos = 0; - int ttyPos = 0; - for (; inPos < int(msg.size()); ++ inPos, ++ ttyPos) { - if (msg[inPos] == '\n') { - result += msg.substr(startInPos, inPos - startInPos + 1); - startInPos = inPos + 1; - lastBreakPos = startInPos + 1; - - // we need to use -1 here because ttyPos is incremented after the loop body - ttyPos = -1; - continue; - } - - if (std::isspace(msg[inPos])) { - lastBreakPos = inPos; - } - - if (ttyPos >= maxWidth) { - if (lastBreakPos > startInPos) { - result += msg.substr(startInPos, lastBreakPos - startInPos); - startInPos = lastBreakPos + 1; - lastBreakPos = startInPos; - inPos = startInPos; - } - else { - result += msg.substr(startInPos, inPos - startInPos); - startInPos = inPos; - lastBreakPos = startInPos; - inPos = startInPos; - } - - result += "\n"; - for (int i = 0; i < indentWidth; ++i) { - result += " "; - } - ttyPos = indentWidth; - } - } - - result += msg.substr(startInPos); - - return result; -} - void reset() { MetaData::clear(); @@ -855,24 +806,6 @@ bool printUnused(std::ostream& os) return false; } -int getTtyWidth() -{ - int ttyWidth = 10*1000; // effectively do not break lines at all. - if (isatty(STDOUT_FILENO)) { -#if defined TIOCGWINSZ - // This is a bit too linux specific, IMO. let's do it anyway - struct winsize ttySize; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &ttySize); - ttyWidth = std::max(80, ttySize.ws_col); -#else - // default for systems that do not implement the TIOCGWINSZ ioctl - ttyWidth = 100; -#endif - } - - return ttyWidth; -} - namespace detail { template bool Get_(const std::string&, bool, bool); diff --git a/opm/models/utils/parametersystem.hpp b/opm/models/utils/parametersystem.hpp index 483ba7e6c..abdcf8f1b 100644 --- a/opm/models/utils/parametersystem.hpp +++ b/opm/models/utils/parametersystem.hpp @@ -93,12 +93,6 @@ void SetDefault_(const std::string& paramName, } -std::string breakLines(const std::string& msg, - int indentWidth, - int maxWidth); - -int getTtyWidth(); - //! \endcond /*! diff --git a/opm/models/utils/start.hh b/opm/models/utils/start.hh index 08a3c9bb4..b1a7ca8c1 100644 --- a/opm/models/utils/start.hh +++ b/opm/models/utils/start.hh @@ -33,7 +33,7 @@ #include #include - +#include #include #include @@ -341,9 +341,9 @@ static inline int start(int argc, char **argv, bool registerParams=true) #endif const std::string briefDescription = Problem::briefDescription(); if (!briefDescription.empty()) { - std::string tmp = Parameters::breakLines(briefDescription, - /*indentWidth=*/0, - Parameters::getTtyWidth()); + std::string tmp = breakLines(briefDescription, + /*indentWidth=*/0, + getTtyWidth()); std::cout << tmp << std::endl << std::endl; } else diff --git a/opm/models/utils/terminal.cpp b/opm/models/utils/terminal.cpp new file mode 100644 index 000000000..aa0b1d9bf --- /dev/null +++ b/opm/models/utils/terminal.cpp @@ -0,0 +1,101 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/* + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + + Consult the COPYING file in the top-level source directory of this + module for the precise wording of the license and the list of + copyright holders. +*/ + +#include +#include + +#include +#include + +namespace Opm { + +std::string breakLines(const std::string& msg, + int indentWidth, + int maxWidth) +{ + std::string result; + int startInPos = 0; + int inPos = 0; + int lastBreakPos = 0; + int ttyPos = 0; + for (; inPos < int(msg.size()); ++ inPos, ++ ttyPos) { + if (msg[inPos] == '\n') { + result += msg.substr(startInPos, inPos - startInPos + 1); + startInPos = inPos + 1; + lastBreakPos = startInPos + 1; + + // we need to use -1 here because ttyPos is incremented after the loop body + ttyPos = -1; + continue; + } + + if (std::isspace(msg[inPos])) { + lastBreakPos = inPos; + } + + if (ttyPos >= maxWidth) { + if (lastBreakPos > startInPos) { + result += msg.substr(startInPos, lastBreakPos - startInPos); + startInPos = lastBreakPos + 1; + lastBreakPos = startInPos; + inPos = startInPos; + } + else { + result += msg.substr(startInPos, inPos - startInPos); + startInPos = inPos; + lastBreakPos = startInPos; + inPos = startInPos; + } + + result += "\n"; + for (int i = 0; i < indentWidth; ++i) { + result += " "; + } + ttyPos = indentWidth; + } + } + + result += msg.substr(startInPos); + + return result; +} + +int getTtyWidth() +{ + int ttyWidth = 10*1000; // effectively do not break lines at all. + if (isatty(STDOUT_FILENO)) { +#if defined TIOCGWINSZ + // This is a bit too linux specific, IMO. let's do it anyway + struct winsize ttySize; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &ttySize); + ttyWidth = std::max(80, ttySize.ws_col); +#else + // default for systems that do not implement the TIOCGWINSZ ioctl + ttyWidth = 100; +#endif + } + + return ttyWidth; +} + +} // namespace Opm diff --git a/opm/models/utils/terminal.hpp b/opm/models/utils/terminal.hpp new file mode 100644 index 000000000..5ec5e2270 --- /dev/null +++ b/opm/models/utils/terminal.hpp @@ -0,0 +1,49 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/* + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + + Consult the COPYING file in the top-level source directory of this + module for the precise wording of the license and the list of + copyright holders. +*/ +#ifndef OPM_TERMINAL_HPP +#define OPM_TERMINAL_HPP + +#include + +namespace Opm { + +/*! + * \brief Break up a string in lines suitable for terminal output. + * \param msg String to print + * \param indentWidth Size of indent + * \param maxWidth Maximum with of terminal + * \return + */ +std::string breakLines(const std::string& msg, + int indentWidth, + int maxWidth); + +/*! + * \brief Get the width of the tty we are attached to. + * \return Width of tty + */ +int getTtyWidth(); + +} // namespace Opm + +#endif // OPM_TERMINAL_HPP