From 0419e5ad9035e11f46dc6950f4ce97b9d1eaf0b6 Mon Sep 17 00:00:00 2001 From: alicestrt Date: Wed, 10 Apr 2019 16:31:14 +0200 Subject: [PATCH] first commit --- overunder.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++ text/yourfile.txt | 6 +++ 2 files changed, 99 insertions(+) create mode 100755 overunder.py create mode 100644 text/yourfile.txt diff --git a/overunder.py b/overunder.py new file mode 100755 index 0000000..9669889 --- /dev/null +++ b/overunder.py @@ -0,0 +1,93 @@ +import linecache +import textwrap +import sys +from sys import exit +import os +from os import path + +class LeavingProgram(Exception): + pass + +def parse(program): + cmds = program.split(',') + splitted_cmds = [] + for cmd in cmds: + splitted = cmd.split() + splitted_cmds.append(splitted) + return splitted_cmds + +def tokenize(s): + return s.split() + +def repl(): + while True: + try: + val = eval(parse(input('> '))) + if val is not None: + print(val) + except LeavingProgram: + break + +text = None +line_number = 0 +last_index = 0 +def eval(cmds): + global text + global line_number + global last_index + global pattern + + for cmd in cmds: + if cmd == []: + line_number += 1 + last_index = 0 + + elif cmd[0] == 'load': + contents = open('text/yourfile.txt').read() + text = textwrap.wrap(contents, 40, break_long_words=True) + print('\n'.join(text)) + line_number = 0 + last_index = 0 + + elif cmd[0] == 'show': + print(text[line_number]) + + elif cmd[0] == 'under': + current_line = text[line_number] + char_number = int(cmd[1]) - 1 + char_list = list(current_line) + + x=range(last_index, char_number + last_index + 1) + for time in x: + if time < len(char_list): + char_list[time] = u'\u21e2' + + last_index += char_number + 1 + + joined = ''.join(char_list) + text[line_number] = joined + + elif cmd[0] == 'over': + last_index += int(cmd[1]) + + elif cmd[0] == 'pattern': + + pattern = text[0:line_number + 1] + print('\n'.join(pattern)) + + elif cmd[0] == 'save': + pattern = text[0:line_number + 1] + pattern_file = open('output/patternfile.txt', 'w') + pattern_file.write('\n'.join(pattern)) + pattern_file.close() + print('Your pattern has been saved in the output folder.') + + elif cmd[0] == 'quit': + print('Come back soon!') + raise LeavingProgram() + else: + joined = ' '.join(cmd) + print('Did not understand command {}'.format(joined)) + +if __name__ == '__main__': + repl() diff --git a/text/yourfile.txt b/text/yourfile.txt new file mode 100644 index 0000000..760850e --- /dev/null +++ b/text/yourfile.txt @@ -0,0 +1,6 @@ +this is a test +because I need to test +something + +hey hey +stuff