Skip to content

Commit

Permalink
Switched from xrange to range -- to be Python3.2 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
superbobry committed Jun 24, 2011
1 parent 802cc02 commit aa6e43e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 54 deletions.
70 changes: 39 additions & 31 deletions pyte/screens.py
Expand Up @@ -37,6 +37,14 @@
from . import modes as mo, graphics as g, charsets as cs


try:
xrange
except NameError:
pass
else:
range = xrange


def take(n, iterable):
"""Returns first n items of the iterable as a list."""
return list(islice(iterable, n))
Expand Down Expand Up @@ -168,7 +176,7 @@ def reset(self):
:manpage:`xterm` -- we now know that.
"""
self[:] = (take(self.columns, self.default_line)
for _ in xrange(self.lines))
for _ in range(self.lines))
self.mode = set([mo.DECAWM, mo.DECTCEM, mo.LNM, mo.DECTCEM])
self.margins = Margins(0, self.lines - 1)

Expand All @@ -181,7 +189,7 @@ def reset(self):
# From ``man terminfo`` -- "... hardware tabs are initially
# set every `n` spaces when the terminal is powered up. Since
# we aim to support VT102 / VT220 and linux -- we use n = 8.
self.tabstops = set(xrange(7, self.columns, 8))
self.tabstops = set(range(7, self.columns, 8))

self.cursor = Cursor(0, 0)
self.cursor_position()
Expand Down Expand Up @@ -213,7 +221,7 @@ def resize(self, lines=None, columns=None):
# size, add lines to the bottom.
if diff < 0:
self.extend(take(self.columns, self.default_line)
for _ in xrange(diff, 0))
for _ in range(diff, 0))
# b) if the current display size is greater than requested
# size, take lines off the top.
elif diff > 0:
Expand All @@ -225,7 +233,7 @@ def resize(self, lines=None, columns=None):
# a) if the current display size is less than the requested
# size, expand each line to the new size.
if diff < 0:
for y in xrange(lines):
for y in range(lines):
self[y].extend(take(abs(diff), self.default_line))
# b) if the current display size is greater than requested
# size, trim each line from the right to the new size.
Expand Down Expand Up @@ -474,8 +482,8 @@ def insert_lines(self, count=None):

# If cursor is outside scrolling margins it -- do nothin'.
if top <= self.cursor.y <= bottom:
# v +1, because xrange() is exclusive.
for line in xrange(self.cursor.y, min(bottom + 1, self.cursor.y + count)):
# v +1, because range() is exclusive.
for line in range(self.cursor.y, min(bottom + 1, self.cursor.y + count)):
self.pop(bottom)
self.insert(line, take(self.columns, self.default_line))

Expand All @@ -495,7 +503,7 @@ def delete_lines(self, count=None):
# If cursor is outside scrolling margins it -- do nothin'.
if top <= self.cursor.y <= bottom:
# v -- +1 to include the bottom margin.
for _ in xrange(min(bottom - self.cursor.y + 1, count)):
for _ in range(min(bottom - self.cursor.y + 1, count)):
self.pop(self.cursor.y)
self.insert(bottom, list(
repeat(self.cursor.attrs, self.columns)))
Expand All @@ -512,7 +520,7 @@ def insert_characters(self, count=None):
"""
count = count or 1

for _ in xrange(min(self.columns - self.cursor.y, count)):
for _ in range(min(self.columns - self.cursor.y, count)):
self[self.cursor.y].insert(self.cursor.x, self.cursor.attrs)
self[self.cursor.y].pop()

Expand All @@ -526,7 +534,7 @@ def delete_characters(self, count=None):
"""
count = count or 1

for _ in xrange(min(self.columns - self.cursor.x, count)):
for _ in range(min(self.columns - self.cursor.x, count)):
self[self.cursor.y].pop(self.cursor.x)
self[self.cursor.y].append(self.cursor.attrs)

Expand All @@ -546,7 +554,7 @@ def erase_characters(self, count=None):
"""
count = count or 1

for column in xrange(self.cursor.x, min(self.cursor.x + count, self.columns)):
for column in range(self.cursor.x, min(self.cursor.x + count, self.columns)):
self[self.cursor.y][column] = self.cursor.attrs

def erase_in_line(self, type_of=0, private=False):
Expand All @@ -565,12 +573,12 @@ def erase_in_line(self, type_of=0, private=False):
interval = (
# a) erase from the cursor to the end of line, including
# the cursor,
xrange(self.cursor.x, self.columns),
range(self.cursor.x, self.columns),
# b) erase from the beginning of the line to the cursor,
# including it,
xrange(0, self.cursor.x + 1),
range(0, self.cursor.x + 1),
# c) erase the entire line.
xrange(0, self.columns)
range(0, self.columns)
)[type_of]

for column in interval:
Expand All @@ -593,17 +601,17 @@ def erase_in_display(self, type_of=0, private=False):
interval = (
# a) erase from cursor to the end of the display, including
# the cursor,
xrange(self.cursor.y + 1, self.lines),
range(self.cursor.y + 1, self.lines),
# b) erase from the beginning of the display to the cursor,
# including it,
xrange(0, self.cursor.y),
range(0, self.cursor.y),
# c) erase the whole display.
xrange(0, self.lines)
range(0, self.lines)
)[type_of]

for line in interval:
self[line][:] = \
(self.cursor.attrs for _ in xrange(self.columns))
(self.cursor.attrs for _ in range(self.columns))

# In case of 0 or 1 we have to erase the line with the cursor.
if type_of in [0, 1]:
Expand Down Expand Up @@ -801,20 +809,20 @@ def __init__(self, *args):

def set_mode(self, *modes, **kwargs):
if mo.DECSCNM >> 5 in modes and kwargs.get("private"):
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))
super(DiffScreen, self).set_mode(*modes, **kwargs)

def reset_mode(self, *modes, **kwargs):
if mo.DECSCNM >> 5 in modes and kwargs.get("private"):
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))
super(DiffScreen, self).reset_mode(*modes, **kwargs)

def reset(self):
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))
super(DiffScreen, self).reset()

def resize(self, *args, **kwargs):
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))
super(DiffScreen, self).resize(*args, **kwargs)

def draw(self, *args):
Expand All @@ -823,22 +831,22 @@ def draw(self, *args):

def index(self):
if self.cursor.y == self.margins.bottom:
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))

super(DiffScreen, self).index()

def reverse_index(self):
if self.cursor.y == self.margins.top:
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))

super(DiffScreen, self).reverse_index()

def insert_lines(self, *args):
self.dirty.update(xrange(self.cursor.y, self.lines))
self.dirty.update(range(self.cursor.y, self.lines))
super(DiffScreen, self).insert_lines(*args)

def delete_lines(self, *args):
self.dirty.update(xrange(self.cursor.y, self.lines))
self.dirty.update(range(self.cursor.y, self.lines))
super(DiffScreen, self).delete_lines(*args)

def insert_characters(self, *args):
Expand All @@ -859,14 +867,14 @@ def erase_in_line(self, *args):

def erase_in_display(self, type_of=0):
self.dirty.update((
xrange(self.cursor.y + 1, self.lines),
xrange(0, self.cursor.y),
xrange(0, self.lines)
range(self.cursor.y + 1, self.lines),
range(0, self.cursor.y),
range(0, self.lines)
)[type_of])
super(DiffScreen, self).erase_in_display(type_of)

def alignment_display(self):
self.dirty.update(xrange(self.lines))
self.dirty.update(range(self.lines))
super(DiffScreen, self).alignment_display()


Expand Down Expand Up @@ -987,7 +995,7 @@ def prev_page(self):
._replace(position=self.history.position - self.lines)

self[:] = list(reversed([
self.history.top.pop() for _ in xrange(mid)
self.history.top.pop() for _ in range(mid)
])) + self[:-mid]

self.ensure_width()
Expand All @@ -1008,7 +1016,7 @@ def next_page(self):
._replace(position=self.history.position + self.lines)

self[:] = self[mid:] + [
self.history.bottom.popleft() for _ in xrange(mid)
self.history.bottom.popleft() for _ in range(mid)
]

self.ensure_width()
24 changes: 12 additions & 12 deletions tests/test_diff.py
Expand Up @@ -14,22 +14,22 @@ def test_mark_whole_screen():
# a) init.
assert hasattr(screen, "dirty")
assert isinstance(screen.dirty, set)
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))

# b) reset().
screen.dirty.clear()
screen.reset()
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))

# c) resize().
screen.dirty.clear()
screen.resize()
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))

# d) alignment_display().
screen.dirty.clear()
screen.alignment_display()
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))


def test_mark_single_line():
Expand All @@ -56,11 +56,11 @@ def test_modes():

screen.dirty.clear()
screen.set_mode(mo.DECSCNM >> 5, private=True)
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))

screen.dirty.clear()
screen.reset_mode(mo.DECSCNM >> 5, private=True)
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))


def test_index():
Expand All @@ -74,7 +74,7 @@ def test_index():
# b) whole screen is dirty.
screen.cursor_to_line(24)
screen.index()
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))


def test_reverse_index():
Expand All @@ -83,7 +83,7 @@ def test_reverse_index():

# a) not at the top margin -- whole screen is dirty.
screen.reverse_index()
assert screen.dirty == set(xrange(screen.lines))
assert screen.dirty == set(range(screen.lines))

# b) nothing is marked dirty.
screen.dirty.clear()
Expand All @@ -99,7 +99,7 @@ def test_insert_delete_lines():
for method in ["insert_lines", "delete_lines"]:
screen.dirty.clear()
getattr(screen, method)()
assert screen.dirty == set(xrange(screen.cursor.y, screen.lines))
assert screen.dirty == set(range(screen.cursor.y, screen.lines))


def test_erase_in_display():
Expand All @@ -109,14 +109,14 @@ def test_erase_in_display():
# a) from cursor to the end of the screen.
screen.dirty.clear()
screen.erase_in_display()
assert screen.dirty == set(xrange(screen.cursor.y, screen.lines))
assert screen.dirty == set(range(screen.cursor.y, screen.lines))

# b) from the begining of the screen to cursor.
screen.dirty.clear()
screen.erase_in_display(1)
assert screen.dirty == set(xrange(0, screen.cursor.y + 1))
assert screen.dirty == set(range(0, screen.cursor.y + 1))

# c) whole screen.
screen.dirty.clear()
screen.erase_in_display(2)
assert screen.dirty == set(xrange(0, screen.lines))
assert screen.dirty == set(range(0, screen.lines))

0 comments on commit aa6e43e

Please sign in to comment.