Skip to content

Commit 41676ba

Browse files
[lldb] improve the heuristics for checking if a terminal supports Unicode
1 parent 80e4d5b commit 41676ba

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

lldb/include/lldb/Host/Terminal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ class TerminalState {
169169
lldb::pid_t m_process_group = -1; ///< Cached process group information.
170170
};
171171

172+
/// Returns whether or not the current terminal supports Unicode rendering.
173+
///
174+
/// The value is cached after the first computation.
175+
///
176+
/// On POSIX systems, we check if the LANG environment variable contains the
177+
/// substring "UTF-8";
178+
///
179+
/// On Windows, we check that we are running from the Windows Terminal
180+
/// application.
181+
bool TerminalSupportsUnicode();
182+
172183
} // namespace lldb_private
173184

174185
#endif // LLDB_HOST_TERMINAL_H

lldb/source/Host/common/DiagnosticsRendering.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Host/common/DiagnosticsRendering.h"
10+
#include "lldb/Host/Terminal.h"
11+
1012
#include <cstdint>
1113

1214
using namespace lldb_private;
@@ -97,12 +99,8 @@ void RenderDiagnosticDetails(Stream &stream,
9799
return;
98100
}
99101

100-
// Since there is no other way to find this out, use the color
101-
// attribute as a proxy for whether the terminal supports Unicode
102-
// characters. In the future it might make sense to move this into
103-
// Host so it can be customized for a specific platform.
104102
llvm::StringRef cursor, underline, vbar, joint, hbar, spacer;
105-
if (stream.AsRawOstream().colors_enabled()) {
103+
if (TerminalSupportsUnicode()) {
106104
cursor = "˄";
107105
underline = "˜";
108106
vbar = "";

lldb/source/Host/common/Terminal.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,18 @@ bool TerminalState::TTYStateIsValid() const { return bool(m_data); }
472472
bool TerminalState::ProcessGroupIsValid() const {
473473
return static_cast<int32_t>(m_process_group) != -1;
474474
}
475+
476+
bool lldb_private::TerminalSupportsUnicode() {
477+
static std::optional<bool> result;
478+
if (result)
479+
return result.value();
480+
#ifdef _WIN32
481+
return true;
482+
#else
483+
const char *lang_var = std::getenv("LANG");
484+
if (!lang_var)
485+
return false;
486+
result = llvm::StringRef(lang_var).lower().find("utf-8") != std::string::npos;
487+
#endif
488+
return result.value();
489+
}

0 commit comments

Comments
 (0)