Skip to content

Commit

Permalink
refactor: fix code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Jan 3, 2021
1 parent 1ce7da6 commit 2028c02
Show file tree
Hide file tree
Showing 33 changed files with 604 additions and 472 deletions.
52 changes: 42 additions & 10 deletions Sources/SendKeysLib/Commands/Command.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation

public enum CommandType {
case undefined
case keyPress
case keyDown
case keyUp
Expand All @@ -11,20 +14,49 @@ public enum CommandType {
case continuation
}

public struct Command: Equatable {
let type: CommandType
let arguments: [String?]
public protocol CommandProtocol {
static var commandType: CommandType { get }
static var expression: NSRegularExpression { get }

init(arguments: [String?])
func execute() throws
func equals(_ comparison: Command) -> Bool
}

public init(_ type: CommandType, _ arguments: [String?]) {
self.type = type
self.arguments = arguments
public class Command: Equatable, CustomStringConvertible {
public class var commandType: CommandType { return .undefined }

private static let _expression = try! NSRegularExpression(pattern: ".")
public class var expression: NSRegularExpression { return _expression }

init() {}

required public init(arguments: [String?]) {
}

public init(_ type: CommandType) {
self.init(type, [])
public func execute() throws {
}


public func equals(_ comparison: Command) -> Bool {
return type(of: self) == type(of: comparison)
}

public static func == (lhs: Command, rhs: Command) -> Bool {
return lhs.type == rhs.type && lhs.arguments == rhs.arguments;
return lhs.equals(rhs) && rhs.equals(lhs)
}

public var description: String {
let output = "\(type(of: self)): \(type(of: self).commandType)"
let members = describeMembers()

if !members.isEmpty {
return "\(output) (\(members))"
}

return output
}

func describeMembers() -> String {
return ""
}
}
116 changes: 1 addition & 115 deletions Sources/SendKeysLib/Commands/CommandExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,121 +5,7 @@ public protocol CommandExecutorProtocol {
}

public class CommandExecutor: CommandExecutorProtocol {
private let keyPresser = KeyPresser()
private let mouseController = MouseController()

public func execute(_ command: Command) {
switch command.type {
case .keyPress, .keyDown, .keyUp:
executeKeyPress(command)
case .pause, .stickyPause:
executePause(command)
case .mouseMove:
executeMouseMove(command)
case .mouseClick:
executeMouseClick(command)
case .mouseDrag:
executeMouseDrag(command)
case .mouseScroll:
executeMouseScroll(command)
case .continuation:
return
}
}

private func executeKeyPress(_ command: Command) {
var modifiers: [String] = []

if command.arguments.count > 1 {
modifiers = command.arguments[1]!.components(separatedBy: ",")
}

switch command.type {
case .keyPress:
try! keyPresser.keyPress(key: command.arguments[0]!, modifiers: modifiers)
case .keyDown:
let _ = try! keyPresser.keyDown(key: command.arguments[0]!, modifiers: modifiers)
case .keyUp:
let _ = try! keyPresser.keyUp(key: command.arguments[0]!, modifiers: modifiers)
default:
return
}
}

private func executePause(_ command: Command) {
Sleeper.sleep(seconds: Double(command.arguments[0]!)!)
}

private func executeMouseMove(_ command: Command) {
let x1 = Double(command.arguments[0]!)!
let y1 = Double(command.arguments[1]!)!
let x2 = Double(command.arguments[2]!)!
let y2 = Double(command.arguments[3]!)!
let duration: TimeInterval = Double(command.arguments[4]!)!
let modifiers = command.arguments[5]

mouseController.move(
start: CGPoint(x: x1, y: y1),
end: CGPoint(x: x2, y: y2),
duration: duration,
flags: modifiers != nil ? try! KeyPresser.getModifierFlags(modifiers!.components(separatedBy: ",")) : []
)
}

private func executeMouseClick(_ command: Command) {
let button = command.arguments[0]!
let modifiers = command.arguments[1]
let clicks = Int(command.arguments[2]!)!

try! mouseController.click(
CGPoint(x: -1, y: -1),
button: getMouseButton(button: button),
flags: modifiers != nil ? try! KeyPresser.getModifierFlags(modifiers!.components(separatedBy: ",")) : [],
clickCount: clicks
)
}

private func executeMouseScroll(_ command: Command) {
let x = Int(command.arguments[0]!) ?? 0
let y = Int(command.arguments[1]!) ?? 0
let duration = Double(command.arguments[2] ?? "0") ?? 0
let modifiers = command.arguments[3]

mouseController.scroll(
CGPoint(x: x, y: y),
duration,
flags: modifiers != nil ? try! KeyPresser.getModifierFlags(modifiers!.components(separatedBy: ",")) : []
)
}

private func executeMouseDrag(_ command: Command) {
let x1 = Double(command.arguments[0]!)!
let y1 = Double(command.arguments[1]!)!
let x2 = Double(command.arguments[2]!)!
let y2 = Double(command.arguments[3]!)!
let duration: TimeInterval = Double(command.arguments[4]!)!
let button = command.arguments[5]!
let modifiers = command.arguments[6]

try! mouseController.drag(
start: CGPoint(x: x1, y: y1),
end: CGPoint(x: x2, y: y2),
duration: duration,
button: getMouseButton(button: button),
flags: modifiers != nil ? try! KeyPresser.getModifierFlags(modifiers!.components(separatedBy: ",")) : []
)
}

private func getMouseButton(button: String) throws -> CGMouseButton {
switch button {
case "left":
return CGMouseButton.left
case "center":
return CGMouseButton.center
case "right":
return CGMouseButton.right
default:
throw RuntimeError("Unknown mouse button: \(button)")
}
try! command.execute()
}
}
20 changes: 20 additions & 0 deletions Sources/SendKeysLib/Commands/CommandFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class CommandFactory {
public static let commands: [Command.Type] = [
KeyPressCommand.self,
KeyDownCommand.self,
KeyUpCommand.self,
StickyPauseCommand.self,
PauseCommand.self,
ContinuationCommand.self,
NewlineCommand.self,
MouseMoveCommand.self,
MouseClickCommand.self,
MouseDragCommand.self,
MouseScrollCommand.self,
DefaultCommand.self
]

public static func create(_ commandType: Command.Type, arguments: [String?]) -> Command {
return commandType.init(arguments: arguments)
}
}
13 changes: 0 additions & 13 deletions Sources/SendKeysLib/Commands/CommandMatcher.swift

This file was deleted.

28 changes: 6 additions & 22 deletions Sources/SendKeysLib/Commands/CommandsIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@ import Foundation
public class CommandsIterator: IteratorProtocol {
public typealias Element = Command

private let commandMatchers: [CommandMatcher] = [
KeyPressCommandMatcher(),
KeyDownCommandMatcher(),
KeyUpCommandMatcher(),
StickyPauseCommandMatcher(),
PauseCommandMatcher(),
ContinuationCommandMatcher(),
NewlineCommandMatcher(),
MouseMoveCommandMatcher(),
MouseClickCommandMatcher(),
MouseDragCommandMatcher(),
MouseScrollCommandMatcher(),
DefaultCommandMatcher()
]

let commandString: String
var index = 0;

Expand All @@ -29,14 +14,13 @@ public class CommandsIterator: IteratorProtocol {
let length = commandString.utf16.count
if index < length {
var matchResult: NSTextCheckingResult?;
let matcher = commandMatchers.first { (matcher: CommandMatcher) -> Bool in
matchResult = matcher.expression.firstMatch(in: commandString, options: .anchored, range: NSMakeRange(index, length - index))
return matchResult != nil
}

if matcher != nil {
if let commandType = CommandFactory.commands.first(where: { (commandType: Command.Type) -> Bool in
matchResult = commandType.expression.firstMatch(in: commandString, options: .anchored, range: NSMakeRange(index, length - index))
return matchResult != nil
}
) {
let args = getArguments(commandString, matchResult!)
let command = matcher!.createCommand(args)
let command = CommandFactory.create(commandType, arguments: args)

if matchResult != nil {
let range = Range(matchResult!.range, in: commandString)
Expand Down
27 changes: 12 additions & 15 deletions Sources/SendKeysLib/Commands/CommandsProcessor.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Foundation

public class CommandsProcessor {
var defaultPause: Double
let commandExecutor: CommandExecutorProtocol
var defaultPause: TimeInterval

let numberFormatter = NumberFormatter()
let commandExecutor: CommandExecutorProtocol

public init(defaultPause: Double, commandExecutor: CommandExecutorProtocol? = nil) {
self.defaultPause = defaultPause
Expand All @@ -15,7 +16,7 @@ public class CommandsProcessor {
}

private func getDefaultPauseCommand() -> Command {
return Command(.pause, [numberFormatter.string(from: NSNumber(value: defaultPause))!])
return PauseCommand(duration: defaultPause)
}

public func process(_ commandString: String) {
Expand All @@ -29,28 +30,24 @@ public class CommandsProcessor {
continue
}

if command.type == .continuation {
if command is ContinuationCommand {
shouldIgnoreNextCommand = true
continue
}
if command.type == .pause {

if command is StickyPauseCommand {
shouldDefaultPause = false
} else if command.type == .stickyPause {
defaultPause = (command as! StickyPauseCommand).duration
} else if command is PauseCommand {
shouldDefaultPause = false
defaultPause = Double(command.arguments[0]!)!
} else if shouldDefaultPause {
executeCommand(getDefaultPauseCommand())
commandExecutor.execute(getDefaultPauseCommand())
shouldDefaultPause = true
} else {
shouldDefaultPause = true
}

executeCommand(command)
commandExecutor.execute(command)
}
}

func executeCommand(_ command: Command) {
commandExecutor.execute(command)
}
}
16 changes: 16 additions & 0 deletions Sources/SendKeysLib/Commands/ContinuationCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

public class ContinuationCommand: Command {
public override class var commandType: CommandType { return .continuation }

private static let _expression = try! NSRegularExpression(pattern: "\\<\\\\\\>")
public override class var expression: NSRegularExpression { return _expression }

public override init () {
super.init()
}

required public init(arguments: [String?]) {
super.init(arguments: arguments)
}
}
11 changes: 0 additions & 11 deletions Sources/SendKeysLib/Commands/ContinuationCommandMatcher.swift

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/SendKeysLib/Commands/DefaultCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

public class DefaultCommand: KeyPressCommand {
private static let _expression = try! NSRegularExpression(pattern: ".")
public override class var expression: NSRegularExpression { return _expression }

public init(key: String) {
super.init()

self.key = key
}

required public init(arguments: [String?]) {
super.init()
self.key = arguments[0]!
}
}
11 changes: 0 additions & 11 deletions Sources/SendKeysLib/Commands/DefaultCommandMatcher.swift

This file was deleted.

0 comments on commit 2028c02

Please sign in to comment.