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
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
Share Improve this question asked Feb 7 at 3:24 IllTime00qwIllTime00qw 1325 bronze badges1 Answer
Reset to default 1There is no simple answer to your question as your goal involves many different layers of of how a terminal can be interacted with.
Your issue occurs because the up/down arrow keys in a terminal typically interact with the shell's input history instead of moving the cursor within your text buffer. To handle this properly, you need to use terminal control codes (VT100 escape sequences).
1. Terminal Control Codes (VT100)
VT100 escape codes allow you to manipulate the cursor position in the terminal. You can read more about them on wikipedia. However, note that not all terminal emulators support every code—see Microsoft’s VT100 support for Windows terminal-specific details as an example
2. Example VT100 Sequences in Python
You can use sys.stdout.write() to send raw escape codes and sys.stdin.read(1) to capture key presses.
import sys
# Move cursor up one line
def move_cursor_up():
sys.stdout.write("\033[1A")
sys.stdout.flush()
# Save current cursor position
def save_cursor_position():
sys.stdout.write("\033[s")
sys.stdout.flush()
# Restore saved cursor position
def restore_cursor_position():
sys.stdout.write("\033[u")
sys.stdout.flush()
# Invert text color (swap foreground/background)
def invert_colors():
sys.stdout.write("\033[7m")
sys.stdout.flush()
# Reset text formatting
def reset_format():
sys.stdout.write("\033[0m")
sys.stdout.flush()
3. Handling Arrow Keys in Python
To read arrow key inputs, use sys.stdin.read() in raw mode with termios (Unix) or msvcrt.getch() (Windows). See this cross platform example about reading single character inputs: stackoverflow
Final notes
You might want to look into complex python applications involving terminal control to gain understanding about implementing them. See the pyvim project as an example: github