Skip to content

Commit f0267d4

Browse files
Patrick Guclaude
andcommitted
feat: support multiline patterns in switch rule conditions
- HostWildcardCondition, UrlWildcardCondition, HostRegexCondition, UrlRegexCondition now accept newline-separated patterns in addition to pipe-separated ones - Replace pattern input with expandable textarea (1 row collapsed, 10 rows expanded) with toggle button - Single-line mode blocks Enter key and auto-expands on click when multiline content exists - validateCondition validates each line independently for regex types Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent af79d9e commit f0267d4

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bower_components
33
package-lock.json
44
*-release.zip
55
*.swp
6+
dist/*

omega-pac/src/conditions.coffee

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ module.exports = exports =
247247

248248
'UrlRegexCondition':
249249
abbrs: ['UR', 'URegex', 'UrlR', 'UrlRegex']
250-
analyze: (condition) -> @safeRegex escapeSlash condition.pattern
250+
analyze: (condition) ->
251+
raw = condition.pattern.split(/[|\n]/)
252+
parts = for p in raw when p.trim()
253+
escapeSlash p.trim()
254+
@safeRegex parts.join('|')
251255
match: (condition, request, cache) ->
252256
return cache.analyzed.test(request.url)
253257
compile: (condition, cache) ->
@@ -257,8 +261,9 @@ module.exports = exports =
257261
abbrs: ['U', 'UW', 'Url', 'UrlW', 'UWild', 'UWildcard', 'UrlWild',
258262
'UrlWildcard']
259263
analyze: (condition) ->
260-
parts = for pattern in condition.pattern.split('|') when pattern
261-
shExp2RegExp pattern, trimAsterisk: true
264+
raw = condition.pattern.split(/[|\n]/)
265+
parts = for pattern in raw when pattern.trim()
266+
shExp2RegExp pattern.trim(), trimAsterisk: true
262267
@safeRegex parts.join('|')
263268
match: (condition, request, cache) ->
264269
return cache.analyzed.test(request.url)
@@ -267,7 +272,11 @@ module.exports = exports =
267272

268273
'HostRegexCondition':
269274
abbrs: ['R', 'HR', 'Regex', 'HostR', 'HRegex', 'HostRegex']
270-
analyze: (condition) -> @safeRegex escapeSlash condition.pattern
275+
analyze: (condition) ->
276+
raw = condition.pattern.split(/[|\n]/)
277+
parts = for p in raw when p.trim()
278+
escapeSlash p.trim()
279+
@safeRegex parts.join('|')
271280
match: (condition, request, cache) ->
272281
return cache.analyzed.test(request.host)
273282
compile: (condition, cache) ->
@@ -277,7 +286,9 @@ module.exports = exports =
277286
abbrs: ['', 'H', 'W', 'HW', 'Wild', 'Wildcard', 'Host', 'HostW', 'HWild',
278287
'HWildcard', 'HostWild', 'HostWildcard']
279288
analyze: (condition) ->
280-
parts = for pattern in condition.pattern.split('|') when pattern
289+
raw = condition.pattern.split(/[|\n]/)
290+
parts = for pattern in raw when pattern.trim()
291+
pattern = pattern.trim()
281292
# Get the magical regex of this pattern. See
282293
# https://github.com/FelisCatus/SwitchyOmega/wiki/Host-wildcard-condition
283294
# for the magic.
@@ -684,4 +695,5 @@ module.exports = exports =
684695
condition.startHour = 0 unless 0 <= condition.startHour < 24
685696
condition.endHour = 0 unless 0 <= condition.endHour < 24
686697
condition
698+
687699
# coffeelint: enable=missing_fat_arrows

omega-web/src/omega/controllers/switch_profile.coffee

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,16 @@ angular.module('omega').controller 'SwitchProfileCtrl', ($scope, $rootScope,
192192
rule.condition.pattern = ''
193193
$scope.profile.rules.push rule
194194

195+
$scope.isMultiline = (pattern) ->
196+
pattern and pattern.indexOf('\n') >= 0
197+
195198
$scope.validateCondition = (condition, pattern) ->
196199
if condition.conditionType.indexOf('Regex') >= 0
197-
try
198-
new RegExp(pattern)
199-
catch _
200-
return false
200+
for line in pattern.split(/[|\n]/) when line.trim()
201+
try
202+
new RegExp(line.trim())
203+
catch _
204+
return false
201205
return true
202206

203207
$scope.conditionHasWarning = (condition) ->

omega-web/src/partials/profile_switch.jade

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,16 @@ div(ng-controller='SwitchProfileCtrl')
9494
label.checkbox-inline(ng-repeat='selected in getWeekdayList(rule.condition) track by $index')
9595
input(type='checkbox' ng-model='selected' ng-change='updateDay(rule.condition, $index, selected)')
9696
= '{{"options_weekDayShort_" + $index | tr}} '
97-
input.form-control(ng-model='rule.condition.pattern' ng-switch-default required
98-
ui-validate='{pattern: "validateCondition(rule.condition, $value)"}')
97+
span.input-group(ng-switch-default)
98+
textarea.form-control.monospace(ng-model='rule.condition.pattern' required
99+
rows='{{rule._expanded ? 10 : 1}}'
100+
ng-click='!rule._expanded && isMultiline(rule.condition.pattern) && (rule._expanded = true)'
101+
ng-keydown='!rule._expanded && $event.keyCode === 13 && $event.preventDefault()'
102+
ng-style='{cursor: !rule._expanded && isMultiline(rule.condition.pattern) ? "pointer" : "auto", resize: "none", overflow: rule._expanded ? "auto" : "hidden", height: rule._expanded ? "auto" : "34px"}'
103+
ui-validate='{pattern: "validateCondition(rule.condition, $value)"}')
104+
span.input-group-btn
105+
button.btn.btn-default(type='button' ng-click='rule._expanded = !rule._expanded')
106+
span.glyphicon(ng-class='rule._expanded ? "glyphicon-chevron-up" : "glyphicon-chevron-down"')
99107
td.switch-rule-row-target
100108
div(omega-profile-select='options | profiles:profile' ng-model='rule.profileName'
101109
disp-name='dispNameFilter' options='options'

0 commit comments

Comments
 (0)