#!/bin/python3

import curses, sys

from pyt.interface import window_setup


class StdoutWrapper(object):
	def __init__(self):
		self.callback = None

		self.write_to_file = False

		if self.write_to_file:
			self.log_file = 'pyt.log'
			open(self.log_file, 'w').close() # clear contents of log file

	def write(self, text):
		if self.write_to_file:
			with open(self.log_file, "a") as fd:
				fd.write(text + "\n")

		if self.callback != None and len(text) != 0 and text != '\n':
			self.callback(
				"recv_msg",
				{
					'target': 'master',
					'sender': 'system',
					'msg': text
				}
			)

	def set_callback(self, callback):
		self.callback = callback

	def flush(self):
		pass


if __name__ == '__main__':
	std = StdoutWrapper()
	sys.stdout = std
	sys.stderr = std

	curses.wrapper(window_setup.setup, std);