Skip to content

Commit a2248f1

Browse files
committed
[lldb-dap] Improve the runInTerminal ux.
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.
1 parent 3f3af56 commit a2248f1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lldb/tools/lldb-dap/tool/lldb-dap.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef int socklen_t;
7474
#include <netinet/in.h>
7575
#include <sys/socket.h>
7676
#include <sys/un.h>
77+
#include <termios.h>
7778
#include <unistd.h>
7879
#endif
7980

@@ -121,6 +122,15 @@ class LLDBDAPOptTable : public llvm::opt::GenericOptTable {
121122
};
122123
} // anonymous namespace
123124

125+
#define ESCAPE "\x1b"
126+
#define CSI ESCAPE "["
127+
// Move the cursor to 0,0
128+
#define ANSI_CURSOR_HOME CSI "H"
129+
// Clear the screen buffer
130+
#define ANSI_ERASE_SCREEN CSI "2J"
131+
// Clear the scroll back buffer
132+
#define ANSI_ERASE_SCROLLBACK CSI "3J"
133+
124134
static void PrintHelp(LLDBDAPOptTable &table, llvm::StringRef tool_name) {
125135
std::string usage_str = tool_name.str() + " options";
126136
table.printHelp(llvm::outs(), usage_str.c_str(), "LLDB DAP", false);
@@ -261,6 +271,14 @@ static llvm::Error LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg,
261271
files.push_back(files.back());
262272
if (llvm::Error err = SetupIORedirection(files))
263273
return err;
274+
} else if ((isatty(STDIN_FILENO) != 0) &&
275+
llvm::StringRef(getenv("TERM")).starts_with_insensitive("xterm")) {
276+
// Clear the screen.
277+
llvm::outs() << ANSI_CURSOR_HOME ANSI_ERASE_SCREEN ANSI_ERASE_SCROLLBACK;
278+
// VSCode will reuse the same terminal for the same debug configuration
279+
// between runs. Clear the input buffer prior to starting the new process so
280+
// prior input is not carried forward to the new debug session.
281+
tcflush(STDIN_FILENO, TCIFLUSH);
264282
}
265283

266284
RunInTerminalLauncherCommChannel comm_channel(comm_file);

0 commit comments

Comments
 (0)