Skip to content

Commit

Permalink
Now with a more sensible struct for passing around data
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Sep 19, 2022
1 parent f474c4b commit 1e357d0
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,8 @@ namespace winrt::TerminalApp::implementation
{
if (const auto& control{ _GetActiveControl() })
{
const auto buffer = control.ReadPromptLines();
_openTaskView(Command::HistoryToCommands(buffer, false));
const auto context = control.ReadPromptLines();
_openTaskView(Command::HistoryToCommands(context.History(), context.CurrentCommandline(), false));
}
args.Handled(true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4341,7 +4341,7 @@ namespace winrt::TerminalApp::implementation
return;
}

const auto buffer = control.ReadPromptLines();
const auto buffer = control.ReadEntireBuffer();
buffer;

SuggestionResults().Text(buffer);
Expand Down
47 changes: 27 additions & 20 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ControlCore.g.cpp"
#include "SelectionColor.g.cpp"
#include "CommandHistoryContext.g.cpp"

using namespace ::Microsoft::Console::Types;
using namespace ::Microsoft::Console::VirtualTerminal;
Expand Down Expand Up @@ -1797,52 +1798,58 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return hstring(ss.str());
}

hstring ControlCore::ReadPromptLines() const
Control::CommandHistoryContext ControlCore::ReadPromptLines() const
{
auto terminalLock = _terminal->LockForWriting();

auto context = winrt::make_self<CommandHistoryContext>();

const auto& textBuffer = _terminal->GetTextBuffer();

std::wstringstream ss;
// std::wstringstream ss;

auto addRowText = [&ss](auto&& row, std::wstring& rowText) {
auto addRowText = [&context](auto&& row, std::wstring& rowText) {
const auto strEnd = rowText.find_last_not_of(UNICODE_SPACE);
if (strEnd != std::string::npos)
{
rowText.erase(strEnd + 1);
ss << rowText;
context->History().Append(winrt::hstring{ rowText });
}

auto wrapped = row.WasWrapForced();
if (!wrapped)
{
ss << UNICODE_CARRIAGERETURN << UNICODE_LINEFEED;
}
// if (!wrapped)
// {
// ss << UNICODE_CARRIAGERETURN << UNICODE_LINEFEED;
// }
return wrapped;
};

// const auto lastRow = textBuffer.GetLastNonSpaceCharacter().Y;
for (const auto& mark : _terminal->GetScrollMarks())
{
const bool markHasCommand = mark.start != mark.end;
bool markHasCommand = mark.start != mark.end;
if (!markHasCommand)
continue;

auto line = markHasCommand ? mark.end.y : mark.start.y;

const auto& row = textBuffer.GetRowByOffset(line);

auto rowText = row.GetText(markHasCommand ? mark.end.x : 0);
auto wrapped = addRowText(row, rowText);

while (wrapped)
{
line++;
const auto& newRow = textBuffer.GetRowByOffset(line);
auto newRowText = newRow.GetText(0);
wrapped = addRowText(newRow, newRowText);
}
}

return hstring(ss.str());
wrapped;
// TODO! dea with wrapped commandlines.
line;
// while (wrapped)
// {
// line++;
// const auto& newRow = textBuffer.GetRowByOffset(line);
// auto newRowText = newRow.GetText(0);
// wrapped = addRowText(newRow, newRowText);
// }
}

return *context;
}

// Helper to check if we're on Windows 11 or not. This is used to check if
Expand Down
10 changes: 9 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#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 @@ -50,6 +52,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
WINRT_PROPERTY(bool, IsIndex16);
};

struct CommandHistoryContext : CommandHistoryContextT<CommandHistoryContext>
{
WINRT_PROPERTY(Windows::Foundation::Collections::IVector<winrt::hstring>, History, winrt::single_threaded_vector<winrt::hstring>());
WINRT_PROPERTY(winrt::hstring, CurrentCommandline);
};

struct ControlCore : ControlCoreT<ControlCore>
{
public:
Expand Down Expand Up @@ -195,7 +203,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void ToggleReadOnlyMode();

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

static bool IsVintageOpacityAvailable() noexcept;

Expand Down
8 changes: 7 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ namespace Microsoft.Terminal.Control
Boolean EndAtRightBoundary;
};

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

[default_interface] runtimeclass SelectionColor
{
SelectionColor();
Expand Down Expand Up @@ -133,7 +139,7 @@ namespace Microsoft.Terminal.Control
void EnablePainting();

String ReadEntireBuffer();
String ReadPromptLines();
CommandHistoryContext ReadPromptLines();

void AdjustOpacity(Double Opacity, Boolean relative);
void WindowVisibilityChanged(Boolean showOrHide);
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
return _core.ReadEntireBuffer();
}
hstring TermControl::ReadPromptLines() const

Control::CommandHistoryContext TermControl::ReadPromptLines() const
{
return _core.ReadPromptLines();
}
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
static Windows::UI::Xaml::Thickness ParseThicknessFromPadding(const hstring padding);

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

winrt::Microsoft::Terminal::Core::Scheme ColorScheme() const noexcept;
void ColorScheme(const winrt::Microsoft::Terminal::Core::Scheme& scheme) const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Microsoft.Terminal.Control
void ToggleReadOnly();

String ReadEntireBuffer();
String ReadPromptLines();
CommandHistoryContext ReadPromptLines();

void AdjustOpacity(Double Opacity, Boolean relative);

Expand Down
30 changes: 18 additions & 12 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return result;
}

winrt::Windows::Foundation::Collections::IVector<Model::Command> Command::HistoryToCommands(winrt::hstring history, bool directories)
winrt::Windows::Foundation::Collections::IVector<Model::Command> Command::HistoryToCommands(Windows::Foundation::Collections::IVector<winrt::hstring> history,
winrt::hstring /*currentCommandline*/,
bool directories)
{
std::wstring cdText = directories ? L"cd " : L"";
auto result = winrt::single_threaded_vector<Model::Command>();
Expand Down Expand Up @@ -705,19 +707,23 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// foundCommands.insert(line, true);
};

std::wstring lineBreak = L"\r\n";

std::wstring_view historyView{ history };
size_t start = 0u;
auto end = historyView.find(lineBreak);
while (end != std::string::npos)
// std::wstring lineBreak = L"\r\n";

// std::wstring_view historyView{ history };
// size_t start = 0u;
// auto end = historyView.find(lineBreak);
// while (end != std::string::npos)
// {
// auto line = historyView.substr(start, end - start);
// createAction(line);
// start = end + lineBreak.length();
// end = historyView.find(lineBreak, start);
// }
// createAction(historyView.substr(start, end));
for (const auto&& command : history)
{
auto line = historyView.substr(start, end - start);
createAction(line);
start = end + lineBreak.length();
end = historyView.find(lineBreak, start);
createAction(command);
}
createAction(historyView.substr(start, end));

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ 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(winrt::hstring history, bool directories);
static Windows::Foundation::Collections::IVector<Model::Command> HistoryToCommands(Windows::Foundation::Collections::IVector<winrt::hstring> history, winrt::hstring currentCommandline, bool directories);

winrt::Windows::UI::Xaml::Data::INotifyPropertyChanged::PropertyChanged_revoker propertyChangedRevoker;

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ namespace Microsoft.Terminal.Settings.Model
Windows.Foundation.Collections.IVector<SettingsLoadWarnings> warnings);

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

0 comments on commit 1e357d0

Please sign in to comment.