Skip to content

Commit

Permalink
fix: make all block + allow optional, fix allow mode logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Dec 23, 2023
1 parent 0ef2336 commit 90cc253
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
29 changes: 24 additions & 5 deletions Sources/hyper-focus/action_handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ enum ActionHandler {
}

static func appAction(_ data: SwitchingActivity) -> Bool {
if match(data.app, data.configuration.block_apps) {
if match(data.app, data.configuration.allow_apps ?? []) {
let allowApps = data.configuration.allow_apps
let hasAllowApps = allowApps != nil

// block list is only used if there is no allow list
if hasAllowApps {
if match(data.app, allowApps!) {
log("app is in allow_apps, releasing")
return false
} else {
log("app is not in allow_apps, blocking")
// TODO: sometimes this hide method does not work
NSWorkspace.shared.frontmostApplication!.hide()
return true
}
}

if match(data.app, data.configuration.block_apps ?? []) {
log("app is in block_apps, hiding application to prevent usage")

// TODO: sometimes this hide method does not work
NSWorkspace.shared.frontmostApplication!.hide()
return true
Expand Down Expand Up @@ -61,9 +71,12 @@ enum ActionHandler {
// thus if url matches allow_url we can automatically release, and if it doesn't but it matches block_url we can block
// if it matches neither, we can continue to the host check

let allowModeEnabled = data.configuration.allow_hosts != nil || data.configuration.allow_urls != nil
debug("allow_mode_enabled: \(allowModeEnabled)")

debug("checking urls for any blocked matches")

let blockUrls = data.configuration.block_urls
let blockUrls = data.configuration.block_urls ?? []
let allowUrls = data.configuration.allow_urls ?? []

// note: the url takes precedence over the host, and the allow takes precedence over the block,
Expand All @@ -80,7 +93,7 @@ enum ActionHandler {

debug("checking hosts for any blocked matches")

let blockHosts = data.configuration.block_hosts
let blockHosts = data.configuration.block_hosts ?? []
let allowHosts = data.configuration.allow_hosts ?? []

// TODO: add 'www.' to all host entries which are not regex, this is not something users want to do manually!
Expand Down Expand Up @@ -111,6 +124,12 @@ enum ActionHandler {
return true
}

if allowModeEnabled {
error("allow mode enabled, blocking by default")
blockTab(data.activeTab)
return true
}

return false
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/hyper-focus/hyper_focus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct Configuration: Codable {
var description: String?
var schedule_only: Bool?
var start_script: String?
var block_hosts: [String]
var block_urls: [String]
var block_apps: [String]
var block_hosts: [String]?
var block_urls: [String]?
var block_apps: [String]?
var allow_hosts: [String]?
var allow_urls: [String]?
var allow_apps: [String]?
Expand Down
2 changes: 1 addition & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"type": "string"
}
},
"required": ["name", "block_hosts", "block_urls", "block_apps", "allow_hosts", "allow_urls", "allow_apps"]
"required": ["name"]
}
}
},
Expand Down

0 comments on commit 90cc253

Please sign in to comment.