Skip to content

Commit

Permalink
Merge pull request #29 from vitek/master
Browse files Browse the repository at this point in the history
contextmanagers: Put state restore code into finally block
  • Loading branch information
erikrose committed Jul 30, 2013
2 parents 14ba650 + 940f60f commit 74e89a5
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions blessings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,29 @@ def location(self, x=None, y=None):
self.stream.write(self.move_x(x))
elif y is not None:
self.stream.write(self.move_y(y))
yield

# Restore original cursor position:
self.stream.write(self.restore)
try:
yield
finally:
# Restore original cursor position:
self.stream.write(self.restore)

@contextmanager
def fullscreen(self):
"""Return a context manager that enters fullscreen mode while inside it and restores normal mode on leaving."""
self.stream.write(self.enter_fullscreen)
yield
self.stream.write(self.exit_fullscreen)
try:
yield
finally:
self.stream.write(self.exit_fullscreen)

@contextmanager
def hidden_cursor(self):
"""Return a context manager that hides the cursor while inside it and makes it visible on leaving."""
self.stream.write(self.hide_cursor)
yield
self.stream.write(self.normal_cursor)
try:
yield
finally:
self.stream.write(self.normal_cursor)

@property
def color(self):
Expand Down

0 comments on commit 74e89a5

Please sign in to comment.