如何将角色移动到某个输入的索引

时间:2017-04-21 00:55:34

标签: python-3.x text-editor

我正在尝试创建自己的文本编辑器,要求输入文件并打印>然后可以将其移动到该行的指定索引,以便它可以删除一个字符。我不知道移动它的最佳方法。到目前为止我已经

def print_file(data, cursor):
"""Prints the file contents (data), with the cursor in the right place."""

# Variable fm will store the formatted representation
# of the file with position information, etc.
fm = ""
pos = 0

# Break the "file" (represented by a string) up into lines.
lines = data.split("\n")

for x, line in enumerate(lines):
    length = len(line)

    if length != 0:
        # Edge case for the end of the "file".
        if data[-1] != "\n" and x == len(lines) - 1:
            length -= 1


        fm += str("{0:70} |{1:3d}:{2:3d}|\n".format(line, pos, pos + length))

        # If the cursor points to char on this line add it. 
        if pos <= cursor <= pos + length:
            fm += " " * (cursor - pos)  + "^\n" 

    # Start at the next character.
    pos += length + 1
print(fm, end="")
#end of function 

def move_cursor(location):
    """Moves the cursor to the right location"""

#end of function

cursor = 0
is_open = False
is_modified = False
file = str(input(">open "))

#Open filename
file == "(filename)"
file = open(file, "r")
file_data = file.read()
print_file(file_data, cursor)
command = str(input("> "))

#Move position
if command == "move(int)":
    move_cursor(location)

我认为最好的方法是创建一个函数,然后在循环中调用它,但我不确定如何实际让它移动到索引....

0 个答案:

没有答案