def test_container_1(): c = Container() b = TextBlock(1,1) c.add_component(b, 1, 1) assert c.size() == (2,2) b.resize(3,3) assert c.size() == (4,4) b[3,3] = 'x' assert c.size() == (5,5) assert c[4,4] == 'x'
def __init__(self, nrows=1, ncols=1, borders=(1, 1, 1, 1), content_pad=(0, 0, 0, 0), maxrows=10000, maxcols=1000, fixrows=False, fixcols=False, wrap=True, wrap_hyphen=True): """ Instantiates a Cell, borders sets type of borders, set 0 for no borders. content_pad sets what happens when no borders is used, 1 means that :param nrows: :param ncols: :param borders: :param content_pad: :param maxrows: :param maxcols: :param fixrows: :param fixcols: :param wrap: :param wrap_hyphen: """ self.borders = borders self.isnt_border = [1 if border == 0 else 0 for border in borders] self.content_pad = content_pad super(Cell, self).__init__(nrows + 2, ncols + 2, fixrows, fixcols, maxrows + 2, maxcols + 2, wrap) self._content = TextBlock(nrows, ncols, fixrows, fixcols, maxrows, maxcols, wrap, wrap_hyphen) self.c = self._content orient = ['top', 'bot', 'left', 'right'] self._borders = [ CellBorder(orient[i], borders[i], self._content, fixrows, fixcols, maxrows + 2, maxcols + 2) for i in range(4) ] self.add_component(self._content, 1, 1) self.add_component(self._borders[0], -1, -1, self._content, 'topleft') # TOP self.add_component(self._borders[1], 1, -1, self._content, 'botleft') # BOT self.add_component(self._borders[2], -1, -1, self._content, 'topleft') # LEFT self.add_component(self._borders[3], -1, 1, self._content, 'topright') # RIGHT self.rowix = 0
def test_container_2(): c = Container() b = TextBlock(1,1,maxcols=3) c.add_component(b, 1, 1) b2 = TextBlock(1,1,maxcols=3) c.add_component(b2, 1, 0, b, 'botleft') b.add_line(0, 'abcd') b2.add_line(0, 'efgh') assert c[:,:] == [' ',' ab-',' cd ',' ef-',' gh ']
""" self[:, :] = ' ' for cc in self.components: row, col = self.get_absolute_position(cc) self[row:row + cc.c.get_nrows(), col:col + cc.c.get_ncols()] = cc.c.get_lines() def _get_lines(self): self._render_components() return super(Container, self)._get_lines() def __getitem__(self, index): self._render_components() return super(Container, self).__getitem__(index) # def __setitem__(self, index, value): if __name__ == '__main__': c = Container() b = TextBlock(1, 1, maxcols=3) c.add_component(b, 1, 1) b2 = TextBlock(1, 1, maxcols=3) c.add_component(b2, 1, 0, b, 'botleft') b.add_line(0, 'abcd') b2.add_line(0, 'efgh') print c # assert c[:, :] == [' ', ' ab-', ' cd ', ' ef-', ' gh ']
def test_text_block_3(): b = TextBlock(3, 3, fixrows=False, fixcols=True, maxrows=5, wrap=True) b.add_lines(0, ['abcd', 'efgh', 'ijkl']) assert b[:, :] == ['ab-', 'cd ', 'ef-', 'gh ', 'ij-']
def test_text_block_2(text_block2): b = TextBlock(3, 3, fixrows=False, fixcols=True, maxrows=5, wrap=True) b.add_line(0, 'abcdefg') assert b[:, :] == ['ab-', 'cd-', 'efg'] b = TextBlock(3, 3, fixrows=False, fixcols=True, maxrows=5, wrap=True) b.add_line(0, 'abcdefgh') assert b[:, :] == ['ab-', 'cd-', 'ef-', 'gh '] b = TextBlock(3, 3, fixrows=False, fixcols=True, maxrows=5, wrap=True) b.add_line(0, 'abcdefghijklmnop') assert b[:, :] == ['ab-', 'cd-', 'ef-', 'gh-', 'ij-'] b = TextBlock(3, 3, fixrows=False, fixcols=True, maxrows=5, wrap=True, wrap_hyphen=False) b.add_line(0, 'abcdefghijklmnop') assert b[:, :] == ['abc', 'def', 'ghi', 'jkl', 'mno'] b = TextBlock(3, 3, fixrows=False, fixcols=False, maxcols=1000, wrap=True) b.add_line(0, 'abcdefg') assert b[:, :] == ['abcdefg', ' ', ' '] b.add_line(1, '12345678') assert b[:, :] == ['abcdefg ', '12345678', ' ']
def text_block2(): b = TextBlock(3, 3, False, True, maxrows=5, wrap=True) return b
def text_block1(): block = TextBlock(3, 5, fixrows=True, fixcols=True, wrap=False) return block
class Cell(Container): def __init__(self, nrows=1, ncols=1, borders=(1, 1, 1, 1), content_pad=(0, 0, 0, 0), maxrows=10000, maxcols=1000, fixrows=False, fixcols=False, wrap=True, wrap_hyphen=True): """ Instantiates a Cell, borders sets type of borders, set 0 for no borders. content_pad sets what happens when no borders is used, 1 means that :param nrows: :param ncols: :param borders: :param content_pad: :param maxrows: :param maxcols: :param fixrows: :param fixcols: :param wrap: :param wrap_hyphen: """ self.borders = borders self.isnt_border = [1 if border == 0 else 0 for border in borders] self.content_pad = content_pad super(Cell, self).__init__(nrows + 2, ncols + 2, fixrows, fixcols, maxrows + 2, maxcols + 2, wrap) self._content = TextBlock(nrows, ncols, fixrows, fixcols, maxrows, maxcols, wrap, wrap_hyphen) self.c = self._content orient = ['top', 'bot', 'left', 'right'] self._borders = [ CellBorder(orient[i], borders[i], self._content, fixrows, fixcols, maxrows + 2, maxcols + 2) for i in range(4) ] self.add_component(self._content, 1, 1) self.add_component(self._borders[0], -1, -1, self._content, 'topleft') # TOP self.add_component(self._borders[1], 1, -1, self._content, 'botleft') # BOT self.add_component(self._borders[2], -1, -1, self._content, 'topleft') # LEFT self.add_component(self._borders[3], -1, 1, self._content, 'topright') # RIGHT self.rowix = 0 def get_nrows(self): return self._nrows - self.isnt_border[0] - self.isnt_border[1] def get_ncols(self): return self._ncols - self.isnt_border[2] - self.isnt_border[3] def add_fill_line(self): """ adds an empty line at the bottom of the cell, can be overridden for custom behavior :return: """ self._content[self._content.get_nrows()] = ' ' def add_fill_column(self): """ adds an empty column at the rightmost of the cell, can be overridden for custom behavior :return: """ self._content[:, self._content.get_ncols()] = ' ' def add_line(self, line=''): """ adds a line to the next row of the cell :param line: :return: """ if line == '': line = ' ' self.rowix += self.c.add_line(self.rowix, line) def _get_lines(self): u = self.isnt_border[0] d = self._nrows - self.isnt_border[1] l = self.isnt_border[2] r = self._ncols - self.isnt_border[3] return self[u:d, l:r]