def gen_blocks(): # Convert the characters to their coloured form for key, block in blocks.items(): # Get supported version of block char blocks[key]['char'] = supported_chars(*block['char']) # If bg is transparent, it must be coloured at runtime. if blocks[key]['colours']['bg'] is not None: blocks[key]['char'] = colour_str( blocks[key]['char'], block['colours']['fg'], block['colours']['bg'], block['colours']['style'] ) return blocks
def gen_blocks(blocks): for key, block in blocks.items(): # Get supported version of block char blocks[key]['char'] = supported_chars(*block['char'])
def render_grid(title, selected, grid, blocks, max_height, sel=None): h, v, tl, t, tr, l, m, r, bl, b, br = \ supported_chars('─│╭┬╮├┼┤╰┴╯', '─│┌┬┐├┼┤└┴┘', '-|+++++++++') max_height = int((max_height-2) / 2) # -2 for title, bottom # Figure out offset if sel: bottom_pad = 2 offset = sel - max( min(sel, max_height - bottom_pad - 1), # Beginning and middle sel + min(0, max_height - len(grid)) # End positions ) else: offset = 0 # Find maximum length of the num column. max_n_w = len(str(max(map(lambda s: s['num'], grid)))) if len(grid) else 1 # Figure out number of trailing spaces to make the grid same width as the title. # | block | num | top = tl + (h*3) + t + (h*(max_n_w+2)) + tr max_w = max(len(top), len(title)) trailing = ' ' * (max_w - len(top)) out = [] out.append(bold(title, selected) + ' ' * (max_w - len(title))) out.append(top + trailing) for c, slot in enumerate(grid[offset:offset+max_height]): i = c + offset if slot is not None: block_char = blocks[slot['block']]['char'] num = slot['num'] else: block_char, num = ' ', '' colour = blocks[slot['block']]['colours'] if colour['bg'] is None: block_char = colour_str( block_char, fg=colour['fg'], bg=None, style=colour['style'] ) # Have to do the padding before colour because the colour # messes with the char count. (The block will always be 1 char wide.) num = '{:{max}}'.format(num, max=max_n_w) out.append('{v} {b} {v} {n} {v}{trail}'.format( b=block_char, n=colour_str(num, bg=RED) if selected and i == sel else num, v=v, trail=trailing )) if not (c == max_height - 1 or i == len(grid) - 1): out.append(l + (h*3) + m + (h*(max_n_w+2)) + r + trailing) out.append(bl + (h*3) + b + (h*(max_n_w+2)) + br + trailing) return out