python | 实现控制台效果

随便写的

import sys, time
import threading
import os
import msvcrt


class Cmd:
	def __init__(self):
		self.buff = b''           # buff string
		print('''Ready to Start | PYcmd by Mz1
========================================================''')
	def flush(self):
		sys.stdout.write(('
>>> ' + self.buff.decode('latin1') + ' '))
		sys.stdout.flush()
	def log(self, string):     # use to print on the screen
		sys.stdout.write(f'
{string}
')
		sys.stdout.flush()
		self.flush()
	def call(self, string):
		self.log(f'   > call {string}')
	def run(self):
		while True:
			while True:		# read byte
				self.flush()     # flush the buff
				c = msvcrt.getch()
				if c == b'
':
					break
				elif c == b'x08':     # backspace
					self.buff = self.buff[:-1]
				else:
					self.buff += c
			if self.buff != b'':
				threading.Thread(target=self.call, args=(self.buff.decode('latin1'),)).start()
			self.buff = b''
			
if __name__ == '__main__':
	cmd = Cmd()
	cmd.run()


原文地址:https://www.cnblogs.com/Mz1-rc/p/14393057.html