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

Add support for opening the Suggestions UI with recent commands #14943

Merged
merged 95 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
c96799c
Preview the input via the TSF input control. This is awesome, and sho…
zadjii-msft Mar 22, 2022
1449088
bugfixes for the demo
zadjii-msft Apr 1, 2022
d3b5533
fix remaining bugs
zadjii-msft Apr 29, 2022
0bda66f
a comment I missed
zadjii-msft Apr 29, 2022
ccfc834
Migrate spelling-0.0.21 changes from main
DHowett Apr 29, 2022
c97ac66
resart with fresh plumbing
zadjii-msft Feb 9, 2023
7404dc3
zhu li, do the thing
zadjii-msft Feb 10, 2023
f361b6c
lots of removal of dead code from the sxnui
zadjii-msft Feb 10, 2023
b0fa972
make the menu mode compact, and remove the search box
zadjii-msft Feb 12, 2023
985fcdb
better UX for typing
zadjii-msft Feb 12, 2023
e785bfc
Merge branch 'main' into dev/migrie/f/12861-preview-input
zadjii-msft Feb 16, 2023
48e7348
small updates to the protocol
zadjii-msft Feb 16, 2023
026f342
Merge remote-tracking branch 'origin/dev/migrie/f/3121-wE-dOnT-hAvE-d…
zadjii-msft Feb 21, 2023
ff5eead
Very important that the backspaces are trimmed from the preview
zadjii-msft Feb 21, 2023
2c66c32
bottoms up bottoms up
zadjii-msft Feb 21, 2023
d94183b
icons are _slick_
zadjii-msft Feb 21, 2023
8feb909
bottom-up mode is basically done
zadjii-msft Feb 21, 2023
f5909d9
Open in different directions based on available space
zadjii-msft Feb 22, 2023
4516b4b
Properly account for the position of panes when opening the menu
zadjii-msft Feb 22, 2023
a3c4776
clamp horizontally
zadjii-msft Feb 23, 2023
7988d89
code cleanup
zadjii-msft Feb 23, 2023
84a41b4
cleanup _for review_?
zadjii-msft Feb 23, 2023
36b5759
[PARENT] lmao I deleted the line that sends the inpu :facepalm"
zadjii-msft Feb 24, 2023
f3796da
mostly all the plumbing to get this back into a reasonable state
zadjii-msft Feb 24, 2023
f4c310a
make this actually work again
zadjii-msft Feb 24, 2023
dc71f0c
align to the cursor in bottoms up mode
zadjii-msft Feb 24, 2023
c9e7156
notes, cleanup, order the history correctly
zadjii-msft Feb 24, 2023
5defde5
POC: We can take it out of the Popup and have it still work
zadjii-msft Feb 24, 2023
57a5327
very confident we can get all the logic into the sxnui
zadjii-msft Feb 24, 2023
a172f53
Finish detaching it from the popup
zadjii-msft Mar 1, 2023
e2cc278
POC: We can take it out of the Popup and have it still work
zadjii-msft Feb 24, 2023
e544871
very confident we can get all the logic into the sxnui
zadjii-msft Feb 24, 2023
173a830
Finish detaching it from the popup
zadjii-msft Mar 1, 2023
a21c815
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 1, 2023
20f5651
final cleanup for review
zadjii-msft Mar 1, 2023
236912a
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 2, 2023
442413f
Final cleanup for review
zadjii-msft Mar 2, 2023
9d5ef3a
format and spell
zadjii-msft Mar 2, 2023
cd2db82
Merge branch 'main' into dev/migrie/fhl-2023/pwsh-autocomplete-demo
zadjii-msft Mar 10, 2023
22c94bf
spell
zadjii-msft Mar 10, 2023
28555a6
Merge remote-tracking branch 'origin/main' into dev/migrie/fhl-2023/p…
zadjii-msft Mar 20, 2023
36b676a
Add a setting to opt in, because feature flags are imprecise
zadjii-msft Mar 20, 2023
b096469
austin mode
zadjii-msft Mar 20, 2023
4e46a63
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 20, 2023
6c778a6
yea no I always run the tests
zadjii-msft Mar 20, 2023
172a298
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 21, 2023
dc4a832
substr takes a count, not an end
zadjii-msft Mar 21, 2023
19c1ed9
This fixes a previewing issue, all the way back in the first PR
zadjii-msft Mar 21, 2023
dd5458c
spell
zadjii-msft Mar 21, 2023
df92394
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 21, 2023
11916fd
Merge remote-tracking branch 'origin/main' into dev/migrie/fhl-2023/p…
zadjii-msft Mar 24, 2023
1c52862
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Mar 24, 2023
d593fe1
Add a range version of ROW::GetText
lhecker Mar 23, 2023
f56d258
Woops, wrong method
lhecker Mar 23, 2023
4c2499c
this is so much cleaner
zadjii-msft Mar 27, 2023
057098e
Merge remote-tracking branch 'origin/main' into dev/migrie/fhl-2023/p…
zadjii-msft Apr 5, 2023
62863f1
Merge branch 'dev/migrie/f/14779-sxnui-recent-commands' of https://gi…
zadjii-msft Apr 5, 2023
a530ccc
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Apr 5, 2023
3fc364c
Merge branch 'main' into dev/migrie/fhl-2023/pwsh-autocomplete-demo
zadjii-msft May 31, 2023
fb40b5d
lazy loading for fun and profit
zadjii-msft May 31, 2023
d4422bb
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft May 31, 2023
2cf9f41
Merge branch 'main' into dev/migrie/fhl-2023/pwsh-autocomplete-demo
zadjii-msft Jul 5, 2023
b8d8f11
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Jul 5, 2023
741b773
This needs to go way earlier in the stack of PRs
zadjii-msft Jul 6, 2023
434572a
Merge branch 'main' into dev/migrie/fhl-2023/pwsh-autocomplete-demo
zadjii-msft Aug 1, 2023
12d2160
minor code movement
zadjii-msft Aug 1, 2023
f10f14c
this is better
zadjii-msft Aug 1, 2023
1a8275e
move code around
zadjii-msft Aug 1, 2023
7ae4716
use less int math where possible
zadjii-msft Aug 1, 2023
1720905
last refactoring - Open() instead of Anchor()
zadjii-msft Aug 2, 2023
45a6e1d
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 2, 2023
7c3fd2a
Remove input previewing. We can necro later. It's too hacky
zadjii-msft Aug 2, 2023
3ee1f9b
load bearing I guess
zadjii-msft Aug 2, 2023
ce70a37
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 2, 2023
f9a764e
lots of load bearing code here
zadjii-msft Aug 2, 2023
a013020
Merge remote-tracking branch 'origin/main' into dev/migrie/fhl-2023/p…
zadjii-msft Aug 8, 2023
0cf6475
whole pile of PR nits
zadjii-msft Aug 8, 2023
902ee8e
raise the event relative to the sender
zadjii-msft Aug 8, 2023
8f0b6f8
revert 1449088, d3b5533 and 0bda66f, because we didn't know what they…
zadjii-msft Aug 8, 2023
4de9303
runformat
zadjii-msft Aug 8, 2023
873d8d4
Merge remote-tracking branch 'origin/main' into dev/migrie/fhl-2023/p…
zadjii-msft Aug 8, 2023
a21977b
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 8, 2023
16492a7
fix the build
zadjii-msft Aug 8, 2023
1759213
oneliner mistakes
zadjii-msft Aug 9, 2023
c1339bc
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 9, 2023
ef7f563
this would crash the palette if there were nested items
zadjii-msft Aug 9, 2023
2bceb92
these are also vestigial
zadjii-msft Aug 9, 2023
4110f2c
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 9, 2023
dff4ae3
update my formatter so it stops blowing up
zadjii-msft Aug 9, 2023
3748900
Merge branch 'dev/migrie/fhl-2023/pwsh-autocomplete-demo' into dev/mi…
zadjii-msft Aug 9, 2023
f23c859
Apply suggestions from code review
zadjii-msft Aug 10, 2023
1e91a1c
Merge remote-tracking branch 'origin/main' into dev/migrie/f/14779-sx…
zadjii-msft Aug 14, 2023
f84d139
remove comment
zadjii-msft Aug 14, 2023
ee3a497
minor nits from review
zadjii-msft Aug 15, 2023
9355520
remove this lambda
zadjii-msft Aug 15, 2023
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
28 changes: 28 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,34 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalPage::_HandleSuggestions(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (args)
{
if (const auto& realArgs = args.ActionArgs().try_as<SuggestionsArgs>())
{
auto source = realArgs.Source();

switch (source)
lhecker marked this conversation as resolved.
Show resolved Hide resolved
{
case SuggestionsSource::CommandHistory:
{
if (const auto& control{ _GetActiveControl() })
{
const auto context = control.CommandHistory();
_OpenSuggestions(control,
Command::HistoryToCommands(context.History(), context.CurrentCommandline(), false),
SuggestionsMode::Palette);
}
args.Handled(true);
}
break;
lhecker marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

void TerminalPage::_HandleColorSelection(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
Expand Down
32 changes: 17 additions & 15 deletions src/cascadia/TerminalApp/SuggestionsControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ namespace winrt::TerminalApp::implementation
}
});

// Focusing the ListView when the Command Palette control is set to Visible
// for the first time fails because the ListView hasn't finished loading by
// the time Focus is called. Luckily, We can listen to SizeChanged to know
// when the ListView has been measured out and is ready, and we'll immediately
// revoke the handler because we only needed to handle it once on initialization.
_sizeChangedRevoker = _filteredActionsView().SizeChanged(winrt::auto_revoke, [this](auto /*s*/, auto /*e*/) {
// This does only fire once, when the size changes, which is the
// very first time it's opened. It does not fire for subsequent
// openings.

_sizeChangedRevoker.revoke();
// When we're in BottomUp mode, we need to adjust our own position
// so that our bottom is aligned with our origin. This will ensure
// that as the menu changes in size (as we filter results), the menu
// stays "attached" to the cursor.
if (Visibility() == Visibility::Visible && _direction == TerminalApp::SuggestionsDirection::BottomUp)
{
auto m = this->Margin();
m.Top = (_anchor.Y - ActualHeight());
this->Margin(m);
}
Comment on lines +104 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the reliance on the cached _direction member here will lead to bugs. I think it would be more robust if there was a common function that would recompute the entire layout whenever any of its parameters change.
I mean a size change should lead to potential a call to _setDirection right? If there was less state here and more "recomputations" (English word 404), would this be simpler? As such I think that this code should be abstracted: https://github.com/microsoft/terminal/blob/dev/migrie/f/14779-sxnui-recent-commands/src/cascadia/TerminalApp/SuggestionsControl.cpp#L1088-L1103
...but it might be necessary to move even more logic in there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was hoping that by just starting with the right direction based off the size it opened with, then anything smaller would still make sense in that direction.

I worry that like, trying to rearrange the layout of the control while the size is changing is just gonna trigger another size change. I'm sure there's a correct way to do this with the Arrange/Layout events in WinUI, but that's a dark part of the framework I've been trying my best to avoid 😅

});

_filteredActionsView().SelectionChanged({ this, &SuggestionsControl::_selectedCommandChanged });
Expand Down Expand Up @@ -851,11 +851,13 @@ namespace winrt::TerminalApp::implementation
}
}
}
if (_mode == SuggestionsMode::Palette)
{
// We want to present the commands sorted
std::sort(actions.begin(), actions.end(), FilteredCommand::Compare);
}

// No sorting in palette mode, so results are still filtered, but in the
// original order. This feels more right for something like
// recentCommands.
//
// This is in contrast to the Command Palette, which always sorts its
// actions.

// Adjust the order of the results depending on if we're top-down or
// bottom up. This way, the "first" / "best" match is always closest to
Expand Down
36 changes: 36 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,42 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return hstring{ str };
}

// Get all of our recent commands. This will only really work if the user has enabled shell integration.
Control::CommandHistoryContext ControlCore::CommandHistory() const
{
auto terminalLock = _terminal->LockForWriting();
const auto& textBuffer = _terminal->GetTextBuffer();

std::vector<winrt::hstring> commands;
for (const auto& mark : _terminal->GetScrollMarks())
{
// The command text is between the `end` (which denotes the end of
// the prompt) and the `commandEnd`.
bool markHasCommand = mark.commandEnd.has_value() &&
mark.commandEnd != mark.end;
if (!markHasCommand)
{
continue;
}

// Get the text of the command
const auto line = mark.end.y;
const auto& row = textBuffer.GetRowByOffset(line);
const auto commandText = row.GetText(mark.end.x, mark.commandEnd->x);

// Trim off trailing spaces.
const auto strEnd = commandText.find_last_not_of(UNICODE_SPACE);
if (strEnd != std::string::npos)
{
const auto trimmed = commandText.substr(0, strEnd + 1);
commands.push_back(winrt::hstring{ trimmed });
}
}
auto context = winrt::make_self<CommandHistoryContext>(std::move(commands));

return *context;
}

Core::Scheme ControlCore::ColorScheme() const noexcept
{
Core::Scheme s;
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "ControlCore.g.h"
#include "SelectionColor.g.h"
#include "CommandHistoryContext.g.h"
#include "ControlSettings.h"
#include "../../audio/midi/MidiAudio.hpp"
#include "../../renderer/base/Renderer.hpp"
Expand Down Expand Up @@ -53,6 +54,16 @@ namespace winrt::Microsoft::Terminal::Control::implementation
til::property<til::color> Color;
til::property<bool> IsIndex16;
};
struct CommandHistoryContext : CommandHistoryContextT<CommandHistoryContext>
{
til::property<Windows::Foundation::Collections::IVector<winrt::hstring>> History;
til::property<winrt::hstring> CurrentCommandline;

CommandHistoryContext(std::vector<winrt::hstring>&& history)
{
History(winrt::single_threaded_vector<winrt::hstring>(std::move(history)));
}
};

struct ControlCore : ControlCoreT<ControlCore>
{
Expand Down Expand Up @@ -213,6 +224,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void SetReadOnlyMode(const bool readOnlyState);

hstring ReadEntireBuffer() const;
Control::CommandHistoryContext CommandHistory() const;

static bool IsVintageOpacityAvailable() noexcept;

Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ namespace Microsoft.Terminal.Control
Boolean IsIndex16;
};

[default_interface] runtimeclass CommandHistoryContext
{
IVector<String> History { get; };
String CurrentCommandline { get; };
};

[default_interface] runtimeclass ControlCore : ICoreState
{
ControlCore(IControlSettings settings,
Expand Down Expand Up @@ -136,6 +142,7 @@ namespace Microsoft.Terminal.Control
void EnablePainting();

String ReadEntireBuffer();
CommandHistoryContext CommandHistory();

void AdjustOpacity(Double Opacity, Boolean relative);
void WindowVisibilityChanged(Boolean showOrHide);
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 @@ -3377,6 +3377,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
return _core.ReadEntireBuffer();
}
Control::CommandHistoryContext TermControl::CommandHistory() const
{
return _core.CommandHistory();
}

Core::Scheme TermControl::ColorScheme() const noexcept
{
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
static Windows::UI::Xaml::Thickness ParseThicknessFromPadding(const hstring padding);

hstring ReadEntireBuffer() const;
Control::CommandHistoryContext CommandHistory() const;

winrt::Microsoft::Terminal::Core::Scheme ColorScheme() const noexcept;
void ColorScheme(const winrt::Microsoft::Terminal::Core::Scheme& scheme) const noexcept;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ namespace Microsoft.Terminal.Control
void SetReadOnly(Boolean readOnlyState);

String ReadEntireBuffer();
CommandHistoryContext CommandHistory();

void AdjustOpacity(Double Opacity, Boolean relative);

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 @@ -52,6 +52,7 @@ static constexpr std::string_view SwitchToTabKey{ "switchToTab" };
static constexpr std::string_view TabSearchKey{ "tabSearch" };
static constexpr std::string_view ToggleAlwaysOnTopKey{ "toggleAlwaysOnTop" };
static constexpr std::string_view ToggleCommandPaletteKey{ "commandPalette" };
static constexpr std::string_view SuggestionsKey{ "showSuggestions" };
static constexpr std::string_view ToggleFocusModeKey{ "toggleFocusMode" };
static constexpr std::string_view SetFocusModeKey{ "setFocusMode" };
static constexpr std::string_view ToggleFullscreenKey{ "toggleFullscreen" };
Expand Down Expand Up @@ -386,6 +387,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::TabSearch, RS_(L"TabSearchCommandKey") },
{ ShortcutAction::ToggleAlwaysOnTop, RS_(L"ToggleAlwaysOnTopCommandKey") },
{ ShortcutAction::ToggleCommandPalette, MustGenerate },
{ ShortcutAction::Suggestions, MustGenerate },
{ ShortcutAction::ToggleFocusMode, RS_(L"ToggleFocusModeCommandKey") },
{ ShortcutAction::SetFocusMode, MustGenerate },
{ ShortcutAction::ToggleFullscreen, RS_(L"ToggleFullscreenCommandKey") },
Expand Down
11 changes: 11 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 "AddMarkArgs.g.cpp"
#include "FindMatchArgs.g.cpp"
#include "ToggleCommandPaletteArgs.g.cpp"
#include "SuggestionsArgs.g.cpp"
#include "NewWindowArgs.g.cpp"
#include "PrevTabArgs.g.cpp"
#include "NextTabArgs.g.cpp"
Expand Down Expand Up @@ -706,6 +707,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return RS_(L"ToggleCommandPaletteCommandKey");
}

winrt::hstring SuggestionsArgs::GenerateName() const
{
switch (Source())
{
case SuggestionsSource::CommandHistory:
return RS_(L"SuggestionsCommandHistoryCommandKey");
}
return RS_(L"SuggestionsCommandKey");
}

winrt::hstring FindMatchArgs::GenerateName() const
{
switch (Direction())
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "AddMarkArgs.g.h"
#include "MoveTabArgs.g.h"
#include "ToggleCommandPaletteArgs.g.h"
#include "SuggestionsArgs.g.h"
#include "FindMatchArgs.g.h"
#include "NewWindowArgs.g.h"
#include "PrevTabArgs.g.h"
Expand Down Expand Up @@ -213,6 +214,10 @@ private: \
#define TOGGLE_COMMAND_PALETTE_ARGS(X) \
X(CommandPaletteLaunchMode, LaunchMode, "launchMode", false, CommandPaletteLaunchMode::Action)

////////////////////////////////////////////////////////////////////////////////
#define SUGGESTIONS_ARGS(X) \
X(SuggestionsSource, Source, "source", false, SuggestionsSource::Tasks)

////////////////////////////////////////////////////////////////////////////////
#define FIND_MATCH_ARGS(X) \
X(FindMatchDirection, Direction, "direction", args->Direction() == FindMatchDirection::None, FindMatchDirection::None)
Expand Down Expand Up @@ -709,6 +714,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

ACTION_ARGS_STRUCT(ToggleCommandPaletteArgs, TOGGLE_COMMAND_PALETTE_ARGS);

ACTION_ARGS_STRUCT(SuggestionsArgs, SUGGESTIONS_ARGS);

ACTION_ARGS_STRUCT(FindMatchArgs, FIND_MATCH_ARGS);

ACTION_ARGS_STRUCT(PrevTabArgs, PREV_TAB_ARGS);
Expand Down Expand Up @@ -836,6 +843,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
BASIC_FACTORY(ClearBufferArgs);
BASIC_FACTORY(MultipleActionsArgs);
BASIC_FACTORY(AdjustOpacityArgs);
BASIC_FACTORY(SuggestionsArgs);
BASIC_FACTORY(SelectCommandArgs);
BASIC_FACTORY(SelectOutputArgs);
}
13 changes: 13 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ namespace Microsoft.Terminal.Settings.Model
ToCurrent,
ToMouse,
};
enum SuggestionsSource
{
Tasks,
CommandHistory,
DirectoryHistory,
};

[default_interface] runtimeclass NewTerminalArgs {
NewTerminalArgs();
Expand Down Expand Up @@ -318,6 +324,13 @@ namespace Microsoft.Terminal.Settings.Model
CommandPaletteLaunchMode LaunchMode { get; };
};

[default_interface] runtimeclass SuggestionsArgs : IActionArgs
{
SuggestionsArgs();
SuggestionsArgs(SuggestionsSource source);
SuggestionsSource Source { get; };
};

[default_interface] runtimeclass FindMatchArgs : IActionArgs
{
FindMatchArgs(FindMatchDirection direction);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
ON_ALL_ACTIONS(MarkMode) \
ON_ALL_ACTIONS(ToggleBlockSelection) \
ON_ALL_ACTIONS(SwitchSelectionEndpoint) \
ON_ALL_ACTIONS(Suggestions) \
ON_ALL_ACTIONS(ColorSelection) \
ON_ALL_ACTIONS(ShowContextMenu) \
ON_ALL_ACTIONS(ExpandSelectionToWord) \
Expand Down Expand Up @@ -150,6 +151,7 @@
ON_ALL_ACTIONS_WITH_ARGS(ClearBuffer) \
ON_ALL_ACTIONS_WITH_ARGS(MultipleActions) \
ON_ALL_ACTIONS_WITH_ARGS(AdjustOpacity) \
ON_ALL_ACTIONS_WITH_ARGS(Suggestions) \
ON_ALL_ACTIONS_WITH_ARGS(SelectCommand) \
ON_ALL_ACTIONS_WITH_ARGS(SelectOutput) \
ON_ALL_ACTIONS_WITH_ARGS(ColorSelection)
49 changes: 49 additions & 0 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,53 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

return winrt::single_threaded_vector<Model::Command>(std::move(result));
}

// Method description:
// * Convert the list of recent commands into a list of sendInput actions to
// send those commands.
// * We'll give each command a "history" icon.
// * If directories is true, we'll prepend "cd " to each command, so that
// the command will be run as a directory change instead.
IVector<Model::Command> Command::HistoryToCommands(IVector<winrt::hstring> history,
winrt::hstring /*currentCommandline*/,
bool directories)
{
std::wstring cdText = directories ? L"cd " : L"";
auto result = std::vector<Model::Command>();

// Use this map to discard duplicates.
std::unordered_map<std::wstring_view, bool> foundCommands{};

auto backspaces = std::wstring(::base::saturated_cast<size_t>(0), L'\x7f');
lhecker marked this conversation as resolved.
Show resolved Hide resolved

// Iterate in reverse over the history, so that most recent commands are first
for (auto i = history.Size(); i > 0; i--)
{
std::wstring_view line{ history.GetAt(i - 1) };

if (line.empty())
{
continue;
}
if (foundCommands.contains(line))
{
continue;
}
auto args = winrt::make_self<SendInputArgs>(
winrt::hstring{ fmt::format(L"{}{}{}", cdText, backspaces, line) });

Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args };

auto command = winrt::make_self<Command>();
command->_ActionAndArgs = actionAndArgs;
command->_name = winrt::hstring{ line };
command->_iconPath = directories ?
L"\ue8da" : // OpenLocal (a folder with an arrow pointing up)
L"\ue81c"; // History icon
result.push_back(*command);
foundCommands[line] = true;
}

return winrt::single_threaded_vector<Model::Command>(std::move(result));
}
}
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void IconPath(const hstring& val);

static Windows::Foundation::Collections::IVector<Model::Command> ParsePowerShellMenuComplete(winrt::hstring json, int32_t replaceLength);
static Windows::Foundation::Collections::IVector<Model::Command> HistoryToCommands(Windows::Foundation::Collections::IVector<winrt::hstring> history,
winrt::hstring currentCommandline,
bool directories);

WINRT_PROPERTY(ExpandCommandType, IterateOn, ExpandCommandType::None);
WINRT_PROPERTY(Model::ActionAndArgs, ActionAndArgs);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ namespace Microsoft.Terminal.Settings.Model
Windows.Foundation.Collections.IMapView<String, Command> NestedCommands { get; };

static IVector<Command> ParsePowerShellMenuComplete(String json, Int32 replaceLength);
static IVector<Command> HistoryToCommands(IVector<String> commandHistory, String commandline, Boolean directories);

}
}
Loading
Loading