added: opm/models/utils/terminal.[ch]pp

holds some terminal related methods that used to sit
in parametersystem
This commit is contained in:
Arne Morten Kvarving 2024-09-05 10:57:56 +02:00
parent 9a0e39c1ad
commit 909aa53efc
6 changed files with 160 additions and 81 deletions

View File

@ -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

View File

@ -35,6 +35,8 @@
#include <dune/common/parametertree.hh>
#include <opm/models/utils/terminal.hpp>
#if HAVE_QUAD
#include <opm/material/common/quad.hpp>
#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<int>(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);

View File

@ -93,12 +93,6 @@ void SetDefault_(const std::string& paramName,
}
std::string breakLines(const std::string& msg,
int indentWidth,
int maxWidth);
int getTtyWidth();
//! \endcond
/*!

View File

@ -33,7 +33,7 @@
#include <opm/material/densead/Evaluation.hpp>
#include <opm/models/utils/parametersystem.hpp>
#include <opm/models/utils/terminal.hpp>
#include <opm/models/utils/simulator.hh>
#include <opm/models/utils/timer.hpp>
@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
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 <config.h>
#include <opm/models/utils/terminal.hpp>
#include <sys/ioctl.h>
#include <unistd.h>
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<int>(80, ttySize.ws_col);
#else
// default for systems that do not implement the TIOCGWINSZ ioctl
ttyWidth = 100;
#endif
}
return ttyWidth;
}
} // namespace Opm

View File

@ -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 <http://www.gnu.org/licenses/>.
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 <string>
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