def calc_pos( text, layout, pref_col, row ):

    if row < 0 or row >= len(layout):
        raise Exception("calculate_pos: out of layout row range")
    
    pos = calc_line_pos( text, layout[row], pref_col )
    if pos is not None:
        return pos
    
    rows_above = range(row-1,-1,-1)
    rows_below = range(row+1,len(layout))
    while rows_above and rows_below:
        if rows_above: 
            r = rows_above.pop(0)
            pos = calc_line_pos(text, layout[r], pref_col)
            if pos is not None: return pos
        if rows_below: 
            r = rows_below.pop(0)
            pos = calc_line_pos(text, layout[r], pref_col)
            if pos is not None: return pos
    return 0
Calculate the closest linear position to pref_col and row given a
dataset/ETHPy150Open AnyMesh/anyMesh-Python/example/urwid/text_layout.py/calc_pos