Skip to content

Commit

Permalink
feat: add option to specify arbitrary configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Oct 28, 2024
1 parent cc21cce commit 61133bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ _Activates the Notes application and sends keystrokes piped from `stdout` of the
Follows a similar convention to `--characters`. (e.g. `f12:command,shift`).
- `--keyboard-layout <layout>`: Use alternate keyboard layout. Defaults to `qwerty`. `colemak` and `dvorak` are also
supported, pull requests for other common keyboard layouts may be considered. If a specific keyboard layout is not
supported, a custom layout can be defined in the [`~/.sendkeysrc.yml`](./examples/.sendkeysrc.yml) configuration file
(`send.remap`).
supported, a custom layout can be defined in using the `--config` option or using the
[`.sendkeysrc.yml`](./examples/.sendkeysrc.yml) configuration file (`send.remap`).
- `--config <yaml-file>`: Configuration file to load settings from.

## Installation

Expand Down Expand Up @@ -338,12 +339,13 @@ application name, followed by the bundle id.

## Configuration

Common arguments can be stored in the [`~/.sendkeysrc.yml`](./examples/.senkeysrc.yml) configuration file. Configuration
values are applied in the following priority order:
Common arguments can be stored in a [`.sendkeysrc.yml`](./examples/.senkeysrc.yml) configuration file. Configuration
values are applied and merged in the following priority order:

1. Command line arguments
2. Configuration file
3. CLI default values
2. Configuration file defined with `--config` option
3. Configuration file defined in `~/.sendkeysrc.yml`
4. Default values

## Prerequisites

Expand Down
26 changes: 18 additions & 8 deletions Sources/SendKeysLib/Configuration/ConfigLoader.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import Foundation
import Yams

let defaultConfigFiles = [
NSString("~/.sendkeysrc.yml").expandingTildeInPath,
NSString("~/.sendkeysrc.yaml").expandingTildeInPath,
]

struct ConfigLoader {
static func loadConfig() -> AllConfiguration {
let defaultConfigFiles = [
NSString("~/.sendkeysrc.yml").expandingTildeInPath, NSString("~/.sendkeysrc.yaml").expandingTildeInPath,
]
static func loadConfig(_ file: String? = nil) -> AllConfiguration {
var config = AllConfiguration()

let configFiles =
if file != nil {
[file!]
} else {
defaultConfigFiles
}

for configFile in defaultConfigFiles {
if FileManager.default.fileExists(atPath: configFile) {
for configFile in configFiles {
if !configFile.isEmpty && FileManager.default.fileExists(atPath: configFile) {
if let contents = FileManager.default.contents(atPath: configFile) {
do {
let decoder = YAMLDecoder()
return try decoder.decode(AllConfiguration.self, from: contents)
config = config.merge(with: try decoder.decode(AllConfiguration.self, from: contents))
} catch {
print("Unable to read \(configFile): \(error)")
}
}
}
}

return AllConfiguration()
return config
}
}
22 changes: 16 additions & 6 deletions Sources/SendKeysLib/Sender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public struct Sender: ParsableCommand {
@Option(name: .shortAndLong, help: "Character sequence to use to terminate execution (e.g. f12:command).")
var terminateCommand: String?

@Option(
name: NameSpecification([.customLong("config")]),
help: "Configuration file to load settings from (yaml format).")
var configurationFile: String?

@Option(name: .long, help: "Keyboard layout to use for sending keystrokes.")
var keyboardLayout: KeyMappings.Layouts?

Expand All @@ -69,12 +74,17 @@ public struct Sender: ParsableCommand {
let app: NSRunningApplication? = try activator.find()
let keyPresser: KeyPresser

self.config = self.config
.merge(with: ConfigLoader.loadConfig().send)
.merge(
with: SendConfig(
activate: activate, animationInterval: animationInterval, delay: delay, initialDelay: initialDelay,
keyboardLayout: keyboardLayout, targeted: targeted, terminateCommand: terminateCommand))
// load from home directory by default
self.config = self.config.merge(with: ConfigLoader.loadConfig().send)

if !(configurationFile ?? "").isEmpty {
self.config = self.config.merge(with: ConfigLoader.loadConfig(configurationFile!).send)
}

self.config = self.config.merge(
with: SendConfig(
activate: activate, animationInterval: animationInterval, delay: delay, initialDelay: initialDelay,
keyboardLayout: keyboardLayout, targeted: targeted, terminateCommand: terminateCommand))

let activate = activate ?? self.config.activate!
let targeted = targeted ?? self.config.targeted!
Expand Down

0 comments on commit 61133bf

Please # to comment.