简体   繁体   English

Python将光标向前移动两个位置

[英]Python move cursor two places forward

I have written the program below which is intended to act as the terminal UI for a chat I am about to build for a university project. 我在下面编写了程序,该程序旨在用作我将要为大学项目建立的聊天的终端UI。 At its present state, the aim is to always have the last line act as the area to write a message, and when enter is pressed the message is written above and the last line becomes blank again. 在当前状态下,目标是始终使最后一行充当写入消息的区域,并且当按下回车键时,消息将被写入上方,并且最后一行再次变为空白。

That works fine if that's all I want. 如果这就是我想要的,那很好。 But, I also want to always have a number of "prompt symbols" at the start of the last line, namely here :> . 但是,我也希望在最后一行的开头始终有许多“提示符号”,即这里:> To make that happen, when enter is pressed,the whole current line is deleted, the bare message is printed, a newline character inserted, and finally I wish to print the :> in the new line and repeat. 为了实现这一点,当按下回车键时,将删除当前行的整个行,打印出空信息,插入换行符,最后我希望在新行中打印:>并重复。

What happens, however, is that the prompt string is indeed printed, but the cursor, after the first enter pressed, begins at the start of the line, which means any subsequent input will overwrite the prompt characters. 但是,发生的事情是确实打印了提示字符串,但是光标在第一次按下输入键之后才开始于行的开头,这意味着任何后续输入都将覆盖提示字符。 That does not happen the first time for some reason, where the first prompt is printed before anything else happens. 由于某种原因,这不会第一次发生,在任何其他情况发生之前先打印第一个提示。

So in the end, I would like for a way for the cursor to actually start after the two prompt characters when a newline is printed. 因此,最后,我希望有一种方法可以使光标在换行符打印后的两个提示字符之后实际开始。 This is all I want as regards to functionality of the terminal, therefore I would like to find an easy way to solve this and be done with it instead of meddling with ncurses library and the likes. 关于终端的功能,这就是我想要的全部,因此,我想找到一种简单的方法来解决此问题,并用它来完成,而不是干预ncurses库之类。 Thank you all for your time. 谢谢大家的时间。 The point of interest in the code where happens whatever I want to happen is inside the last while loop. 我想发生的任何事情都在代码中引起关注的一点是在最后一个while循环内。

The code should be run with Python3. 该代码应与Python3一起运行。

import sys
from string import printable
import termios
import tty

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
    screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()
class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            # ch = sys.stdin.read(1)
            ch = sys.stdin.read(1)[0]
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()

# enter: ord 13
#backspace: ord 127

current_input = ""
prompt_msg = ":> "

print(10*"\n"+prompt_msg,end="")

getch = _Getch()

def clear_input():
    linelen = (len(current_input)+len(prompt_msg))
    sys.stdout.write("\b"*linelen+" "*linelen+"\b"*linelen)
    sys.stdout.flush()


while(True):

    ch=getch()
    if ord(ch)==3:# ctrl+c
        exit()
    # >>>>>>>.POINT OF INTEREST<<<<<<<<<<<
    if ord(ch)==13:# enter
        clear_input()
        print(current_input)
        current_input = ""

        # print(prompt_msg,end="")
        sys.stdout.write(prompt_msg)
        sys.stdout.flush()

    if ord(ch)==127 and len(current_input)>0:# backspace
        sys.stdout.write("\b"+" "+"\b")
        sys.stdout.flush()
        current_input=current_input[:-1]
    if ch in printable or ord(ch)>127: # printable
        current_input+=ch
        sys.stdout.write(ch)
        sys.stdout.flush()

与其尝试使指针向前移动两个位置,我没有运气寻找答案;我只是在应该完成的每个位置中,从current_input字符串中删除了回车符(“ \\ r”)-流氓回车字符在字符串中似乎还存在,这导致了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM