From 13efca58f192293bcaeed64fe599022d22b4267a Mon Sep 17 00:00:00 2001 From: Jose Luis Blanco Claraco Date: Tue, 6 Feb 2024 23:46:06 +0100 Subject: [PATCH] Add hyperlink() --- doc/source/doxygen-docs/changelog.md | 2 + libs/system/include/mrpt/system/hyperlink.h | 28 ++++++++++++++ libs/system/src/hyperlink.cpp | 42 +++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 libs/system/include/mrpt/system/hyperlink.h create mode 100644 libs/system/src/hyperlink.cpp diff --git a/doc/source/doxygen-docs/changelog.md b/doc/source/doxygen-docs/changelog.md index 4939a33191..ba554ca4d2 100644 --- a/doc/source/doxygen-docs/changelog.md +++ b/doc/source/doxygen-docs/changelog.md @@ -15,6 +15,8 @@ - Add missing mrpt::ros1bridge::fromROS() for `PointCloud2` => mrpt::maps::CPointsMapXYZIRT conversions. - \ref mrpt_ros2bridge_grp: - Fix wrong macros leading to including obsolete header ``. + - \ref mrpt_system_grp: + - New function mrpt::system::hyperlink() to generate clickable links in terminal messages. # Version 2.11.7: Released Jan 25th, 2024 - Changes in apps: diff --git a/libs/system/include/mrpt/system/hyperlink.h b/libs/system/include/mrpt/system/hyperlink.h new file mode 100644 index 0000000000..74fd860b8e --- /dev/null +++ b/libs/system/include/mrpt/system/hyperlink.h @@ -0,0 +1,28 @@ +/* +------------------------------------------------------------------------+ + | Mobile Robot Programming Toolkit (MRPT) | + | https://www.mrpt.org/ | + | | + | Copyright (c) 2005-2024, Individual contributors, see AUTHORS file | + | See: https://www.mrpt.org/Authors - All rights reserved. | + | Released under BSD License. See: https://www.mrpt.org/License | + +------------------------------------------------------------------------+ */ +#pragma once + +#include +#include + +namespace mrpt::system +{ +/** Build an text with a hyperlink, if printed to a terminal that supports it + * (e.g. Ubuntu >=21.04), or the plain text otherwise. + * + * \ingroup mrpt_system_grp + * \note (New in MRPT 2.11.8) + * \sa See discussion in, for example, + * https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda + */ +std::string hyperlink( + const std::string& text, const std::string& uri, + bool force_use_format = false, bool show_uri_anyway = false); + +} // namespace mrpt::system diff --git a/libs/system/src/hyperlink.cpp b/libs/system/src/hyperlink.cpp new file mode 100644 index 0000000000..db4bde81e9 --- /dev/null +++ b/libs/system/src/hyperlink.cpp @@ -0,0 +1,42 @@ +/* +------------------------------------------------------------------------+ + | Mobile Robot Programming Toolkit (MRPT) | + | https://www.mrpt.org/ | + | | + | Copyright (c) 2005-2024, Individual contributors, see AUTHORS file | + | See: https://www.mrpt.org/Authors - All rights reserved. | + | Released under BSD License. See: https://www.mrpt.org/License | + +------------------------------------------------------------------------+ */ + +#include "system-precomp.h" // Precompiled headers +// +#include +#include + +#ifndef _WIN32 +#include + +#include +#endif + +std::string mrpt::system::hyperlink( + const std::string& text, const std::string& uri, bool force_use_format, + bool show_uri_anyway) +{ + using namespace std::string_literals; + + bool supportsLinks = false; + +#ifndef _WIN32 + thread_local bool coutIsAtty = isatty(fileno(stdout)); + supportsLinks = coutIsAtty; +#endif + // See: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda + if (supportsLinks || force_use_format) + return mrpt::format( + "\e]8;;%s\e\\%s\e]8;;\e\\", uri.c_str(), text.c_str()); + else + return show_uri_anyway ? // + (text + " ("s + uri + ")"s) + : // + text; +}