[Python] Move cursor accross multiple lines in python

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    so i am trying to make a simple text editor to learn python , i am trying to learn by making some thing , yesterday i had asked about non blocking keyboard listening here and i solved that problem currently my code looks like this:

    '''
    imports: keyboard for listening to CTRL
    readline for navigating cursor in a line
    '''
    import readline
    # import keyboard
    from pynput import keyboard
    lines = ""

    def write(line_count , no_of_lines):
    '''
    function:write
    parameters:line_count,no_of_lines
    returns:none
    '''
    global lines
    print(line_count,no_of_lines)
    print(f"Default line count {no_of_lines}")
    print(f"Buffer Length {len(lines)}")
    print("press CTRL key to set line to 1")
    print("start typing")
    print("--------------------------------------")
    # def on_ctrl_event(event):
    # nonlocal line_count
    # if event.name == 'ctrl':
    # line_count = 1
    # keyboard.on_press(on_ctrl_event)
    def on_release(key):
    nonlocal line_count
    if key == keyboard.Key.ctrl:
    line_count = 1
    # with keyboard.Listener(on_release=on_press) as listener:listener.join()
    # non vlocking
    listener = keyboard.Listener(on_release=on_release)
    listener.start()
    while line_count > 0:
    lines += input() + "\n"
    line_count -= 1
    if line_count == 0:
    print("last line")
    try:
    inc_line = int(input("<line> or 0: "))
    except ValueError:
    inc_line = 0
    if inc_line > 0:
    no_of_lines += inc_line
    line_count += inc_line
    def main():
    '''
    function:main
    returns:none
    main entry point
    '''
    global lines
    while True:
    event = input("'n' to write ,'s' to save , 'v' to view buffer , 'q' to exit: ")
    if event == 's' and (len(lines)>1):
    filename = input("filename>")
    path = input("path>")
    try:
    with open(f"{path}{filename}", "w",encoding='utf-8') as file:
    file.write(lines)
    print("Saved the content\nBuffer is cleared")
    except (OSError, FileExistsError) as error:
    print(error)
    elif event == 'v' and len(lines) > 1:
    print(lines)
    elif event == 'q':
    break
    elif event == "n":
    lines = ""
    write(5,5)
    else:
    print(f"Current Buffer length: {len(lines)} must be > than 1")

    if __name__=="__main__":
    main()



    i wanna make my cursor to move accross multiple lines , currently it only moves in current line please help me

    i wanna make my cursor to move accross multiple lines , currently it only moves in current line please help me , like up arrow key makes it move up and vice versa with down arrow , left and right arrow works well because i found out that i can import readline , but currently when i press up arrow it just shows previously written buffer in previous line

    Continue reading...

Compartilhe esta Página