Skip to content

Instantly share code, notes, and snippets.

@littlecodersh
Created July 20, 2018 08:15
Show Gist options
  • Save littlecodersh/2ab4d88070eb16bf9f2b26f7cbc16ad0 to your computer and use it in GitHub Desktop.
Save littlecodersh/2ab4d88070eb16bf9f2b26f7cbc16ad0 to your computer and use it in GitHub Desktop.
Python 2&3 Windows Q:www.v2ex.com/t/472582
#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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment