-
-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save ('s') and restore ('u') support added #137
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,11 +76,13 @@ def style(self, style=None, on_stderr=False): | |
def set_console(self, attrs=None, on_stderr=False): | ||
if attrs is None: | ||
attrs = self.get_attrs() | ||
handle = win32.STDOUT | ||
if on_stderr: | ||
handle = win32.STDERR | ||
handle = self.get_handle(on_stderr) | ||
win32.SetConsoleTextAttribute(handle, attrs) | ||
|
||
@staticmethod | ||
def get_handle(on_stderr=False): | ||
return win32.STDERR if on_stderr else win32.STDOUT | ||
|
||
def get_position(self, handle): | ||
position = win32.GetConsoleScreenBufferInfo(handle).dwCursorPosition | ||
# Because Windows coordinates are 0-based, | ||
|
@@ -89,31 +91,38 @@ def get_position(self, handle): | |
position.Y += 1 | ||
return position | ||
|
||
def get_cursor_position(self, on_stderr=False, adjust=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we need this function. We already have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sensitive to wiggin's criticism here which I think might be valid, but am enthusiastic about the PR overall. Assuming @kgeorgiy is not still around / motivated after 4 years, I'll take a closer look at this code tonight, and try to merge it... Apologies for resurrecting this after ignoring it for years! |
||
handle = self.get_handle(on_stderr) | ||
info = win32.GetConsoleScreenBufferInfo(handle) | ||
position = info.dwCursorPosition | ||
# Because Windows coordinates are 0-based, | ||
# and win32.SetConsoleCursorPosition expects 1-based. | ||
y, x = position.Y + 1, position.X + 1 | ||
if adjust: | ||
window = info.srWindow | ||
y -= window.Top | ||
x -= window.Left | ||
return y, x | ||
|
||
def set_cursor_position(self, position=None, on_stderr=False): | ||
if position is None: | ||
# I'm not currently tracking the position, so there is no default. | ||
# position = self.get_position() | ||
return | ||
handle = win32.STDOUT | ||
if on_stderr: | ||
handle = win32.STDERR | ||
handle = self.get_handle(on_stderr) | ||
win32.SetConsoleCursorPosition(handle, position) | ||
|
||
def cursor_adjust(self, x, y, on_stderr=False): | ||
handle = win32.STDOUT | ||
if on_stderr: | ||
handle = win32.STDERR | ||
position = self.get_position(handle) | ||
adjusted_position = (position.Y + y, position.X + x) | ||
(cy, cx) = self.get_cursor_position(on_stderr, adjust=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to continue to use get_position instead of the new get_cursor_position, we will not need this change. |
||
adjusted_position = (cy + y, cx + x) | ||
handle = self.get_handle(on_stderr) | ||
win32.SetConsoleCursorPosition(handle, adjusted_position, adjust=False) | ||
|
||
def erase_screen(self, mode=0, on_stderr=False): | ||
# 0 should clear from the cursor to the end of the screen. | ||
# 1 should clear from the cursor to the beginning of the screen. | ||
# 2 should clear the entire screen, and move cursor to (1,1) | ||
handle = win32.STDOUT | ||
if on_stderr: | ||
handle = win32.STDERR | ||
handle = self.get_handle(on_stderr) | ||
csbi = win32.GetConsoleScreenBufferInfo(handle) | ||
# get the number of character cells in the current buffer | ||
cells_in_screen = csbi.dwSize.X * csbi.dwSize.Y | ||
|
@@ -140,9 +149,7 @@ def erase_line(self, mode=0, on_stderr=False): | |
# 0 should clear from the cursor to the end of the line. | ||
# 1 should clear from the cursor to the beginning of the line. | ||
# 2 should clear the entire line. | ||
handle = win32.STDOUT | ||
if on_stderr: | ||
handle = win32.STDERR | ||
handle = self.get_handle(on_stderr) | ||
csbi = win32.GetConsoleScreenBufferInfo(handle) | ||
if mode == 0: | ||
from_coord = csbi.dwCursorPosition | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import print_function | ||
import fixpath | ||
import colorama | ||
import sys | ||
import time | ||
|
||
# Demonstrate cursor saving, restoring and positioning: SAVE, RESTORE and POS in colorama.Cursor | ||
|
||
save = colorama.Cursor.SAVE | ||
restore = colorama.Cursor.RESTORE | ||
pos = colorama.Cursor.POS | ||
|
||
blue = colorama.Back.BLUE | ||
reset = colorama.Back.RESET | ||
|
||
def main(): | ||
""" | ||
expected output: | ||
Current state is shown at top | ||
Progress is shown at the current cursor position | ||
""" | ||
colorama.init() | ||
for i in range(1, 10): | ||
sys.stdout.write("Step {}: ".format(i)) | ||
for j in range(1, 10): | ||
sys.stdout.write(str(j)) | ||
sys.stdout.write(save() + pos(10, 1) + blue + " State {}.{} ".format(i, j) + restore() + reset) | ||
sys.stdout.flush() | ||
time.sleep(0.02) | ||
print() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this ^