diff --git a/it/main.py b/it/main.py old mode 100644 new mode 100755 index 6244c49..6521aa0 --- a/it/main.py +++ b/it/main.py @@ -1,25 +1,13 @@ +#!/usr/bin/env python3 # it, a shITtier version of ed import importlib import sys -oldinput = input - -def input(*args): - message = args[0] if len(args) > 0 else "" - while 1: - try: - user_input = str(oldinput(message)) - except KeyboardInterrupt: - pass - except EOFError: - sys.exit(0) - return user_input - def parsecommand(command): casesensitive = True - escapes = ["\\"] - quotes = ["'", '"'] - spaces = [" ", "\t"] + escapes = {"\\"} + quotes = {"'", '"'} + spaces = {" ", "\t"} command = command.strip() @@ -27,48 +15,56 @@ def parsecommand(command): word = "" parsed_command = [] + # copied from other project. mystery code. works though for i in range(len(command)): - if (command[i] in quotes) and inQuotes == 0\ - and (i == 0 \ - or (i > 0 and not(command[i-1] in escapes))): + if ((command[i] in quotes) + and inQuotes == 0 + and (i == 0 + or (i > 0 and not(command[i-1] in escapes)))): inQuotes = command[i] - elif command[i] == inQuotes \ - and (i > 0 and not(command[i-1] in escapes)): + elif (command[i] == inQuotes + and (i > 0 and not(command[i-1] in escapes))): inQuotes = 0 - elif command[i] in space and inQuotes == 0\ - and (i > 0 and not(command[i-1] in escapes)): + elif (command[i] in spaces and inQuotes == 0 + and (i > 0 and not(command[i-1] in escapes))): parsed_command += [word] word = "" - elif command[i] != "\\" or (i == len(command) - 1) \ - or not(command[i+1] in space + quotes): + elif (command[i] != "\\" or (i == len(command) - 1) + or not(command[i+1] in spaces + quotes)): word += command[i] parsed_command += [word] - if parsed_command == ['']: return [] - return parsed_command + return [] if parsed_command == [''] else parsed_command -class buffer: # all the information about the current file - content = [] - dot = -1 - filename = "" - saved = 0 +class Buffer: # all the information about the current file + def __init__(self): + self.content = [] # content of the file + self.dot = 0 # where we are in the file + self.filename = "" # name of where we'll save the file + self.saved = 1 # bool that says whether or not we have saved the file -def main(argc, argv): - file = buffer() +def main(): + buffer = Buffer() modules = {} while True: - command = parsecommand(input()) - if(command == []): + try: + command = parsecommand(input()) + except KeyboardInterrupt: # bastard behavior from ed + pass + except EOFError: + break + if command == []: continue try: modules[command[0]] = importlib.import_module(command[0]) except ModuleNotFoundError as err: print(err) else: - file = modules[command[0]].main(len(command), command, file) + buffer = modules[command[0]].main(buffer, command) + return 0 if __name__ == "__main__": - return main(len(sys.argv), sys.argv) + sys.exit(main())