Skip to content

Commit a0aef7e

Browse files
[lldb] improve the heuristics for checking if a terminal supports Unicode
1 parent 04a1fd5 commit a0aef7e

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/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 TerminalSupportsUnicode() {
477+
static std::optional<bool> result;
478+
if (result)
479+
return result.value();
480+
#ifndef _WIN32
481+
if (const char *lang_var = std::getenv("LANG"))
482+
result = std::string(lang_var).find("UTF-8");
483+
else
484+
result = false;
485+
#else
486+
result = std::getenv("WT_SESSION") != nullptr;
487+
#endif
488+
return result.value();
489+
}

lldb/source/Utility/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/Utility/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 = "";

0 commit comments

Comments
 (0)