Skip to content

Commit

Permalink
Add hyperlink()
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Feb 6, 2024
1 parent 4c9da0f commit 13efca5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<cv_bridge/cv_bridge.h>`.
- \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:
Expand Down
28 changes: 28 additions & 0 deletions libs/system/include/mrpt/system/hyperlink.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <string>

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
42 changes: 42 additions & 0 deletions libs/system/src/hyperlink.cpp
Original file line number Diff line number Diff line change
@@ -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 <mrpt/core/format.h>
#include <mrpt/system/hyperlink.h>

#ifndef _WIN32
#include <unistd.h>

#include <cstdio>
#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;
}

0 comments on commit 13efca5

Please sign in to comment.