Commits

Jonathan Eunice  committed e8d62ce

adjusted TextRange constructor and to take negative indices

  • Participants
  • Parent commits 636d4f2

Comments (0)

Files changed (3)

File say/textrange.py

 """
 A range of say.text.Text lines that can be edited on their own,
-effecting the underlying lines.
+effecting the underlying lines. If the underlying text object
+changes (especially changing the number of lines somewhere
+before or overlapping whether the text range lies), TextRange
+will probably become confused. However, edits made through the
+TextRange are completely fine.
 """
 
 from say import Text
 
 class TextRange(object):
     
-    def __init__(self, text=None, indices=None):
+    def __init__(self, text=None, start=None, stop=None):
+        """
+        Create a TextRange object on the given text, over the given range.
+        Just as with Python list specifications, the stop value should be
+        one more than the last index.
+        """
         self._text = text
-        self._indices = slice(*indices)
-        
-    # TODO: Convert this constructor to take start stop rather than tuple to specify indicates - also, support open ended ranges
+        if stop is None:
+            stop = len(text)
+        elif stop < 0:
+            stop = len(text) + stop
+        if start is None:
+            start = 0
+        elif start < 0:
+            start = len(text) + start
+        self._indices = slice(start, stop)
 
     def __iadd__(self, data):
         """
         """
         Check to the given index n to ensure it's within the range of lines.
         """
-        index = self._indices.start + n
+        index = self._indices.start + n if n >= 0 else self._indices.stop + n
         if self._indices.start <= index < self._indices.stop:
             return index
         raise IndexError('index {0} ({1} in underlying Text) out of range'.format(n, index))
 
 setup(
     name='say',
-    version=verno("0.923"),
+    version=verno("0.925"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Super-simple templated printing. E.g.: say("Hello, {whoever}!", indent=1)',

File test/test_textrange.py

    t = Text('a\nb\nc\nd')
    assert t.lines == 'a b c d'.split()
    
-   tr = TextRange(t, (1,3))
+   tr = TextRange(t, 1, 3)
    assert tr.text == 'b\nc'
    assert tr.lines == [ 'b', 'c' ]
    
    t = Text('a\nb\nc\nd')
    assert t.lines == 'a b c d'.split()
    
-   tr = TextRange(t, (1,3))
+   tr = TextRange(t, 1, 3)
    assert tr.text == 'b\nc'
    assert tr.lines == [ 'b', 'c' ]
    
    t = Text('a\na\n\a\nb')
    assert t.lines == 'a a a b'.split()
    
-   tr = TextRange(t, (2,4))
+   tr = TextRange(t, 2, 4)
    assert tr.lines == 'a b'.split()
    
    tr.replace('a', 'A')
    t = Text("Joe,\nthis is {x}")
    assert t.text == 'Joe,\nthis is 21'
    
-   tr = TextRange(t, (1, 2))
+   tr = TextRange(t, 1, 3)
    
    tr += 'and {x}'
    assert tr.text == 'this is 21\nand 21'
 def test_replace():
    t = Text('a\nb\nc\nd')
    assert t.lines == 'a b c d'.split()
-   tr = TextRange(t, (1,3))
+   tr = TextRange(t, 1, 3)
    assert tr.lines == [ 'b', 'c' ]
    
    tr.replace('b', 'B')