import Carbon
import Foundation
func prop(_ s:TISInputSource,_ k:CFString)->String { guard let p=TISGetInputSourceProperty(s,k) else{return ""}; return Unmanaged<CFString>.fromOpaque(p).takeUnretainedValue() as String }
let list=TISCreateInputSourceList(nil,true).takeRetainedValue() as! [TISInputSource]
let arg=CommandLine.arguments.dropFirst().first ?? "list"
if arg == "enabled" {
 for s in list { if let p=TISGetInputSourceProperty(s,kTISPropertyInputSourceIsEnabled), Unmanaged<CFBoolean>.fromOpaque(p).takeUnretainedValue() == kCFBooleanTrue {print(prop(s,kTISPropertyInputSourceID))} }
} else if arg == "disable" { for s in list where prop(s,kTISPropertyInputSourceID)==CommandLine.arguments[2] {print(TISDisableInputSource(s))} } else if arg == "list" {
 for s in list { print(prop(s,kTISPropertyInputSourceID),prop(s,kTISPropertyLocalizedName)) }
} else if arg == "current" { print(prop(TISCopyCurrentKeyboardInputSource().takeRetainedValue(),kTISPropertyInputSourceID)) }
else { for s in list where prop(s,kTISPropertyInputSourceID)==arg { print("enable",TISEnableInputSource(s)); print("select",TISSelectInputSource(s)) } }
