MCPcopy Index your code
hub / github.com/python/cpython / build_menu

Function build_menu

Lib/_pyrepl/completing_reader.py:72–117  ·  view source on GitHub ↗
(
        cons: console.Console,
        wordlist: list[str],
        start: int,
        use_brackets: bool,
        sort_in_column: bool,
)

Source from the content-addressed store, hash-verified

70
71
72def 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.

Callers 2

doMethod · 0.85
doMethod · 0.85

Calls 3

left_alignFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…