File tree 1 file changed +4
-4
lines changed
Sources/D2Commands/music/piano
1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change 1
1
import Utils
2
2
import MusicTheory
3
3
4
- fileprivate let argsPattern = try ! LegacyRegex ( from : " ( ?:(\\ w+) \\ s+)?( \\ w+[b#]?) " )
4
+ fileprivate let argsPattern = #/( ?:(?<scale>\ w+)\s+)?(?<key>\ w+[b#]?)/#
5
5
fileprivate let scales : [ String : ( Note ) -> Scale ] = [
6
6
" major " : MajorScale . init,
7
7
" minor " : MinorScale . init,
@@ -29,13 +29,13 @@ public class PianoScaleCommand: StringCommand {
29
29
30
30
public func invoke( with input: String , output: any CommandOutput , context: CommandContext ) {
31
31
do {
32
- guard let parsedArgs = argsPattern. firstGroups ( in: input) else {
32
+ guard let parsedArgs = try ? argsPattern. firstMatch ( in: input) else {
33
33
output. append ( errorText: info. helpText!)
34
34
return
35
35
}
36
36
37
- let rawScale = parsedArgs [ 1 ] . nilIfEmpty ?? defaultScale
38
- let rawKey = parsedArgs [ 2 ] . nilIfEmpty ?? " C3 "
37
+ let rawScale = parsedArgs. scale . map { String ( $0 ) } ?? defaultScale
38
+ let rawKey = String ( parsedArgs. key )
39
39
40
40
guard let scale = scales [ rawScale] else {
41
41
output. append ( errorText: " Unknown scale ` \( rawScale) `. Try one of these: \( scales. keys. map { " ` \( $0) ` " } . joined ( separator: " , " ) ) " )
You can’t perform that action at this time.
0 commit comments