<commit_before>//
//  ViewController.swift
//  Fuwari
//
//  Created by <NAME> on 2016/11/29.
//  Copyright © 2016年 AppKnop. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {

    fileprivate var windowControllers = [NSWindowController]()
    fileprivate var fullScreenWindows = [FullScreenWindow]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NSScreen.screens.forEach {
            let fullScreenWindow = FullScreenWindow(contentRect: $0.frame, styleMask: .borderless, backing: .buffered, defer: false)
            fullScreenWindow.captureDelegate = self
            fullScreenWindows.append(fullScreenWindow)
            let controller = NSWindowController(window: fullScreenWindow)
            controller.showWindow(nil)
            windowControllers.append(controller)
            fullScreenWindow.orderOut(nil)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(startCapture), name: Notification.Name(rawValue: Constants.Notification.capture), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: Constants.Notification.capture), object: nil)
    }
    
    fileprivate func createFloatWindow(rect: NSRect, image: CGImage) {
        let floatWindow = FloatWindow(contentRect: rect, image: image)
        floatWindow.floatDelegate = self
        let floatWindowController = NSWindowController(window: floatWindow)
        floatWindowController.showWindow(nil)
        windowControllers.append(floatWindowController)
    }
    
    @objc private func startCapture() {
        NSCursor.hide()
        
        fullScreenWindows.forEach { $0.startCapture() }
    }
}

extension ViewController: CaptureDelegate {
    func didCaptured(rect: NSRect, image: CGImage) {
        createFloatWindow(rect: rect, image: image)
        NSCursor.unhide()
        fullScreenWindows.forEach { $0.orderOut(nil) }
    }
    
    func didCanceled() {
        NSCursor.unhide()
        fullScreenWindows.forEach { $0.orderOut(nil) }
    }
}

extension ViewController: FloatDelegate {
    func close(floatWindow: FloatWindow) {
        windowControllers.filter({ $0.window === floatWindow }).first?.close()
    }

    func save(floatWindow: FloatWindow, image: CGImage) {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd-HHmmss"
        
        let savePanel = NSSavePanel()
        savePanel.canCreateDirectories = true
        savePanel.showsTagField = false
        savePanel.nameFieldStringValue = "screenshot-\(formatter.string(from: Date())).png"
        savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
        savePanel.begin { (result) in
            if result.rawValue == NSFileHandlingPanelOKButton {
                guard let url = savePanel.url else { return }
                
                let bitmapRep = NSBitmapImageRep(cgImage: image)
                let data = bitmapRep.representation(using: .png, properties: [:])
                do {
                    try data?.write(to: url, options: .atomicWrite)
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }
}<commit_msg>:heavy_plus_sign: Add various export format<commit_after>//
//  ViewController.swift
//  Fuwari
//
//  Created by <NAME> on 2016/11/29.
//  Copyright © 2016年 AppKnop. All rights reserved.
//

import Cocoa
import Quartz

class ViewController: NSViewController {

    private var windowControllers = [NSWindowController]()
    private var fullScreenWindows = [FullScreenWindow]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NSScreen.screens.forEach {
            let fullScreenWindow = FullScreenWindow(contentRect: $0.frame, styleMask: .borderless, backing: .buffered, defer: false)
            fullScreenWindow.captureDelegate = self
            fullScreenWindows.append(fullScreenWindow)
            let controller = NSWindowController(window: fullScreenWindow)
            controller.showWindow(nil)
            windowControllers.append(controller)
            fullScreenWindow.orderOut(nil)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(startCapture), name: Notification.Name(rawValue: Constants.Notification.capture), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: Constants.Notification.capture), object: nil)
    }
    
    private func createFloatWindow(rect: NSRect, image: CGImage) {
        let floatWindow = FloatWindow(contentRect: rect, image: image)
        floatWindow.floatDelegate = self
        let floatWindowController = NSWindowController(window: floatWindow)
        floatWindowController.showWindow(nil)
        windowControllers.append(floatWindowController)
    }
    
    @objc private func startCapture() {
        NSCursor.hide()
        
        fullScreenWindows.forEach { $0.startCapture() }
    }
}

extension ViewController: CaptureDelegate {
    func didCaptured(rect: NSRect, image: CGImage) {
        createFloatWindow(rect: rect, image: image)
        NSCursor.unhide()
        fullScreenWindows.forEach { $0.orderOut(nil) }
    }
    
    func didCanceled() {
        NSCursor.unhide()
        fullScreenWindows.forEach { $0.orderOut(nil) }
    }
}

extension ViewController: FloatDelegate {
    func close(floatWindow: FloatWindow) {
        windowControllers.filter({ $0.window === floatWindow }).first?.close()
    }

    func save(floatWindow: FloatWindow, image: CGImage) {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd-HHmmss"
        
        let savePanel = NSSavePanel()
        savePanel.canCreateDirectories = true
        savePanel.showsTagField = false
        savePanel.nameFieldStringValue = "screenshot-\(formatter.string(from: Date()))"
        savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
        let saveOptions = IKSaveOptions(imageProperties: [:], imageUTType: kUTTypePNG as String?)
        saveOptions?.addAccessoryView(to: savePanel)
        
        let result = savePanel.runModal()
        if result == .OK {
            if let url = savePanel.url as CFURL?, let type = saveOptions?.imageUTType as CFString? {
                guard let destination = CGImageDestinationCreateWithURL(url, type, 1, nil) else { return }
                CGImageDestinationAddImage(destination, image, saveOptions!.imageProperties! as CFDictionary)
                CGImageDestinationFinalize(destination)
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09414909414909414, 'fraction_numerical': 0.005197505197505198, 'mean_word_length': 2.6883899233296824, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3597142', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1803, 'n_words': 372}