Skip to content

Commit 5a84c87

Browse files
ashgtiJDevlieghere
authored andcommitted
Automerge: [lldb-dap] Improve the runInTerminal ux. (#163830)
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. --------- Co-authored-by: Jonas Devlieghere <[email protected]>
2 parents d1d5af7 + db530bf commit 5a84c87

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lldb/include/lldb/Utility/AnsiTerminal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272

7373
#define ANSI_ESC_START_LEN 2
7474

75+
// Cursor Position, set cursor to position [l, c] (default = [1, 1]).
76+
#define ANSI_CSI_CUP(...) ANSI_ESC_START #__VA_ARGS__ "H"
77+
// Reset cursor to position.
78+
#define ANSI_CSI_RESET_CURSOR ANSI_CSI_CUP()
79+
// Erase In Display.
80+
#define ANSI_CSI_ED(opt) ANSI_ESC_START #opt "J"
81+
// Erase complete viewport.
82+
#define ANSI_CSI_ERASE_VIEWPORT ANSI_CSI_ED(2)
83+
// Erase scrollback.
84+
#define ANSI_CSI_ERASE_SCROLLBACK ANSI_CSI_ED(3)
85+
7586
// OSC (Operating System Commands)
7687
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
7788
#define OSC_ESCAPE_START "\033"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Host/MainLoopBase.h"
2222
#include "lldb/Host/MemoryMonitor.h"
2323
#include "lldb/Host/Socket.h"
24+
#include "lldb/Utility/AnsiTerminal.h"
2425
#include "lldb/Utility/Status.h"
2526
#include "lldb/Utility/UriParser.h"
2627
#include "lldb/lldb-forward.h"
@@ -74,6 +75,7 @@ typedef int socklen_t;
7475
#include <netinet/in.h>
7576
#include <sys/socket.h>
7677
#include <sys/un.h>
78+
#include <termios.h>
7779
#include <unistd.h>
7880
#endif
7981

@@ -261,6 +263,15 @@ static llvm::Error LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg,
261263
files.push_back(files.back());
262264
if (llvm::Error err = SetupIORedirection(files))
263265
return err;
266+
} else if ((isatty(STDIN_FILENO) != 0) &&
267+
llvm::StringRef(getenv("TERM")).starts_with_insensitive("xterm")) {
268+
// Clear the screen.
269+
llvm::outs() << ANSI_CSI_RESET_CURSOR ANSI_CSI_ERASE_VIEWPORT
270+
ANSI_CSI_ERASE_SCROLLBACK;
271+
// VS Code will reuse the same terminal for the same debug configuration
272+
// between runs. Clear the input buffer prior to starting the new process so
273+
// prior input is not carried forward to the new debug session.
274+
tcflush(STDIN_FILENO, TCIFLUSH);
264275
}
265276

266277
RunInTerminalLauncherCommChannel comm_channel(comm_file);

0 commit comments

Comments
 (0)