-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathMatchProfilesEntry.cpp
104 lines (84 loc) · 3.51 KB
/
MatchProfilesEntry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "MatchProfilesEntry.h"
#include "JsonUtils.h"
#include "MatchProfilesEntry.g.cpp"
using namespace Microsoft::Terminal::Settings::Model;
static constexpr std::string_view NameKey{ "name" };
static constexpr std::string_view CommandlineKey{ "commandline" };
static constexpr std::string_view SourceKey{ "source" };
namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
MatchProfilesEntry::MatchProfilesEntry() noexcept :
MatchProfilesEntryT<MatchProfilesEntry, ProfileCollectionEntry>(NewTabMenuEntryType::MatchProfiles)
{
}
Json::Value MatchProfilesEntry::ToJson() const
{
auto json = NewTabMenuEntry::ToJson();
JsonUtils::SetValueForKey(json, NameKey, _Name);
JsonUtils::SetValueForKey(json, CommandlineKey, _Commandline);
JsonUtils::SetValueForKey(json, SourceKey, _Source);
return json;
}
winrt::com_ptr<NewTabMenuEntry> MatchProfilesEntry::FromJson(const Json::Value& json)
{
auto entry = winrt::make_self<MatchProfilesEntry>();
JsonUtils::GetValueForKey(json, NameKey, entry->_Name);
JsonUtils::GetValueForKey(json, CommandlineKey, entry->_Commandline);
JsonUtils::GetValueForKey(json, SourceKey, entry->_Source);
return entry;
}
bool MatchProfilesEntry::MatchesProfile(const Model::Profile& profile)
{
// We use an optional here instead of a simple bool directly, since there is no
// sensible default value for the desired semantics: the first property we want
// to match on should always be applied (so one would set "true" as a default),
// but if none of the properties are set, the default return value should be false
// since this entry type is expected to behave like a positive match/whitelist.
//
// The easiest way to deal with this neatly is to use an optional, then for any
// property to match we consider a null value to be "true", and for the return
// value of the function we consider the null value to be "false".
auto isMatching = std::optional<bool>{};
auto isMatch = [](std::wstring_view regex, std::wstring_view text) {
if (text.empty())
{
return false;
}
std::wregex re;
try
{
re = { regex.cbegin(), regex.cend() };
}
catch (std::regex_error)
{
// invalid regex
return false;
}
return std::regex_match(text.cbegin(), text.cend(), re);
};
if (!_Name.empty())
{
isMatching = { isMatching.value_or(true) && isMatch(_Name, profile.Name()) };
}
if (!_Source.empty())
{
isMatching = { isMatching.value_or(true) && isMatch(_Source, profile.Source()) };
}
if (!_Commandline.empty())
{
isMatching = { isMatching.value_or(true) && isMatch(_Commandline, profile.Commandline()) };
}
return isMatching.value_or(false);
}
Model::NewTabMenuEntry MatchProfilesEntry::Copy() const
{
auto entry = winrt::make_self<MatchProfilesEntry>();
entry->_Name = _Name;
entry->_Commandline = _Commandline;
entry->_Source = _Source;
return *entry;
}
}