- Notifications
You must be signed in to change notification settings - Fork 14.9k
[lldb-dap] Improve the runInTerminal ux. #163830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This updates lldb-dap to clear the screen when using `"console": "integratedTerminal"` or `"console": "externalTerminal"`. VSCode will reuse the same terminal for a given debug configuration. After the process exits it will return to the shell but if the debug session is launched again it will be invoked in the same terminal. VSCode is sending the termainl the launch args as terminal input which means the terminal would now have a string like `lldb-dap --comm-file ... --launch-target ...` and the scrollback buffer from any previous output or shell commands used in the terminal. To address this, I've updated LaunchRunInTerminalTarget to reset the cursor, clear the screen and clear the scrollback buffer to soft 'reset' the terminal prior to launching the process.
@llvm/pr-subscribers-lldb Author: John Harrison (ashgti) ChangesThis updates lldb-dap to clear the screen when using VSCode will reuse the same terminal for a given debug configuration. After the process exits it will return to the shell but if the debug session is launched again it will be invoked in the same terminal. VSCode is sending the terminal the launch args as terminal input which means the terminal would now have a string like To address this, I've updated LaunchRunInTerminalTarget to reset the cursor, clear the screen and clear the scrollback buffer to soft 'reset' the terminal prior to launching the process. Full diff: https://github.com/llvm/llvm-project/pull/163830.diff 1 Files Affected:
diff --git a/lldb/tools/lldb-dap/tool/lldb-dap.cpp b/lldb/tools/lldb-dap/tool/lldb-dap.cpp index e59cef9793366..7a077d220e47f 100644 --- a/lldb/tools/lldb-dap/tool/lldb-dap.cpp +++ b/lldb/tools/lldb-dap/tool/lldb-dap.cpp @@ -74,6 +74,7 @@ typedef int socklen_t; #include <netinet/in.h> #include <sys/socket.h> #include <sys/un.h> +#include <termios.h> #include <unistd.h> #endif @@ -121,6 +122,15 @@ class LLDBDAPOptTable : public llvm::opt::GenericOptTable { }; } // anonymous namespace +#define ESCAPE "\x1b" +#define CSI ESCAPE "[" +// Move the cursor to 0,0 +#define ANSI_CURSOR_HOME CSI "H" +// Clear the screen buffer +#define ANSI_ERASE_SCREEN CSI "2J" +// Clear the scroll back buffer +#define ANSI_ERASE_SCROLLBACK CSI "3J" + static void PrintHelp(LLDBDAPOptTable &table, llvm::StringRef tool_name) { std::string usage_str = tool_name.str() + " options"; table.printHelp(llvm::outs(), usage_str.c_str(), "LLDB DAP", false); @@ -261,6 +271,14 @@ static llvm::Error LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg, files.push_back(files.back()); if (llvm::Error err = SetupIORedirection(files)) return err; + } else if ((isatty(STDIN_FILENO) != 0) && + llvm::StringRef(getenv("TERM")).starts_with_insensitive("xterm")) { + // Clear the screen. + llvm::outs() << ANSI_CURSOR_HOME ANSI_ERASE_SCREEN ANSI_ERASE_SCROLLBACK; + // VSCode will reuse the same terminal for the same debug configuration + // between runs. Clear the input buffer prior to starting the new process so + // prior input is not carried forward to the new debug session. + tcflush(STDIN_FILENO, TCIFLUSH); } RunInTerminalLauncherCommChannel comm_channel(comm_file); |
// Cursor Position, set cursor to position [l, c] (default = [1, 1]). | ||
#define ANSI_CSI_CUP(...) ANSI_ESC_START #__VA_ARGS__ "H" | ||
// Reset cursor to position. | ||
#define ANSI_CSI_RESET_CURSOR ANSI_CSI_CUP() | ||
// Erase In Display. | ||
#define ANSI_CSI_ED(opt) ANSI_ESC_START #opt "J" | ||
// Erase complete viewport. | ||
#define ANSI_CSI_ERASE_VIEWPORT ANSI_CSI_ED(2) | ||
// Erase scrollback. | ||
#define ANSI_CSI_ERASE_SCROLLBACK ANSI_CSI_ED(3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'll adopt ANSI_CSI_ERASE_VIEWPORT
in the statusline which does this too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy
This updates lldb-dap to clear the screen when using
"console": "integratedTerminal"
or"console": "externalTerminal"
.VSCode will reuse the same terminal for a given debug configuration. After the process exits it will return to the shell but if the debug session is launched again it will be invoked in the same terminal. VSCode is sending the terminal the launch args as terminal input which means the terminal would now have a string like
lldb-dap --comm-file ... --launch-target ...
and the scrollback buffer from any previous output or shell commands used in the terminal.To address this, I've updated LaunchRunInTerminalTarget to reset the cursor, clear the screen and clear the scrollback buffer to soft 'reset' the terminal prior to launching the process.