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(('\r>>> ' + self.buff.decode('latin1') + ' '))
		sys.stdout.flush()
	def log(self, string):     # use to print on the screen
		sys.stdout.write(f'\r{string}\n')
		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'\r':
					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()


posted @ 2021-02-09 15:15  Mz1  阅读(396)  评论(0编辑  收藏  举报