Skip to content

Commit

Permalink
Automatic parsing of .wt.json files in the CWD, for additional actions
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Sep 19, 2022
1 parent 1e357d0 commit 4def21a
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 35 deletions.
22 changes: 21 additions & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,20 @@ namespace winrt::TerminalApp::implementation
case TaskSource::Prompt:
{
auto commandsCollection = _settings.GlobalSettings().ActionMap().FilterToSendInput();
if (const auto& control{ _GetActiveControl() })
{
const auto context = control.DirectoryHistory();
auto cwd = context.CurrentWorkingDirectory();
if (!cwd.empty())
{
auto localTasks = CascadiaSettings::ReadFile(cwd + L"\\.wt.json");
if (!localTasks.empty())
{
Command::AddLocalCommands(commandsCollection, localTasks);
}
}
}

_openTaskView(commandsCollection);
args.Handled(true);
}
Expand All @@ -1205,14 +1219,20 @@ namespace winrt::TerminalApp::implementation
{
if (const auto& control{ _GetActiveControl() })
{
const auto context = control.ReadPromptLines();
const auto context = control.CommandHistory();
_openTaskView(Command::HistoryToCommands(context.History(), context.CurrentCommandline(), false));
}
args.Handled(true);
}
break;
case TaskSource::DirectoryHistory:
{
if (const auto& control{ _GetActiveControl() })
{
const auto context = control.DirectoryHistory();
_openTaskView(Command::HistoryToCommands(context.History(), L"", true));
}
args.Handled(true);
}
break;

Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/CommandPalette.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@
Title=""
IsOpen="False"
PreferredPlacement="Right"
Subtitle="">
Subtitle=""
Translation="0,0,-100">
<!-- <muxc:TeachingTip.IconSource>
<muxc:SymbolIconSource Symbol="Refresh" />
</muxc:TeachingTip.IconSource>-->
Expand Down
11 changes: 10 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ControlCore.g.cpp"
#include "SelectionColor.g.cpp"
#include "CommandHistoryContext.g.cpp"
#include "DirectoryHistoryContext.g.cpp"

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

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

Expand Down Expand Up @@ -1852,6 +1853,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return *context;
}

Control::DirectoryHistoryContext ControlCore::DirectoryHistory() const
{
auto terminalLock = _terminal->LockForWriting();
auto context = winrt::make_self<DirectoryHistoryContext>();
context->CurrentWorkingDirectory(WorkingDirectory());
return *context;
}

// Helper to check if we're on Windows 11 or not. This is used to check if
// we need to use acrylic to achieve transparency, because vintage opacity
// doesn't work in islands on win10.
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 @@ -18,6 +18,7 @@
#include "ControlCore.g.h"
#include "SelectionColor.g.h"
#include "CommandHistoryContext.g.h"
#include "DirectoryHistoryContext.g.h"

#include "ControlSettings.h"
#include "../../audio/midi/MidiAudio.hpp"
Expand Down Expand Up @@ -57,6 +58,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
WINRT_PROPERTY(Windows::Foundation::Collections::IVector<winrt::hstring>, History, winrt::single_threaded_vector<winrt::hstring>());
WINRT_PROPERTY(winrt::hstring, CurrentCommandline);
};
struct DirectoryHistoryContext : DirectoryHistoryContextT<DirectoryHistoryContext>
{
WINRT_PROPERTY(Windows::Foundation::Collections::IVector<winrt::hstring>, History, winrt::single_threaded_vector<winrt::hstring>());
WINRT_PROPERTY(winrt::hstring, CurrentWorkingDirectory);
WINRT_PROPERTY(bool, SetByClient);
};

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

hstring ReadEntireBuffer() const;
Control::CommandHistoryContext ReadPromptLines() const;
Control::CommandHistoryContext CommandHistory() const;
Control::DirectoryHistoryContext DirectoryHistory() const;

static bool IsVintageOpacityAvailable() noexcept;

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

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

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

String ReadEntireBuffer();
CommandHistoryContext ReadPromptLines();

void AdjustOpacity(Double Opacity, Boolean relative);
void WindowVisibilityChanged(Boolean showOrHide);
Expand Down
18 changes: 14 additions & 4 deletions src/cascadia/TerminalControl/ICoreState.idl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ namespace Microsoft.Terminal.Control
Last
};

struct MenuEntry

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

[default_interface] runtimeclass DirectoryHistoryContext
{
IVector<String> History;
String CurrentWorkingDirectory;
Boolean SetByClient;
};

// These are properties of the TerminalCore that should be queryable by the
Expand All @@ -46,6 +53,9 @@ namespace Microsoft.Terminal.Control

String WorkingDirectory { get; };

CommandHistoryContext CommandHistory { get; };
DirectoryHistoryContext DirectoryHistory { get; };

Windows.Foundation.IReference<Windows.UI.Color> TabColor { get; };

Int32 ScrollOffset { get; };
Expand Down
9 changes: 7 additions & 2 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3036,9 +3036,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return _core.ReadEntireBuffer();
}

Control::CommandHistoryContext TermControl::ReadPromptLines() const
Control::CommandHistoryContext TermControl::CommandHistory() const
{
return _core.ReadPromptLines();
return _core.CommandHistory();
}

Control::DirectoryHistoryContext TermControl::DirectoryHistory() const
{
return _core.DirectoryHistory();
}

Core::Scheme TermControl::ColorScheme() const noexcept
Expand Down
5 changes: 2 additions & 3 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void ClearAllMarks();
void ScrollToMark(const Control::ScrollToMarkDirection& direction);

Windows::Foundation::Collections::IVector<Control::MenuEntry> MenuEntries() const;

#pragma endregion

void ScrollViewport(int viewTop);
Expand Down Expand Up @@ -131,7 +129,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
static Windows::UI::Xaml::Thickness ParseThicknessFromPadding(const hstring padding);

hstring ReadEntireBuffer() const;
Control::CommandHistoryContext ReadPromptLines() const;
Control::CommandHistoryContext CommandHistory() const;
Control::DirectoryHistoryContext DirectoryHistory() const;

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

String ReadEntireBuffer();
CommandHistoryContext ReadPromptLines();

void AdjustOpacity(Double Opacity, Boolean relative);

Expand Down
15 changes: 15 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,21 @@ void CascadiaSettings::ExportFile(winrt::hstring path, winrt::hstring content)
CATCH_LOG();
}

winrt::hstring CascadiaSettings::ReadFile(winrt::hstring path)
{
try
{
auto maybeContents = ReadUTF8FileIfExists({ path.c_str() });
if (maybeContents.has_value())
{
return winrt::hstring{ til::u8u16(*maybeContents) };
}
}
CATCH_LOG();

return L"";
}

void CascadiaSettings::_validateThemeExists()
{
if (_globals->Themes().Size() == 0)
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static winrt::hstring ApplicationDisplayName();
static winrt::hstring ApplicationVersion();
static void ExportFile(winrt::hstring path, winrt::hstring content);
static winrt::hstring ReadFile(winrt::hstring path);

CascadiaSettings() noexcept = default;
CascadiaSettings(const winrt::hstring& userJSON, const winrt::hstring& inboxJSON);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.Terminal.Settings.Model
static String ApplicationVersion { get; };

static void ExportFile(String path, String content);
static String ReadFile(String path);

CascadiaSettings(String userJSON, String inboxJSON);

Expand Down
39 changes: 25 additions & 14 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,27 +704,38 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
command.Name(winrt::hstring{ line });
result.Append(command);
foundCommands[line] = true;
// 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)
// {
// 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)
{
createAction(command);
}

return result;
}

void Command::AddLocalCommands(Windows::Foundation::Collections::IVector<Model::Command> commands,
winrt::hstring localTasksFileContents)
{
auto data = winrt::to_string(localTasksFileContents);
std::string errs;
static std::unique_ptr<Json::CharReader> reader{ Json::CharReaderBuilder::CharReaderBuilder().newCharReader() };
Json::Value root;
if (!reader->parse(data.data(), data.data() + data.size(), &root, &errs))
{
throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
}
if (auto actions{ root[JsonKey("actions")] })
{
std::vector<SettingsLoadWarnings> warnings;
for (const auto& json : actions)
{
auto parsed = Command::FromJson(json, warnings);
if (parsed->ActionAndArgs().Action() != ShortcutAction::SendInput)
continue;
commands.Append(*parsed);
}
}
}

}
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

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);
static void AddLocalCommands(Windows::Foundation::Collections::IVector<Model::Command>, winrt::hstring localTasksFileContents);

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

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 @@ -51,5 +51,7 @@ namespace Microsoft.Terminal.Settings.Model

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

}
}

0 comments on commit 4def21a

Please sign in to comment.