diff options
author | Erik Rose <erik@mozilla.com> | 2011-11-15 13:47:19 -0800 |
---|---|---|
committer | Erik Rose <erik@mozilla.com> | 2011-11-15 13:47:19 -0800 |
commit | e90c368aa79b55a86f77cb03fe071c3515ffbc6b (patch) | |
tree | 4aee6c7612a3ea2358fbdc4ffdd616b6c230373a | |
parent | 8c1e1a701429a2a832be6613c006d626a63dd2ac (diff) | |
download | blessings-e90c368aa79b55a86f77cb03fe071c3515ffbc6b.tar.gz |
Let location() operate on just an x *or* y coordinate.
Use force_styling in a test so we don't have to mock things anymore.
-rw-r--r-- | README.rst | 25 | ||||
-rw-r--r-- | blessings/__init__.py | 27 | ||||
-rw-r--r-- | blessings/tests.py | 23 |
3 files changed, 54 insertions, 21 deletions
@@ -110,7 +110,7 @@ certain pieces of formatting, even at the lowest level. You might notice that the above aren't the typical incomprehensible terminfo capability names; we alias a few of the harder-to-remember ones for -readability. However, **all** capabilities are available: you can reference any +readability. However, you aren't limited to these: you can reference any string-returning capability listed on the `terminfo man page`_ by the name under the "Cap-name" column: for example, ``rum``. @@ -155,10 +155,20 @@ pass the parameters right in:: from blessings import Terminal term = Terminal() - print term.create_window(1, 1, 20, 20) + print term.move(10, 1) -You can reference any string-returning capability listed on the `terminfo man -page`_ by the name under the "Cap-name" column. +Here are some of interest: + +``move`` + Position the cursor elsewhere. Parameters are y coordinate, then x + coordinate. +``move_x`` + Move the cursor to the given column. +``move_y`` + Move the cursor to the given row. + +You can also reference any other string-returning capability listed on the +`terminfo man page`_ by its name under the "Cap-name" column. .. _`terminfo man page`: http://www.manpagez.com/man/5/terminfo/ @@ -190,6 +200,12 @@ return: for example, when updating a progress bar at the bottom of the screen. print 'Here is the bottom.' print 'This is back where I came from.' +Parameters to ``location()`` are ``x`` and then ``y``, but you can also pass +just one of them, leaving the other alone. For example... :: + + with term.location(y=10): + print 'We changed just the row.' + Pipe Savvy ---------- @@ -252,6 +268,7 @@ Version History * Added the ``is_a_tty`` attribute for telling whether the output stream is a terminal. * Added sugar for the remaining simple formatting capabilities. + * Let ``location()`` operate on just an x *or* y coordinate. 1.0 * Extracted Blessings from nose-progressive, my `progress-bar-having, diff --git a/blessings/__init__.py b/blessings/__init__.py index 6d28f82..84d8fd5 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -86,7 +86,10 @@ class Terminal(object): restore='rc', clear_eol='el', - position='cup', + position='cup', # deprecated + move='cup', + move_x='hpa', + move_y='vpa', # You can use these if you want, but the named equivalents # like "red" and "bg_green" are probably easier. @@ -143,7 +146,7 @@ class Terminal(object): def width(self): return height_and_width()[1] - def location(self, x, y): + def location(self, x=None, y=None): """Return a context manager for temporarily moving the cursor Move the cursor to a certain position on entry, let you print stuff @@ -156,7 +159,7 @@ class Terminal(object): print 'I can do it %i times!' % x """ - return Location(x, y, self) + return Location(self, x, y) def _add_color_methods(): # Really, really private @@ -231,15 +234,25 @@ def height_and_width(): class Location(object): - """Context manager for temporarily moving the cursor""" - def __init__(self, x, y, term): + """Context manager for temporarily moving the cursor + + On construction, specify ``x`` to move to a certain column, ``y`` to move + to a certain row, or both. + + """ + def __init__(self, term, x=None, y=None): self.x, self.y = x, y self.term = term def __enter__(self): - """Save position and move to progress bar, col 1.""" + """Save position and move to the requested position.""" self.term.stream.write(self.term.save) # save position - self.term.stream.write(self.term.position(self.y, self.x)) + if self.x and self.y: + self.term.stream.write(self.term.move(self.y, self.x)) + elif self.x: + self.term.stream.write(self.term.move_x(self.x)) + elif self.y: + self.term.stream.write(self.term.move_y(self.y)) def __exit__(self, type, value, tb): self.term.stream.write(self.term.restore) # restore position diff --git a/blessings/tests.py b/blessings/tests.py index b45116e..df71192 100644 --- a/blessings/tests.py +++ b/blessings/tests.py @@ -54,19 +54,22 @@ def test_stream_attr(): def test_location(): """Make sure ``location()`` does what it claims.""" - # Let the Terminal grab the actual tty and call setupterm() so things work: - t = Terminal() - - # Then rip it away, replacing it with something we can check later: - output = t.stream = StringIO() + t = Terminal(stream=StringIO(), force_styling=True) with t.location(3, 4): - output.write('hi') + t.stream.write('hi') - eq_(output.getvalue(), tigetstr('sc') + - tparm(tigetstr('cup'), 4, 3) + - 'hi' + - tigetstr('rc')) + eq_(t.stream.getvalue(), tigetstr('sc') + + tparm(tigetstr('cup'), 4, 3) + + 'hi' + + tigetstr('rc')) + +def test_horizontal_location(): + """Make sure we can move the cursor horizontally without changing rows.""" + t = Terminal(stream=StringIO(), force_styling=True) + with t.location(x=5): + pass + eq_(t.stream.getvalue(), t.save + tparm(tigetstr('hpa'), 5) + t.restore) def test_null_fileno(): |