Commits

Ronny Pfannschmidt committed 9fc3642

add buffer slicing

Comments (0)

Files changed (2)

tests/test_engine.py

     buffer.text = "one line"
     assert len(buffer) == 1
 
+
+def test_buffer_slice(engine):
+    buffer = engine.create_buffer()
+    buffer.text = "Test text\n\nline2"
+    assert len(buffer) == 3
+
+    assert buffer[1] == "\n"
+
+    print repr(buffer.text)
+
+    buffer[1] = "Test\n"
+
+    print repr(buffer.text)
+
+    assert buffer[1] == "Test\n"
+    assert buffer[2] == "line2"
     def __len__(self):
         return self.text_buffer.get_line_count()
 
+    def __getitem__(self, item):
+        start = self.text_buffer.get_iter_at_line(item)
+        if item+1 < len(self):
+            end = self.text_buffer.get_iter_at_line(item +1)
+        else:
+            end = self.text_buffer.get_end_iter()
+        return self.text_buffer.get_slice(start, end)
+
+    def __setitem__(self, item, value):
+        start = self.text_buffer.get_iter_at_line(item)
+        end = self.text_buffer.get_iter_at_line(item +1)
+
+        self.text_buffer.select_range(start, end)
+        self.text_buffer.delete_selection(False, False)
+
+        start = self.text_buffer.get_iter_at_line(item)
+        self.text_buffer.insert(start, value)
+
+
 class Engine(object):
 
     def create_buffer(self):