Created
July 20, 2018 08:15
-
-
Save littlecodersh/2ab4d88070eb16bf9f2b26f7cbc16ad0 to your computer and use it in GitHub Desktop.
Revisions
-
littlecodersh created this gist
Jul 20, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ #coding=utf8 import re, sys, time, os from ctypes import Structure, c_short, windll, byref try: from shutil import get_terminal_size except: from backports.shutil_get_terminal_size import get_terminal_size CN_REGEX = re.compile(u'[\u4e00-\u9fff]') CONTENT = [ u'+--------+------------------------------+------------+------------------+', u'| 代码 | 名称 | ******** | 更新时间 |', u'|--------+------------------------------+------------+------------------|', u'| 110022 | ************** | -0.{}% | 2018-07-20 10:39 |', u'+--------+------------------------------+------------+------------------+', ] def first_solution(): class Pos(Structure): _fields_ = [('X',c_short),('Y', c_short)] class Rect(Structure): _fields_ = [('Left',c_short),('Top',c_short),('Right',c_short),('Bottom',c_short)] class Screen(Structure): _fields_=[('Size',Pos),('CursorPosition',Pos),('Attributes',c_short),('Window',Rect),('MaximumWindowSize',Pos)] h = windll.kernel32.GetStdHandle(-11) s = Screen() windll.kernel32.GetConsoleScreenBufferInfo(h, byref(s)) current_position = s.CursorPosition print('\n'.join(CONTENT).format(11)) time.sleep(1) windll.kernel32.SetConsoleCursorPosition(h, current_position) print('\n'.join(CONTENT).format(22)) def second_solution(): os.system('') columns = get_terminal_size().columns line_number = sum([((len(l) + len(CN_REGEX.findall(l))) // columns + 1) for l in CONTENT]) print('\n'.join(CONTENT).format(11)) time.sleep(1) # ANSI_ESCAPE_CODE: https://en.m.wikipedia.org/wiki/ANSI_escape_code sys.stdout.write('\x1b[{}A\r'.format(line_number)) print('\n'.join(CONTENT).format(22)) first_solution()