<commit_before>/**
 * @file	KCTerminalViewCore.swift
 * @brief Define KCTerminalViewCore class
 * @par Copyright
 *   Copyright (C) 2017-2020 Steel Wheels Project
 */

#if os(OSX)
	import Cocoa
#else
	import UIKit
#endif
import CoconutData

open class KCTerminalViewCore : KCView, KCTextViewDelegate, NSTextStorageDelegate
{
	public enum TerminalMode {
		case log
		case console
	}

	#if os(OSX)
	@IBOutlet weak var	mScrollView: NSScrollView!
	@IBOutlet weak var 	mTextView: NSTextView!
	#else
	@IBOutlet weak var mTextView: UITextView!
	#endif

	private var mInputPipe:			Pipe
	private var mOutputPipe:		Pipe
	private var mErrorPipe:			Pipe
	private var mCurrentIndex:		Int
	private var mSavedIndex:		Int
	private var mTerminalInfo:		CNTerminalInfo

	public override init(frame frameRect: KCRect) {
		let tpref = CNPreference.shared.terminalPreference

		mInputPipe	= Pipe()
		mOutputPipe	= Pipe()
		mErrorPipe	= Pipe()
		mCurrentIndex	= 0
		mSavedIndex	= 0
		mTerminalInfo	= CNTerminalInfo(width: tpref.width, height: tpref.height)
		mTerminalInfo.foregroundColor = tpref.foregroundTextColor
		mTerminalInfo.backgroundColor = tpref.backgroundTextColor
		super.init(frame: frameRect)
		setupObservers()
		setupFileStream()
	}

	public required init?(coder: NSCoder) {
		let tpref = CNPreference.shared.terminalPreference

		mInputPipe	= Pipe()
		mOutputPipe	= Pipe()
		mErrorPipe	= Pipe()
		mCurrentIndex	= 0
		mSavedIndex	= 0
		mTerminalInfo	= CNTerminalInfo(width: tpref.width, height: tpref.height)
		mTerminalInfo.foregroundColor = tpref.foregroundTextColor
		mTerminalInfo.backgroundColor = tpref.backgroundTextColor
		super.init(coder: coder)
		setupObservers()
		setupFileStream()
	}

	deinit {
		removeObservers()
	}

	private func setupObservers() {
		/* Start observe */
		let tpref = CNPreference.shared.terminalPreference
		tpref.addObserver(observer: self, forKey: tpref.WidthItem)
		tpref.addObserver(observer: self, forKey: tpref.HeightItem)
		tpref.addObserver(observer: self, forKey: tpref.ForegroundTextColorItem)
		tpref.addObserver(observer: self, forKey: tpref.BackgroundTextColorItem)
		tpref.addObserver(observer: self, forKey: tpref.FontItem)

		let spref = CNPreference.shared.systemPreference
		spref.addObserver(observer: self, forKey: CNSystemPreference.InterfaceStyleItem)
	}

	private func setupFileStream() {
		if mInputPipe.fileHandleForReading.setRawMode(enable: true) != 0 {
			NSLog("Failed to set raw mode on KCTerminalViewCore")
		}

		mOutputPipe.fileHandleForReading.readabilityHandler = {
			(_ hdl: FileHandle) -> Void in
			let data = hdl.availableData
			if let str = String.stringFromData(data: data) {
				CNExecuteInMainThread(doSync: false, execute: {
					() -> Void in
					self.receiveInputStream(string: str)
					self.scrollToBottom()
				})
			}<commit_msg>KCTerminalViewCore: Remove raw mode setting<commit_after>/**
 * @file	KCTerminalViewCore.swift
 * @brief Define KCTerminalViewCore class
 * @par Copyright
 *   Copyright (C) 2017-2020 Steel Wheels Project
 */

#if os(OSX)
	import Cocoa
#else
	import UIKit
#endif
import CoconutData

open class KCTerminalViewCore : KCView, KCTextViewDelegate, NSTextStorageDelegate
{
	public enum TerminalMode {
		case log
		case console
	}

	#if os(OSX)
	@IBOutlet weak var	mScrollView: NSScrollView!
	@IBOutlet weak var 	mTextView: NSTextView!
	#else
	@IBOutlet weak var mTextView: UITextView!
	#endif

	private var mInputPipe:			Pipe
	private var mOutputPipe:		Pipe
	private var mErrorPipe:			Pipe
	private var mCurrentIndex:		Int
	private var mSavedIndex:		Int
	private var mTerminalInfo:		CNTerminalInfo

	public override init(frame frameRect: KCRect) {
		let tpref = CNPreference.shared.terminalPreference

		mInputPipe	= Pipe()
		mOutputPipe	= Pipe()
		mErrorPipe	= Pipe()
		mCurrentIndex	= 0
		mSavedIndex	= 0
		mTerminalInfo	= CNTerminalInfo(width: tpref.width, height: tpref.height)
		mTerminalInfo.foregroundColor = tpref.foregroundTextColor
		mTerminalInfo.backgroundColor = tpref.backgroundTextColor
		super.init(frame: frameRect)
		setupObservers()
		setupFileStream()
	}

	public required init?(coder: NSCoder) {
		let tpref = CNPreference.shared.terminalPreference

		mInputPipe	= Pipe()
		mOutputPipe	= Pipe()
		mErrorPipe	= Pipe()
		mCurrentIndex	= 0
		mSavedIndex	= 0
		mTerminalInfo	= CNTerminalInfo(width: tpref.width, height: tpref.height)
		mTerminalInfo.foregroundColor = tpref.foregroundTextColor
		mTerminalInfo.backgroundColor = tpref.backgroundTextColor
		super.init(coder: coder)
		setupObservers()
		setupFileStream()
	}

	deinit {
		removeObservers()
	}

	private func setupObservers() {
		/* Start observe */
		let tpref = CNPreference.shared.terminalPreference
		tpref.addObserver(observer: self, forKey: tpref.WidthItem)
		tpref.addObserver(observer: self, forKey: tpref.HeightItem)
		tpref.addObserver(observer: self, forKey: tpref.ForegroundTextColorItem)
		tpref.addObserver(observer: self, forKey: tpref.BackgroundTextColorItem)
		tpref.addObserver(observer: self, forKey: tpref.FontItem)

		let spref = CNPreference.shared.systemPreference
		spref.addObserver(observer: self, forKey: CNSystemPreference.InterfaceStyleItem)
	}

	private func setupFileStream() {
		mOutputPipe.fileHandleForReading.readabilityHandler = {
			(_ hdl: FileHandle) -> Void in
			let data = hdl.availableData
			if let str = String.stringFromData(data: data) {
				CNExecuteInMainThread(doSync: false, execute: {
					() -> Void in
					self.receiveInputStream(string: str)
					self.scrollToBottom()
				})
			}
{'fraction_non_alphanumeric': 0.08657851838435066, 'fraction_numerical': 0.004528165187466039, 'mean_word_length': 4.7701149425287355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3940147', 'n_tokens_mistral': 2125, 'n_tokens_neox': 1902, 'n_words': 421}