Automatic parsing of .wt.json files in the CWD, for additional actions

This commit is contained in:
Mike Griese
2022-09-19 14:20:32 -05:00
parent 1e357d0694
commit 4def21a018
15 changed files with 110 additions and 35 deletions

View File

@@ -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);
}
@@ -1205,7 +1219,7 @@ 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);
@@ -1213,6 +1227,12 @@ namespace winrt::TerminalApp::implementation
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;

View File

@@ -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>-->

View File

@@ -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;
@@ -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();
@@ -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.

View File

@@ -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"
@@ -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>
{
@@ -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;

View File

@@ -49,12 +49,6 @@ namespace Microsoft.Terminal.Control
Boolean EndAtRightBoundary;
};
[default_interface] runtimeclass CommandHistoryContext
{
IVector<String> History;
String CurrentCommandline;
};
[default_interface] runtimeclass SelectionColor
{
SelectionColor();
@@ -139,7 +133,6 @@ namespace Microsoft.Terminal.Control
void EnablePainting();
String ReadEntireBuffer();
CommandHistoryContext ReadPromptLines();
void AdjustOpacity(Double Opacity, Boolean relative);
void WindowVisibilityChanged(Boolean showOrHide);

View File

@@ -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
@@ -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; };

View File

@@ -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

View File

@@ -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);
@@ -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;

View File

@@ -84,7 +84,6 @@ namespace Microsoft.Terminal.Control
void ToggleReadOnly();
String ReadEntireBuffer();
CommandHistoryContext ReadPromptLines();
void AdjustOpacity(Double Opacity, Boolean relative);

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -704,22 +704,8 @@ 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);
@@ -727,4 +713,29 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
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);
}
}
}
}

View File

@@ -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;

View File

@@ -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);
}
}