Repository: utmapp/UTM
File: Platform/macOS/Display/VMDisplayTerminalWindowController.swift (Swift)

Code before:
            return
        }
        guard let rows = self.rows else {
            logger.error("Did not get rows from page")
            return
        }
        let template = vmQemuConfig?.consoleResizeCommand ?? kVMDefaultResizeCmd
        let cmd = template
            .replacingOccurrences(of: "$COLS", with: String(columns))
            .replacingOccurrences(of: "$ROWS", with: String(rows))
            .replacingOccurrences(of: "\\n", with: "\n")
        qemuVM.sendInput(cmd)
    }
}

extension VMDisplayTerminalWindowController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        updateSettings()
    }
    
    func updateSettings() {
        if let consoleTextColor = vmQemuConfig?.consoleTextColor,
           let consoleBackgroundColor = vmQemuConfig?.consoleBackgroundColor,
           // TODO: Should use default colors, or keep it as if let?
           let consoleTextColorString = consoleTextColor.hexString,
           let consoleBackgroundColorString = consoleBackgroundColor.hexString {
            webView.evaluateJavaScript("changeColor('\(consoleTextColorString)', '\(consoleBackgroundColorString)');") { (_, err) in
                if let error = err {
                    logger.error("changeColor error: \(error)")
                }
            }
        }
        if let consoleFont = vmQemuConfig?.consoleFont {
            let consoleFontSize = vmQemuConfig?.consoleFontSize?.intValue ?? 12
            webView.evaluateJavaScript("changeFont('\(consoleFont)', \(consoleFontSize));") { (_, err) in
                if let error = err {
                    logger.error("changeFont error: \(error)")
                }
            }
        }
        if let cursorBlink = vmQemuConfig?.consoleCursorBlink {
            webView.evaluateJavaScript("setCursorBlink(\(cursorBlink ? "true" : "false"));") { (_, err) in
                if let error = err {
                    logger.error("setCursorBlink error: \(error)")
                }
            }
        }
    }
}

extension VMDisplayTerminalWindowController: WKScriptMessageHandler {

Review comment (suggestion):
I don't think using a default value would be necessary here, since default colors are set when migrating the configuration, and also in the `Binding` in the configuration view.

Code after:
            return
        }
        guard let rows = self.rows else {
            logger.error("Did not get rows from page")
            return
        }
        let template = vmQemuConfig?.consoleResizeCommand ?? kVMDefaultResizeCmd
        let cmd = template
            .replacingOccurrences(of: "$COLS", with: String(columns))
            .replacingOccurrences(of: "$ROWS", with: String(rows))
            .replacingOccurrences(of: "\\n", with: "\n")
        qemuVM.sendInput(cmd)
    }
}

extension VMDisplayTerminalWindowController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        updateSettings()
    }
    
    func updateSettings() {
        if let consoleTextColor = vmQemuConfig?.consoleTextColor,
           let consoleBackgroundColor = vmQemuConfig?.consoleBackgroundColor {
            webView.evaluateJavaScript("changeColor('\(consoleTextColor)', '\(consoleBackgroundColor)');") { (_, err) in
                if let error = err {
                    logger.error("changeColor error: \(error)")
                }
            }
        }
        if let consoleFont = vmQemuConfig?.consoleFont {
            let consoleFontSize = vmQemuConfig?.consoleFontSize?.intValue ?? 12
            webView.evaluateJavaScript("changeFont('\(consoleFont)', \(consoleFontSize));") { (_, err) in
                if let error = err {
                    logger.error("changeFont error: \(error)")
                }
            }
        }
        if let cursorBlink = vmQemuConfig?.consoleCursorBlink {
            webView.evaluateJavaScript("setCursorBlink(\(cursorBlink ? "true" : "false"));") { (_, err) in
                if let error = err {
                    logger.error("setCursorBlink error: \(error)")
                }
            }
        }
    }
}

extension VMDisplayTerminalWindowController: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        guard let command = JSCommand.init(rawValue: message.name) else {
            logger.error("Unknown command: \(message.name)")