Skip to content
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

[textinput] Clear the terminal screen on Ctrl+L #10078

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/textinput/src/textinput/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace textinput {
const TextInputContext* GetContext() const { return fContext; }
void SetContext(TextInputContext* C) { fContext = C; }

/// If is a TTY, clear the terminal screen
virtual void Clear() {}
virtual void Redraw() { NotifyTextChange(Range::AllWithPrompt()); }

virtual void NotifyTextChange(Range r) = 0; // Update the displayed text
Expand Down
6 changes: 6 additions & 0 deletions core/textinput/src/textinput/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cctype>
#include <vector>
#include "textinput/Callbacks.h"
#include "textinput/Display.h"
#include "textinput/History.h"
#include "textinput/KeyBinding.h"
#include "textinput/StreamReaderUnix.h"
Expand Down Expand Up @@ -438,6 +439,11 @@ namespace textinput {
fReplayHistEntry = fCurHistEntry;
return kPRSuccess;
case kCmdClearScreen:
for (auto *D : fContext->GetDisplays()) {
D->Clear();
D->Redraw();
}
return kPRSuccess;
case kCmd_END_TEXT_MODIFYING_CMDS:
return kPRError;
case kCmdEsc:
Expand Down
12 changes: 6 additions & 6 deletions core/textinput/src/textinput/TerminalDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ namespace textinput {
~TerminalDisplay();
static TerminalDisplay* Create();

void NotifyTextChange(Range r);
void NotifyCursorChange();
void NotifyResetInput();
void NotifyError();
void Detach();
void DisplayInfo(const std::vector<std::string>& Options);
void NotifyTextChange(Range r) override;
void NotifyCursorChange() override;
void NotifyResetInput() override;
void NotifyError() override;
void Detach() override;
void DisplayInfo(const std::vector<std::string>& Options) override;
bool IsTTY() const { return fIsTTY; }

protected:
Expand Down
7 changes: 7 additions & 0 deletions core/textinput/src/textinput/TerminalDisplayUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ namespace textinput {
#endif
}

void
TerminalDisplayUnix::Clear() {
static const char text[] = "\033[2J\033[H";
if (!IsTTY()) return;
WriteRawString(text, sizeof(text));
}

void
TerminalDisplayUnix::SetColor(char CIdx, const Color& C) {
if (!IsTTY()) return;
Expand Down
23 changes: 12 additions & 11 deletions core/textinput/src/textinput/TerminalDisplayUnix.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ namespace textinput {
~TerminalDisplayUnix();

void HandleResizeSignal();
void Clear() override;

void Attach();
void Detach();
void Attach() override;
void Detach() override;

protected:
void MoveUp(size_t nLines = 1);
void MoveDown(size_t nLines = 1);
void MoveLeft(size_t nCols = 1);
void MoveRight(size_t nCols = 1);
void MoveUp(size_t nLines = 1) override;
void MoveDown(size_t nLines = 1) override;
void MoveLeft(size_t nCols = 1) override;
void MoveRight(size_t nCols = 1) override;
void MoveInternal(char What, size_t n);
void MoveFront();
void SetColor(char CIdx, const Color& C);
void WriteRawString(const char* text, size_t len);
void ActOnEOL();
void EraseToRight();
void MoveFront() override;
void SetColor(char CIdx, const Color& C) override;
void WriteRawString(const char* text, size_t len) override;
void ActOnEOL() override;
void EraseToRight() override;
int GetClosestColorIdx256(const Color& C);
int GetClosestColorIdx16(const Color& C);

Expand Down
8 changes: 8 additions & 0 deletions core/textinput/src/textinput/TerminalDisplayWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ namespace textinput {
}
}

void
TerminalDisplayWin::Clear() {
static const char text[] = "\033[2J\033[H";
if (!IsTTY()) return;
EnableVTProcessingRAII RAII(fOut);
WriteRawString(text, sizeof(text));
}

void
TerminalDisplayWin::SetColor(char CIdx, const Color& C) {
WORD Attribs = 0;
Expand Down
36 changes: 24 additions & 12 deletions core/textinput/src/textinput/TerminalDisplayWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ namespace textinput {
~TerminalDisplayWin();

void HandleResizeEvent();
void Clear() override;

void Attach();
void Detach();
void Attach() override;
void Detach() override;

protected:
void Move(Pos p);
void Move(Pos p) override;
void MoveInternal(Pos p);
void MoveUp(size_t nLines = 1);
void MoveDown(size_t nLines = 1);
void MoveLeft(size_t nCols = 1);
void MoveRight(size_t nCols = 1);
void MoveFront();
void SetColor(char CIdx, const Color& C);
void WriteRawString(const char* text, size_t len);

void EraseToRight();
void MoveUp(size_t nLines = 1) override;
void MoveDown(size_t nLines = 1) override;
void MoveLeft(size_t nCols = 1) override;
void MoveRight(size_t nCols = 1) override;
void MoveFront() override;
void SetColor(char CIdx, const Color& C) override;
void WriteRawString(const char* text, size_t len) override;

void EraseToRight() override;
void CheckCursorPos();

void ShowError(const char* Where) const;
Expand All @@ -55,6 +56,17 @@ namespace textinput {
DWORD fMyMode; // console configuration when active
WORD fDefaultAttributes; // attributes to restore on destruction
const UINT fOldCodePage; // saved codepage of console

/// RAII object that temporarily enables VT sequences for the given console
struct EnableVTProcessingRAII {
HANDLE fConsole;
DWORD fMode;
EnableVTProcessingRAII(HANDLE con) : fConsole(con) {
::GetConsoleMode(fConsole, &fMode);
::SetConsoleMode(fConsole, fMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
~EnableVTProcessingRAII() { ::SetConsoleMode(fConsole, fMode); }
};
};
}
#endif // TEXTINPUT_TERMINALDISPLAYWIN_H