Skip to content

Commit e9462be

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

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

lldb/include/lldb/Host/Terminal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ class Terminal {
6868

6969
llvm::Error SetHardwareFlowControl(bool enabled);
7070

71+
/// Returns whether or not the current terminal supports Unicode rendering.
72+
///
73+
/// The value is cached after the first computation.
74+
///
75+
/// On POSIX systems, we check if the LANG environment variable contains the
76+
/// substring "UTF-8", case insensitive.
77+
///
78+
/// On Windows, we always return true since we use the `WriteConsoleW` API
79+
/// internally. Note that the default Windows codepage (437) does not support
80+
/// all Unicode characters. This function does not check the codepage.
81+
static bool SupportsUnicode();
82+
7183
protected:
7284
struct Data;
7385

lldb/source/Host/common/DiagnosticsRendering.cpp

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

99
#include "lldb/Host/common/DiagnosticsRendering.h"
10+
#include "lldb/Host/Terminal.h"
1011
#include <cstdint>
1112

1213
using namespace lldb_private;
@@ -102,7 +103,7 @@ void RenderDiagnosticDetails(Stream &stream,
102103
// characters. In the future it might make sense to move this into
103104
// Host so it can be customized for a specific platform.
104105
llvm::StringRef cursor, underline, vbar, joint, hbar, spacer;
105-
if (TerminalSupportsUnicode()) {
106+
if (Terminal::SupportsUnicode()) {
106107
cursor = "˄";
107108
underline = "˜";
108109
vbar = "";
@@ -231,20 +232,4 @@ void RenderDiagnosticDetails(Stream &stream,
231232
stream << detail.rendered << '\n';
232233
}
233234
}
234-
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-
250235
} // namespace lldb_private

lldb/source/Host/common/Terminal.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ llvm::Error Terminal::SetHardwareFlowControl(bool enabled) {
400400
#endif // LLDB_ENABLE_TERMIOS
401401
}
402402

403+
bool Terminal::SupportsUnicode() {
404+
static std::optional<bool> result;
405+
if (result)
406+
return result.value();
407+
#ifdef _WIN32
408+
return true;
409+
#else
410+
const char *lang_var = std::getenv("LANG");
411+
if (!lang_var)
412+
return false;
413+
result = llvm::StringRef(lang_var).lower().find("utf-8") != std::string::npos;
414+
#endif
415+
return result.value();
416+
}
417+
403418
TerminalState::TerminalState(Terminal term, bool save_process_group)
404419
: m_tty(term) {
405420
Save(term, save_process_group);

lldb/source/Target/StackFrameList.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "lldb/Target/Target.h"
2424
#include "lldb/Target/Thread.h"
2525
#include "lldb/Target/Unwind.h"
26-
#include "lldb/Utility/DiagnosticsRendering.h"
2726
#include "lldb/Utility/LLDBLog.h"
2827
#include "lldb/Utility/Log.h"
2928
#include "llvm/ADT/SmallPtrSet.h"
@@ -902,8 +901,8 @@ bool StackFrameList::IsPreviousFrameHidden(lldb_private::StackFrame &frame) {
902901
std::wstring StackFrameList::FrameMarker(lldb::StackFrameSP frame_sp,
903902
lldb::StackFrameSP selected_frame_sp) {
904903
if (frame_sp == selected_frame_sp) {
905-
return TerminalSupportsUnicode() ? L" * " : L"* ";
906-
} else if (!TerminalSupportsUnicode()) {
904+
return Terminal::SupportsUnicode() ? L" * " : L"* ";
905+
} else if (!Terminal::SupportsUnicode()) {
907906
return L" ";
908907
} else if (IsPreviousFrameHidden(*frame_sp)) {
909908
return L"";

0 commit comments

Comments
 (0)