最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

command line interface - Python multiline prompt - Stack Overflow

programmeradmin2浏览0评论

I have been trying to change my prompt in sys.ps1 with PYTHONSTARTUP. My idea was that I will show some information on first line (time, path) and the prompt on the second. When I tried changing ps1 to a multiline string the first line would overwrite previous line in the terminal every time I typed a character¹:

>>> sys.ps1 = "foo\nbar>"
foo
foo
foo
foo
foo
foo
foo
bar>testing

Then I tried using a class with __repr__ function which will print the first line and return the second, but that resulted in the first line just adding onto the previous one¹:

[first line]
PY> [first line]
PY> t[first line]
PY> te[first line]
PY> tes[first line]
PY> test[first line]
PY> testi[first line]
PY> testin[first line]
PY> testing

This is the code that I have so far (if you need it):

import sys
import datetime
import threading
import os
import ctypes
from BlurWindow.blurWindow import GlobalBlur as gb__
gb__(ctypes.windll.user32.GetForegroundWindow(), '#000000CC', True, True)
c__ = "\033[38;5;"
e__ = "\033[m"
sys.ps2 = f"{c__}125m... {e__}"
if ctypes.windll.shell32.IsUserAnAdmin() != 0:
    h = "#"
else: 
    h = ">"


class ps1__:
    def __repr__(self):
        time = datetime.datetime.now().strftime("%H:%M:%S")
        path = os.path.abspath(os.getcwd())
        print(f"{c__}208m{path} {c__}106m{time}{e__}")
        return f"{c__}125mPY{h} {e__}"


sys.ps1 = ps1__()

Update: adding an extra line break to the start of the first line made it only repeat, and not add/replace the prev. one¹:

[first line]
PY>
[first line]
PY> t
[first line]
PY> te
[first line]
PY> tes
[first line]
PY> test
[first line]
PY> testi
[first line]
PY> testin
[first line]
PY> testing

My guess the only thing left is to check if the user executed some command and only then print the first line.

¹ - Note: I did not press enter in that example once
Another note: This is all did in the python terminal (or how do you call it, python in cmd)

发布评论

评论列表(0)

  1. 暂无评论