[Lldb-commits] [lldb] 080635e - [lldb] Add a setting to change the autosuggestion ANSI escape codes
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Mon Mar 7 08:55:12 PST 2022
Author: Jonas Devlieghere
Date: 2022-03-07T08:54:37-08:00
New Revision: 080635ef27914c3f0c20d221bc2f02ab6f13e6b4
URL: https://github.com/llvm/llvm-project/commit/080635ef27914c3f0c20d221bc2f02ab6f13e6b4
DIFF: https://github.com/llvm/llvm-project/commit/080635ef27914c3f0c20d221bc2f02ab6f13e6b4.diff
LOG: [lldb] Add a setting to change the autosuggestion ANSI escape codes
I'm a big fan of the autosuggestion feature but my terminal/color scheme
doesn't display faint any differently than regular lldb output, which
makes the feature a little confusing. This patch add a setting to change
the autosuggestion ANSI escape codes.
For example, to display the autosuggestion in italic, you can add this
to your ~/.lldbinit
settings set show-autosuggestion-ansi-prefix ${ansi.italic}
setting set show-autosuggestion-ansi-suffix ${ansi.normal}
Differential revision: https://reviews.llvm.org/D121064
Added:
Modified:
lldb/include/lldb/Core/Debugger.h
lldb/include/lldb/Host/Editline.h
lldb/source/Core/CoreProperties.td
lldb/source/Core/Debugger.cpp
lldb/source/Core/IOHandler.cpp
lldb/source/Host/common/Editline.cpp
lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py
Removed:
################################################################################
diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h
index d4fae0c5d09cb..d801e2d874df4 100644
--- a/lldb/include/lldb/Core/Debugger.h
+++ b/lldb/include/lldb/Core/Debugger.h
@@ -331,6 +331,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
bool GetUseAutosuggestion() const;
+ llvm::StringRef GetAutosuggestionAnsiPrefix() const;
+
+ llvm::StringRef GetAutosuggestionAnsiSuffix() const;
+
bool GetUseSourceCache() const;
bool SetUseSourceCache(bool use_source_cache);
diff --git a/lldb/include/lldb/Host/Editline.h b/lldb/include/lldb/Host/Editline.h
index 876f6052311e9..14b6778d20764 100644
--- a/lldb/include/lldb/Host/Editline.h
+++ b/lldb/include/lldb/Host/Editline.h
@@ -210,6 +210,14 @@ class Editline {
m_fix_indentation_callback_chars = indent_chars;
}
+ void SetSuggestionAnsiPrefix(std::string prefix) {
+ m_suggestion_ansi_prefix = std::move(prefix);
+ }
+
+ void SetSuggestionAnsiSuffix(std::string suffix) {
+ m_suggestion_ansi_suffix = std::move(suffix);
+ }
+
/// Prompts for and reads a single line of user input.
bool GetLine(std::string &line, bool &interrupted);
@@ -388,9 +396,11 @@ class Editline {
const char *m_fix_indentation_callback_chars = nullptr;
CompleteCallbackType m_completion_callback;
-
SuggestionCallbackType m_suggestion_callback;
+ std::string m_suggestion_ansi_prefix;
+ std::string m_suggestion_ansi_suffix;
+
std::size_t m_previous_autosuggestion_size = 0;
std::mutex m_output_mutex;
};
diff --git a/lldb/source/Core/CoreProperties.td b/lldb/source/Core/CoreProperties.td
index 399407075a7e0..1811f66efd0e2 100644
--- a/lldb/source/Core/CoreProperties.td
+++ b/lldb/source/Core/CoreProperties.td
@@ -163,4 +163,12 @@ let Definition = "debugger" in {
Global,
DefaultFalse,
Desc<"If true, LLDB will show suggestions to complete the command the user typed. Suggestions may be accepted using Ctrl-F.">;
+ def ShowAutosuggestionAnsiPrefix: Property<"show-autosuggestion-ansi-prefix", "String">,
+ Global,
+ DefaultStringValue<"${ansi.faint}">,
+ Desc<"When displaying suggestion in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately before the suggestion.">;
+ def ShowAutosuggestionAnsiSuffix: Property<"show-autosuggestion-ansi-suffix", "String">,
+ Global,
+ DefaultStringValue<"${ansi.normal}">,
+ Desc<"When displaying suggestion in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately after the suggestion.">;
}
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index ae4fb93e6d4af..eb0f6b5371d6b 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -384,6 +384,16 @@ bool Debugger::GetUseAutosuggestion() const {
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
}
+llvm::StringRef Debugger::GetAutosuggestionAnsiPrefix() const {
+ const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix;
+ return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
+}
+
+llvm::StringRef Debugger::GetAutosuggestionAnsiSuffix() const {
+ const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix;
+ return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
+}
+
bool Debugger::GetUseSourceCache() const {
const uint32_t idx = ePropertyUseSourceCache;
return m_collection_sp->GetPropertyAtIndexAsBoolean(
diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp
index c35b17990842f..179b60c3586e7 100644
--- a/lldb/source/Core/IOHandler.cpp
+++ b/lldb/source/Core/IOHandler.cpp
@@ -17,6 +17,7 @@
#include "lldb/Core/StreamFile.h"
#include "lldb/Host/Config.h"
#include "lldb/Host/File.h"
+#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/Predicate.h"
#include "lldb/Utility/ReproducerProvider.h"
#include "lldb/Utility/Status.h"
@@ -273,10 +274,14 @@ IOHandlerEditline::IOHandlerEditline(
this->AutoCompleteCallback(request);
});
- if (debugger.GetUseAutosuggestion() && debugger.GetUseColor()) {
+ if (debugger.GetUseAutosuggestion()) {
m_editline_up->SetSuggestionCallback([this](llvm::StringRef line) {
return this->SuggestionCallback(line);
});
+ m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes(
+ debugger.GetAutosuggestionAnsiPrefix()));
+ m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes(
+ debugger.GetAutosuggestionAnsiSuffix()));
}
// See if the delegate supports fixing indentation
const char *indent_chars = delegate.IOHandlerGetFixIndentationCharacters();
diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp
index 6898f8452161d..826db61f19a6b 100644
--- a/lldb/source/Host/common/Editline.cpp
+++ b/lldb/source/Host/common/Editline.cpp
@@ -1080,8 +1080,13 @@ unsigned char Editline::TypedCharacter(int ch) {
llvm::StringRef line(line_info->buffer,
line_info->lastchar - line_info->buffer);
+ const char *ansi_prefix =
+ m_color_prompts ? m_suggestion_ansi_prefix.c_str() : "";
+ const char *ansi_suffix =
+ m_color_prompts ? m_suggestion_ansi_suffix.c_str() : "";
+
if (llvm::Optional<std::string> to_add = m_suggestion_callback(line)) {
- std::string to_add_color = ANSI_FAINT + to_add.getValue() + ANSI_UNFAINT;
+ std::string to_add_color = ansi_prefix + to_add.getValue() + ansi_suffix;
fputs(typed.c_str(), m_output_file);
fputs(to_add_color.c_str(), m_output_file);
size_t new_autosuggestion_size = line.size() + to_add->length();
diff --git a/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py b/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py
index 3c8d20b2d1dea..80e45222a8157 100644
--- a/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py
+++ b/lldb/test/API/iohandler/autosuggestion/TestAutosuggestion.py
@@ -16,6 +16,11 @@ class TestCase(PExpectTest):
mydir = TestBase.compute_mydir(__file__)
+ ANSI_FAINT = "\x1b[2m"
+ ANSI_RESET = "\x1b[0m"
+ ANSI_RED = "\x1b[31m"
+ ANSI_CYAN = "\x1b[36m"
+
# PExpect uses many timeouts internally and doesn't play well
# under ASAN on a loaded machine..
@skipIfAsan
@@ -23,15 +28,12 @@ class TestCase(PExpectTest):
def test_autosuggestion_add_spaces(self):
self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
- # Common input codes and escape sequences.
- faint_color = "\x1b[2m"
- reset = "\x1b[0m"
# Check if spaces are added to hide the previous gray characters.
self.expect("help frame var")
self.expect("help frame info")
self.child.send("help frame v")
- self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + faint_color + "ar" + reset + " ")
+ self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + self.ANSI_FAINT + "ar" + self.ANSI_RESET + " ")
self.quit()
@@ -40,10 +42,8 @@ def test_autosuggestion_add_spaces(self):
def test_autosuggestion(self):
self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
- # Common input codes and escape sequences.
+ # Common input codes.
ctrl_f = "\x06"
- faint_color = "\x1b[2m"
- reset = "\x1b[0m"
delete = chr(127)
frame_output_needle = "Syntax: frame <subcommand>"
@@ -52,7 +52,7 @@ def test_autosuggestion(self):
# Check that LLDB shows the autosuggestion in gray behind the text.
self.child.send("hel")
- self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p frame" + reset)
+ self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + self.ANSI_FAINT + "p frame" + self.ANSI_RESET)
# Apply the autosuggestion and press enter. This should print the
# 'help frame' output if everything went correctly.
@@ -80,7 +80,7 @@ def test_autosuggestion(self):
# Check that autosuggestion complete to the most recent one.
self.child.send("help frame variable\n")
self.child.send("help fr")
- self.child.expect_exact(faint_color + "ame variable" + reset)
+ self.child.expect_exact(self.ANSI_FAINT + "ame variable" + self.ANSI_RESET)
self.child.send("\n")
# Try another command.
@@ -90,7 +90,7 @@ def test_autosuggestion(self):
# Check that 'hel' should have an autosuggestion for 'help apropos' now.
self.child.send("hel")
- self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p apropos" + reset)
+ self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + self.ANSI_FAINT + "p apropos" + self.ANSI_RESET)
# Run the command and expect the 'help apropos' output.
self.child.send(ctrl_f + "\n")
@@ -102,4 +102,19 @@ def test_autosuggestion(self):
self.child.expect_exact(breakpoint_output_needle)
+ self.quit()
+
+ @skipIfAsan
+ @skipIfEditlineSupportMissing
+ def test_autosuggestion_custom_ansi_prefix_suffix(self):
+ self.launch(extra_args=["-o", "settings set show-autosuggestion true",
+ "-o", "settings set use-color true",
+ "-o", "settings set show-autosuggestion-ansi-prefix ${ansi.fg.red}",
+ "-o", "setting set show-autosuggestion-ansi-suffix ${ansi.fg.cyan}"])
+
+ self.child.send("help frame variable\n")
+ self.child.send("help fr")
+ self.child.expect_exact(self.ANSI_RED + "ame variable" + self.ANSI_CYAN)
+ self.child.send("\n")
+
self.quit()
More information about the lldb-commits
mailing list