#!/usr/bin/env python3
import sys
from buffer import Buffer
from get_command import get_command

def version():
	print("it.py line editor; ALPHA 2021")
	return None

def main(buffer, supplied_command):
	if supplied_command != [] and len(supplied_command) > 1:
		command = supplied_command[1:]
	else:
		command = get_command("")

	while True:
		# EOFError in get_command(); ^D
		if command == 0:
			break

		if command == []:
			continue

		if command[0] in buffer.modules.keys() or buffer.import_module_(command[0]):
			buffer = buffer.modules[command[0]].main(buffer, command)
			if type(buffer) is int:
				break

		command = get_command("")

	return buffer

if __name__ == "__main__":
	buffer = main(Buffer(), [])
	if type(buffer) is int:
		sys.exit(buffer)
	else:
		sys.exit(0)
