Skip to content

Commit

Permalink
Add Dark Mode toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
th507 committed Jan 23, 2020
1 parent 51e4f4c commit 9f33910
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Tested with Swift 5.1.3, Xcode 11.3.1 (11C504), OS X Catalina 10.15.2 (19C57), R
./scres.swift -s 2880
```

### Toggle Dark Mode (Code from Sindre Sorhus https://github.com/sindresorhus/dark-mode/)
```bash
./scres.swift -d
```

### Show quick help
```bash
./scres.swift -h
Expand Down
31 changes: 28 additions & 3 deletions scres.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,32 @@ struct DisplayInfo: Hashable {
}
}

// from Sindre Sorhus
// https://github.com/sindresorhus/dark-mode/blob/master/Sources/DarkMode.swift
struct DarkMode {
private static let prefix = "tell application \"System Events\" to tell appearance preferences to"

static var isEnabled: Bool {
get {
UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark"
}
set {
toggle(force: newValue)
}
}

static func toggle(force: Bool? = nil) {
let value = force.map(String.init) ?? "not dark mode"
NSAppleScript(source: "\(prefix) set dark mode to \(value)")?.executeAndReturnError(nil)
}
}

struct UserInput {
enum Intention {
case listDisplays
case listModes(Int)
case setMode
case darkMode
case seeHelp
}

Expand All @@ -248,6 +268,8 @@ struct UserInput {
intention = Intention.listModes(index)
case "-s", "--set", "set", "-r", "--set-retina", "retina":
intention = Intention.setMode
case "-d", "--toggle-dark-mode":
intention = Intention.darkMode
default:
intention = Intention.seeHelp
}
Expand All @@ -272,11 +294,12 @@ Here are some examples:
-l list displays
-m 0 list all mode from a certain display
-m shorthand for -m 0
-s 0 800 2 set resolution of display 0 to 800 [x 600] @ 2x [@ 60Hz]
-s 0 800 shorthand for -s 0 800 1
-s 800 shorthand for -s 0 800 1
-s 0 800 1 set resolution of display 0 to 800 [x 600] @ 1x [@ 60Hz]
-s 0 800 shorthand for -s 0 800 2 (highest scale factor)
-s 800 shorthand for -s 0 800 2 (highest scale factor)
-r 0 800 shorthand for -s 0 800 2
-r 800 shorthand for -s 0 800 2
-d toggle macOS Dark Mode
"""

func main () {
Expand Down Expand Up @@ -325,6 +348,8 @@ func main () {

screens.set(displayIndex:arg2, width:arg3, scale:arg4)
}
case .darkMode:
DarkMode.toggle()
default:
print(help_msg)
}
Expand Down

0 comments on commit 9f33910

Please sign in to comment.