(
cons: console.Console,
wordlist: list[str],
start: int,
use_brackets: bool,
sort_in_column: bool,
)
| 70 | |
| 71 | |
| 72 | def build_menu( |
| 73 | cons: console.Console, |
| 74 | wordlist: list[str], |
| 75 | start: int, |
| 76 | use_brackets: bool, |
| 77 | sort_in_column: bool, |
| 78 | ) -> tuple[list[str], int]: |
| 79 | if use_brackets: |
| 80 | item = "[ %s ]" |
| 81 | padding = 4 |
| 82 | else: |
| 83 | item = "%s " |
| 84 | padding = 2 |
| 85 | maxlen = min(max(map(real_len, wordlist)), cons.width - padding) |
| 86 | cols = int(cons.width / (maxlen + padding)) |
| 87 | rows = int((len(wordlist) - 1)/cols + 1) |
| 88 | |
| 89 | if sort_in_column: |
| 90 | # sort_in_column=False (default) sort_in_column=True |
| 91 | # A B C A D G |
| 92 | # D E F B E |
| 93 | # G C F |
| 94 | # |
| 95 | # "fill" the table with empty words, so we always have the same amount |
| 96 | # of rows for each column |
| 97 | missing = cols*rows - len(wordlist) |
| 98 | wordlist = wordlist + ['']*missing |
| 99 | indexes = [(i % cols) * rows + i // cols for i in range(len(wordlist))] |
| 100 | wordlist = [wordlist[i] for i in indexes] |
| 101 | menu = [] |
| 102 | i = start |
| 103 | for r in range(rows): |
| 104 | row = [] |
| 105 | for col in range(cols): |
| 106 | row.append(item % left_align(wordlist[i], maxlen)) |
| 107 | i += 1 |
| 108 | if i >= len(wordlist): |
| 109 | break |
| 110 | menu.append(''.join(row)) |
| 111 | if i >= len(wordlist): |
| 112 | i = 0 |
| 113 | break |
| 114 | if r + 5 > cons.height: |
| 115 | menu.append(" %d more... " % (len(wordlist) - i)) |
| 116 | break |
| 117 | return menu, i |
| 118 | |
| 119 | # this gets somewhat user interface-y, and as a result the logic gets |
| 120 | # very convoluted. |
no test coverage detected
searching dependent graphs…