MCPcopy Index your code
hub / github.com/RustPython/RustPython / build_menu

Function build_menu

Lib/_pyrepl/completing_reader.py:71–116  ·  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

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

Callers 2

doMethod · 0.85
doMethod · 0.85

Calls 6

minFunction · 0.85
maxFunction · 0.85
lenFunction · 0.85
left_alignFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected