Skip to content

Commit

Permalink
This plumbs #1882 and #1193 all the way through.
Browse files Browse the repository at this point in the history
  Unfortunately, I need to reset the cursor position, and I actually just need
  to do this differently entirely.

  iTerm actually maintains the last line of the buffer entirely. That's kind of
  important! Otherwise the prompt just disappears too.

  They're actually even smarter than that:
  * https://gitlab.com/gnachman/iterm2/-/issues/1330
  * https://gitlab.com/gnachman/iterm2/-/issues/4393

  and know where the prompt starts and ends, and keep all of multi-line prompts.
  That's a very 2023 feature, but we should keep at least one line.
  • Loading branch information
zadjii-msft committed Jul 29, 2021
1 parent 2436cc0 commit 8e0ed29
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 107 deletions.
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,20 @@ namespace winrt::TerminalApp::implementation
}
}
}

void TerminalPage::_HandleClearBuffer(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (args)
{
if (const auto& realArgs = args.ActionArgs().try_as<ClearBufferArgs>())
{
if (const auto termControl{ _GetActiveControl() })
{
termControl.ClearBuffer(realArgs.Clear());
args.Handled(true);
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,4 +1414,20 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_ReceivedOutputHandlers(*this, nullptr);
}

void ControlCore::ClearBuffer(Control::ClearBufferType clearType)
{
if (clearType == Control::ClearBufferType::Scrollback || clearType == Control::ClearBufferType::All)
{
_terminal->EraseInDisplay(::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType::Scrollback);
}

if (clearType == Control::ClearBufferType::Screen || clearType == Control::ClearBufferType::All)
{
if (auto conpty{ _connection.try_as<TerminalConnection::ConptyConnection>() })
{
conpty.ClearBuffer();
}
}
}

}
3 changes: 3 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
const short wheelDelta,
const ::Microsoft::Console::VirtualTerminal::TerminalInput::MouseButtonState state);
void UserScrollViewport(const int viewTop);

void ClearBuffer(Control::ClearBufferType clearType);

#pragma endregion

void BlinkAttributeTick();
Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ namespace Microsoft.Terminal.Control
IsRightButtonDown = 0x4
};


enum ClearBufferType
{
Screen,
Scrollback,
All
};

[default_interface] runtimeclass ControlCore : ICoreState
{
ControlCore(IControlSettings settings,
Expand Down Expand Up @@ -49,6 +57,7 @@ namespace Microsoft.Terminal.Control
Microsoft.Terminal.Core.ControlKeyStates modifiers);
void SendInput(String text);
void PasteText(String text);
void ClearBuffer(ClearBufferType clearType);

void SetHoveredCell(Microsoft.Terminal.Core.Point terminalPosition);
void ClearHoveredCell();
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_core.SendInput(wstr);
}
void TermControl::ClearBuffer(Control::ClearBufferType clearType)
{
_core.ClearBuffer(clearType);
}

void TermControl::ToggleShaderEffects()
{
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
til::point GetFontSize() const;

void SendInput(const winrt::hstring& input);
void ClearBuffer(Control::ClearBufferType clearType);

void ToggleShaderEffects();

winrt::fire_and_forget RenderEngineSwapChainChanged(IInspectable sender, IInspectable args);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "IControlSettings.idl";
import "IDirectKeyListener.idl";
import "EventArgs.idl";
import "ICoreState.idl";
import "ControlCore.idl";

namespace Microsoft.Terminal.Control
{
Expand Down Expand Up @@ -46,6 +47,7 @@ namespace Microsoft.Terminal.Control

Boolean CopySelectionToClipboard(Boolean singleLine, Windows.Foundation.IReference<CopyFormat> formats);
void PasteTextFromClipboard();
void ClearBuffer(ClearBufferType clearType);
void Close();
Windows.Foundation.Size CharacterDimensions { get; };
Windows.Foundation.Size MinimumSize { get; };
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static constexpr std::string_view OpenWindowRenamerKey{ "openWindowRenamer" };
static constexpr std::string_view GlobalSummonKey{ "globalSummon" };
static constexpr std::string_view QuakeModeKey{ "quakeMode" };
static constexpr std::string_view FocusPaneKey{ "focusPane" };
static constexpr std::string_view ClearBufferKey{ "clearBuffer" };

static constexpr std::string_view ActionKey{ "action" };

Expand Down Expand Up @@ -362,6 +363,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::GlobalSummon, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::QuakeMode, RS_(L"QuakeModeCommandKey") },
{ ShortcutAction::FocusPane, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::ClearBuffer, L"" }, // TODO!
};
}();

Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "RenameWindowArgs.g.cpp"
#include "GlobalSummonArgs.g.cpp"
#include "FocusPaneArgs.g.cpp"
#include "ClearBufferArgs.g.cpp"

#include <LibraryResources.h>

Expand Down Expand Up @@ -662,4 +663,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
Id())
};
}
winrt::hstring ClearBufferArgs::GenerateName() const
{
// TODO!
return winrt::hstring{ L"Clear buffer" };
}
}
52 changes: 52 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "RenameWindowArgs.g.h"
#include "GlobalSummonArgs.g.h"
#include "FocusPaneArgs.g.h"
#include "ClearBufferArgs.g.h"

#include "../../cascadia/inc/cppwinrt_utils.h"
#include "JsonUtils.h"
Expand Down Expand Up @@ -1698,6 +1699,56 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
};

struct ClearBufferArgs : public ClearBufferArgsT<ClearBufferArgs>
{
ClearBufferArgs() = default;
ClearBufferArgs(winrt::Microsoft::Terminal::Control::ClearBufferType clearType) :
_Clear{ clearType } {};
WINRT_PROPERTY(winrt::Microsoft::Terminal::Control::ClearBufferType, Clear, winrt::Microsoft::Terminal::Control::ClearBufferType::All);
static constexpr std::string_view ClearKey{ "clear" };

public:
hstring GenerateName() const;

bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<ClearBufferArgs>();
if (otherAsUs)
{
return otherAsUs->_Clear == _Clear;
}
return false;
};
static FromJsonResult FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<ClearBufferArgs>();
JsonUtils::GetValueForKey(json, ClearKey, args->_Clear);
return { *args, {} };
}
static Json::Value ToJson(const IActionArgs& val)
{
if (!val)
{
return {};
}
Json::Value json{ Json::ValueType::objectValue };
const auto args{ get_self<ClearBufferArgs>(val) };
JsonUtils::SetValueForKey(json, ClearKey, args->_Clear);
return json;
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<ClearBufferArgs>() };
copy->_Clear = _Clear;
return *copy;
}
size_t Hash() const
{
return ::Microsoft::Terminal::Settings::Model::HashUtils::HashProperty(_Clear);
}
};

}

namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
Expand All @@ -1719,4 +1770,5 @@ namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
BASIC_FACTORY(FindMatchArgs);
BASIC_FACTORY(NewWindowArgs);
BASIC_FACTORY(FocusPaneArgs);
BASIC_FACTORY(ClearBufferArgs);
}
6 changes: 6 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,10 @@ namespace Microsoft.Terminal.Settings.Model
FocusPaneArgs(UInt32 Id);
UInt32 Id { get; };
};

[default_interface] runtimeclass ClearBufferArgs : IActionArgs
{
ClearBufferArgs(Microsoft.Terminal.Control.ClearBufferType clear);
Microsoft.Terminal.Control.ClearBufferType Clear { get; };
};
}
6 changes: 4 additions & 2 deletions src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
ON_ALL_ACTIONS(OpenWindowRenamer) \
ON_ALL_ACTIONS(GlobalSummon) \
ON_ALL_ACTIONS(QuakeMode) \
ON_ALL_ACTIONS(FocusPane)
ON_ALL_ACTIONS(FocusPane) \
ON_ALL_ACTIONS(ClearBuffer)

#define ALL_SHORTCUT_ACTIONS_WITH_ARGS \
ON_ALL_ACTIONS_WITH_ARGS(AdjustFontSize) \
Expand Down Expand Up @@ -106,4 +107,5 @@
ON_ALL_ACTIONS_WITH_ARGS(SplitPane) \
ON_ALL_ACTIONS_WITH_ARGS(SwitchToTab) \
ON_ALL_ACTIONS_WITH_ARGS(ToggleCommandPalette) \
ON_ALL_ACTIONS_WITH_ARGS(FocusPane)
ON_ALL_ACTIONS_WITH_ARGS(FocusPane) \
ON_ALL_ACTIONS_WITH_ARGS(ClearBuffer)
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,12 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::MonitorBehavior)
pair_type{ "toMouse", ValueType::ToMouse },
};
};

JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Control::ClearBufferType)
{
JSON_MAPPINGS(3) = {
pair_type{ "all", ValueType::All },
pair_type{ "screen", ValueType::Screen },
pair_type{ "scrollback", ValueType::Scrollback },
};
};
2 changes: 2 additions & 0 deletions src/inc/conpty-static.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ HRESULT WINAPI ConptyCreatePseudoConsole(COORD size, HANDLE hInput, HANDLE hOutp

HRESULT WINAPI ConptyResizePseudoConsole(HPCON hPC, COORD size);

HRESULT WINAPI ConptyClearPseudoConsole(HPCON hPC);

VOID WINAPI ConptyClosePseudoConsole(HPCON hPC);

HRESULT WINAPI ConptyPackPseudoConsole(HANDLE hServerProcess, HANDLE hRef, HANDLE hSignal, HPCON* phPC);
Expand Down
Loading

0 comments on commit 8e0ed29

Please sign in to comment.