 // 6    id, width, scale
 // 7    id, width, height, scale
 struct DisplayUserSetting {
-  static let MAX_SCALE = 10
-  
-  var displayIndex = 0, width = 0
-  var height, scale:Int?
-  init(_ arr:[String]) {
-    var args = arr.compactMap { Int($0) }
-    
-    if args.count < 1 { return }
-    
-    if args[0] > Screens.MAX_DISPLAYS { args.insert(0 /* displayIndex */, at:0) }
-
-    if args.count < 2 { return }
-
-    displayIndex = args[0]
-    width = args[1]
-
-    if args.count == 2 { return }
-
-    if args[2] > DisplayUserSetting.MAX_SCALE {
-      height = args[2]
-      if args.count > 3 { scale = args[3] }
-    }
-    else {
-      scale = args[2]
-      if args.count > 3 { height = args[3] }
-    }
-  }
-
-  // override a lesser-used operator to simplify display mode checks
-  static func == (lhs: Self, rhs: CGDisplayModeFromJSON) -> Bool {
-      return rhs == lhs
-  }
+    static let MAX_SCALE = 10
+    
+    var displayIndex = 0, width = 0
+    var height, scale:Int?
+    init(_ arr:[String]) {
+        var args = arr.compactMap { Int($0) }
+        
+        if args.count < 1 { return }
+        
+        if args[0] > Screens.MAX_DISPLAYS { args.insert(0 /* displayIndex */, at:0) }
+        
+        if args.count < 2 { return }
+        
+        displayIndex = args[0]
+        width = args[1]
+        
+        if args.count == 2 { return }
+        
+        if args[2] > DisplayUserSetting.MAX_SCALE {
+            height = args[2]
+            if args.count > 3 { scale = args[3] }
+        }
+        else {
+            scale = args[2]
+            if args.count > 3 { height = args[3] }
+        }
+    }
+    
+    // override a lesser-used operator to simplify display mode checks
+    static func == (lhs: Self, rhs: CGDisplayModeFromJSON) -> Bool {
+        return rhs == lhs
+    }
 }
 
 class Screens {
-  // assume at most 8 display connected
-  static let MAX_DISPLAYS = 8
-  var maxDisplays = MAX_DISPLAYS
-  // actual number of display
-  var displayCount:Int = 0
-  var dm = [DisplayManager]()
-  
-  init() {
+    // assume at most 8 display connected
+    static let MAX_DISPLAYS = 8
+    var maxDisplays = MAX_DISPLAYS
     // actual number of display
-    var displayCount32:UInt32 = 0
-    var displayIDs = [CGDirectDisplayID](repeating: 0, count:maxDisplays)
-
-    guard CGGetOnlineDisplayList(UInt32(maxDisplays), &displayIDs, &displayCount32) == .success else {
-      print("Error on getting online display List.")
-      return
-    }
-    displayCount = Int( displayCount32 )
-    dm = displayIDs
-      .filter { $0 != 0 }
-      .map { DisplayManager($0) }
-  }
-
-  // print a list of all displays
-  // used by -l
-  func listDisplays() {
-    for (i, m) in dm.enumerated() {
-     m.printForOneDisplay("Display \(i):")
-    }
-  }
-
-  func listModes(_ displayIndex:Int) {
-    dm[displayIndex].printFormatForAllModes()
-  }
-
-  func set(with setting:DisplayUserSetting) {
-    dm[setting.displayIndex].set(with:setting)
-  }
+    var displayCount:Int = 0
+    var dm = [DisplayManager]()
+    
+    init() {
+        // actual number of display
+        var displayCount32:UInt32 = 0
+        var displayIDs = [CGDirectDisplayID](repeating: 0, count:maxDisplays)
+        
+        guard CGGetOnlineDisplayList(UInt32(maxDisplays), &displayIDs, &displayCount32) == .success else {
+            print("Error on getting online display List.")
+            return
+        }
+        displayCount = Int( displayCount32 )
+        dm = displayIDs
+            .filter { $0 != 0 }
+            .map { DisplayManager($0) }
+    }
+    
+    // print a list of all displays
+    // used by -l
+    func listDisplays() {
+        for (i, m) in dm.enumerated() {
+            m.printForOneDisplay("Display \(i):")
+        }
+    }
+    
+    func listModes(_ displayIndex:Int) {
+        dm[displayIndex].printFormatForAllModes()
+    }
+    
+    func set(with setting:DisplayUserSetting) {
+        dm[setting.displayIndex].set(with:setting)
+    }
 }
 
 // darkMode toggle code with JXA ;-)
 // Method from Stackoverflow User: bacongravy
 // https://stackoverflow.com/questions/44209057
 struct DarkMode {
-  static let scriptString = """
+    static let scriptString = """
   pref = Application(\"System Events\").appearancePreferences
   pref.darkMode = !pref.darkMode()
 """
-  let script = OSAScript.init(source: scriptString, language: OSALanguage.init(forName: "JavaScript"))
-  
-  init() {
-    var compileError: NSDictionary?
-
-    script.compileAndReturnError(&compileError)
-  }
-  func toggle() {
-    var scriptError: NSDictionary?
-
-    if let result = script.executeAndReturnError(&scriptError)?.stringValue { print("Dark Mode:", result) }
-  }
+    let script = OSAScript.init(source: scriptString, language: OSALanguage.init(forName: "JavaScript"))
+    
+    init() {
+        var compileError: NSDictionary?
+        
+        script.compileAndReturnError(&compileError)
+    }
+    func toggle() {
+        var scriptError: NSDictionary?
+        
+        if let result = script.executeAndReturnError(&scriptError)?.stringValue { print("Dark Mode:", result) }
+    }
 }
 
 func sleepDisplay() {
-  let wrangler = strdup("IOService:/IOResources/IODisplayWrangler")
-  // for legacy system
-  // current compile threshold is a ballpark guess, tested on Catalina only  
-  #if compiler(<5.5)
-  let flag = kIOMasterPortDefault
-  #else
-  let flag = kIOMainPortDefault
-  #endif
-  // https://github.com/glfw/glfw/issues/1985
-  let r = IORegistryEntryFromPath(flag, wrangler)
-  IORegistryEntrySetCFProperty(r, ("IORequestIdle" as CFString), kCFBooleanTrue)
-  IOObjectRelease(r)
+    let wrangler = strdup("IOService:/IOResources/IODisplayWrangler")
+    // for legacy system
+    // current compile threshold is a ballpark guess, tested on Catalina only
+#if compiler(<5.5)
+    let flag = kIOMasterPortDefault
+#else
+    let flag = kIOMainPortDefault
+#endif
+    // https://github.com/glfw/glfw/issues/1985
+    let r = IORegistryEntryFromPath(flag, wrangler)
+    IORegistryEntrySetCFProperty(r, ("IORequestIdle" as CFString), kCFBooleanTrue)
+    IOObjectRelease(r)
 }
 
 func seeHelp() {
-print("""
+    print("""
 Usage:
 screen-resolution-switcher [-h|--help] [-l|--list|list] [-m|--mode|mode displayIndex]
 [-s|--set|set displayIndex width scale] [-r|--set-retina|retina displayIndex width],

}

func main() {
  let screens = Screens()

  let arguments = CommandLine.arguments
  let count = arguments.count
  guard count >= 2 else {
      seeHelp()
      return
  }
  switch arguments[1] {
  case "-l", "--list", "list":
      screens.listDisplays()
  case "-m", "--mode", "mode":
      var displayIndex = 0
      if count > 2, let index = Int(arguments[2]) {
          displayIndex = index
      }
      if displayIndex < screens.displayCount {
          print("Supported Modes for Display \(displayIndex):")
          screens.listModes(displayIndex)
      } else {
          print("Display index not found. List all available displays by:\n    screen-resolution-switcher -l")
      }
  case "-s", "--set", "set", "-r", "--set-retina", "retina":
      screens.set(with:DisplayUserSetting( arguments ))
  case "-d", "--toggle-dark-mode":
      DarkMode().toggle()
  case "-sl", "--sleep", "sleep":
      //if #available(macOS 12.0, *) {
          sleepDisplay()
      //}
  default:
      seeHelp()
  }
}

#if os(macOS)
  // run it
  main()
#else
  print("This script currently only runs on macOS")
#endif

import OSAKit
import IOKit

// DisplayMode
struct MyDisplayMode {
    let mode:CGDisplayMode
    let modeFromJSON:CGDisplayModeFromJSON
    init(mode:CGDisplayMode, modeFromJSON:CGDisplayModeFromJSON) {
        self.mode = mode
        self.modeFromJSON = modeFromJSON
    }
}

// a custom data type that stores String or Double
// from https://medium.com/swlh/f6ea6a2babf8
enum StringOrDouble: Decodable {
    case double(Double)
    case string(String)
    
    init(from decoder: Decoder) throws {
        if let string = try? decoder.singleValueContainer().decode(String.self) {
            self = .string(string)
            return
        }
        if let double = try? decoder.singleValueContainer().decode(Double.self) {
            self = .double(double)
            return
        }
        throw Error.couldNotFindStringOrDouble
    }
    enum Error: Swift.Error {
        case couldNotFindStringOrDouble
    }
}

// get computed refreshrate
fileprivate func _getAlternativeRefreshRate(by displayID:CGDirectDisplayID) -> Int {
    var link:CVDisplayLink?
    CVDisplayLinkCreateWithCGDisplay(displayID, &link)
    
