Skip to content

Commit ed8dc4b

Browse files
[lldb] improve the heuristics for checking if a terminal supports Unicode
1 parent 3e24dad commit ed8dc4b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lldb/include/lldb/Utility/DiagnosticsRendering.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ void RenderDiagnosticDetails(Stream &stream,
6464
bool show_inline,
6565
llvm::ArrayRef<DiagnosticDetail> details);
6666

67+
/// Returns whether or not the current terminal supports Unicode rendering.
68+
///
69+
/// The value is cached after the first computation.
70+
///
71+
/// On POSIX systems, we check if the LANG environment variable contains the
72+
/// substring "UTF-8";
73+
///
74+
/// On Windows, we check that we are running from the Windows Terminal
75+
/// application.
76+
bool TerminalSupportsUnicode();
77+
6778
class DiagnosticError
6879
: public llvm::ErrorInfo<DiagnosticError, CloneableECError> {
6980
public:

lldb/source/Utility/DiagnosticsRendering.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void RenderDiagnosticDetails(Stream &stream,
102102
// characters. In the future it might make sense to move this into
103103
// Host so it can be customized for a specific platform.
104104
llvm::StringRef cursor, underline, vbar, joint, hbar, spacer;
105-
if (stream.AsRawOstream().colors_enabled()) {
105+
if (TerminalSupportsUnicode()) {
106106
cursor = "˄";
107107
underline = "˜";
108108
vbar = "";
@@ -232,4 +232,19 @@ void RenderDiagnosticDetails(Stream &stream,
232232
}
233233
}
234234

235+
bool TerminalSupportsUnicode() {
236+
static std::optional<bool> result;
237+
if (result)
238+
return result.value();
239+
#ifndef _WIN32
240+
if (const char *lang_var = std::getenv("LANG"))
241+
result = std::string(lang_var).find("UTF-8");
242+
else
243+
result = false;
244+
#else
245+
result = std::getenv("WT_SESSION") != nullptr;
246+
#endif
247+
return result.value();
248+
}
249+
235250
} // namespace lldb_private

0 commit comments

Comments
 (0)