make-selection 1.0.7__tar.gz → 1.0.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,19 @@
1
- Copyright (c) 2024 Steve Frazee
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all
11
- copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ Copyright (c) 2024 Steve Frazee
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  SOFTWARE.
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: make_selection
3
- Version: 1.0.7
3
+ Version: 1.0.9
4
4
  Summary: Package for interactive command line menu
5
5
  Author-email: Steven Frazee <stevefrazee123@gmail.com>
6
6
  Classifier: License :: OSI Approved :: MIT License
7
7
  Classifier: Development Status :: 4 - Beta
8
- Classifier: Programming Language :: Python :: 3.9
8
+ Classifier: Programming Language :: Python :: 3.14
9
9
  Classifier: Operating System :: Microsoft :: Windows
10
10
  Requires-Python: >=3.9
11
11
  Description-Content-Type: text/markdown
@@ -1,26 +1,26 @@
1
- <p align="center">
2
- <img src="https://github.com/steve3424/make_selection/blob/main/images/logo.png?raw=true" alt="Make selection logo">
3
- </p>
4
-
5
-
6
- ## Setup
7
- ```
8
- pip install make_selection
9
- ```
10
-
11
- ## Example
12
- #### file.py
13
- ```python
14
- from make_selection import makeSelection
15
-
16
- options = ["one", "two", "three"]
17
- label = "choose option"
18
- selected = makeSelection(options, label)
19
- ```
20
-
21
- #### Interacting with menu
22
- <img src="https://github.com/steve3424/make_selection/blob/main/images/using_menu.png?raw=true" alt="image of cli while using the menu">
23
- <br>
24
-
25
- #### After making selection
26
- <img src="https://github.com/steve3424/make_selection/blob/main/images/item_selected.png?raw=true" alt="image of cli after item is selected">
1
+ <p align="center">
2
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/logo.png?raw=true" alt="Make selection logo">
3
+ </p>
4
+
5
+
6
+ ## Setup
7
+ ```
8
+ pip install make_selection
9
+ ```
10
+
11
+ ## Example
12
+ #### file.py
13
+ ```python
14
+ from make_selection import makeSelection
15
+
16
+ options = ["one", "two", "three"]
17
+ label = "choose option"
18
+ selected = makeSelection(options, label)
19
+ ```
20
+
21
+ #### Interacting with menu
22
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/using_menu.png?raw=true" alt="image of cli while using the menu">
23
+ <br>
24
+
25
+ #### After making selection
26
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/item_selected.png?raw=true" alt="image of cli after item is selected">
@@ -1,15 +1,15 @@
1
- [project]
2
- name = "make_selection"
3
- version = "1.0.7"
4
- authors = [
5
- { name="Steven Frazee", email="stevefrazee123@gmail.com" },
6
- ]
7
- description = "Package for interactive command line menu"
8
- readme = "README.md"
9
- requires-python = ">=3.9"
10
- classifiers = [
11
- "License :: OSI Approved :: MIT License",
12
- "Development Status :: 4 - Beta",
13
- "Programming Language :: Python :: 3.9",
14
- "Operating System :: Microsoft :: Windows",
15
- ]
1
+ [project]
2
+ name = "make_selection"
3
+ version = "1.0.9"
4
+ authors = [
5
+ { name="Steven Frazee", email="stevefrazee123@gmail.com" },
6
+ ]
7
+ description = "Package for interactive command line menu"
8
+ readme = "README.md"
9
+ requires-python = ">=3.9"
10
+ classifiers = [
11
+ "License :: OSI Approved :: MIT License",
12
+ "Development Status :: 4 - Beta",
13
+ "Programming Language :: Python :: 3.14",
14
+ "Operating System :: Microsoft :: Windows",
15
+ ]
@@ -0,0 +1,10 @@
1
+ from enum import Enum, auto
2
+
3
+ class KeyCode(Enum):
4
+ UP = auto()
5
+ DOWN = auto()
6
+ SELECT = auto()
7
+ SELECT_MULTI = auto()
8
+ CANCEL = auto()
9
+ DELETE_CHAR = auto()
10
+ SEARCHABLE = auto()
@@ -1,271 +1,288 @@
1
- """
2
- Module for interactive command line menu. Simply accepts a list of str-able objects
3
- and allows user to select using arrow keys.
4
-
5
- Ansi escape codes are used as described here: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
6
- """
7
- # TODO: work on all platforms
8
- # TODO: multi_select: Maintain original index for re-insertion.
9
- # TODO: multi_select: TAB switches to delete mode.
10
- import sys
11
- if sys.platform != "win32":
12
- raise NotImplementedError("This module is only available on Windows.")
13
- import msvcrt
14
- import ctypes
15
- from typing import Any
16
- from copy import copy
17
- from enum import Enum
18
-
19
- stdout = -11
20
- enable_ansi_codes = 7
21
- kernel32 = ctypes.windll.kernel32
22
- kernel32.SetConsoleMode(kernel32.GetStdHandle(stdout), enable_ansi_codes)
23
-
24
- ANSI_MOVE_CURSOR = "\x1b[{up}F\r\x1b[{right}C"
25
- ANSI_HIGHLIGHT = "\x1b[30;47m"
26
- ANSI_HIGHLIGHT_SEARCH_STRING = "\x1b[95;47m"
27
- ANSI_YELLOW = "\x1b[93m"
28
- ANSI_BLUE = "\x1b[94m"
29
- ANSI_MAGENTA = "\x1b[95m"
30
- ANSI_GREEN = "\x1b[92m"
31
- ANSI_RESET = "\x1b[0m"
32
-
33
- SPECIAL_KEY = 224
34
- UP_ARROW = 72
35
- DOWN_ARROW = 80
36
- ENTER_KEY = 13
37
- CTL_C = 3
38
- BACKSPACE = 8
39
- SPACEBAR = 32
40
- CTL_RIGHT = 116
41
-
42
- class Mode(Enum):
43
- NORMAL = 0
44
- MULTI_SELECT = 1
45
- MULTI_DELETE = 2
46
-
47
- class Option:
48
- def __init__(self, obj: Any, sub_string_start: int=0) -> None:
49
- self.value = obj
50
- self.sub_string_start = sub_string_start
51
-
52
- class Menu:
53
- def __init__(self, options: list, label: str, window_size: int=10, multi_select: bool=False) -> None:
54
- assert options
55
- assert label
56
- assert 1 <= window_size and window_size <= 25
57
- window_size = min((len(options)), window_size)
58
-
59
- self.options_original = [Option(op) for op in options]
60
- self.options_current = copy(self.options_original)
61
- self.options_selected = []
62
- self.search_string = ""
63
- self.label = label
64
- self.selected_index = 0
65
- self.window_top = 0
66
- self.window_size_original = window_size
67
- self.window_size_current = window_size
68
- self.help_string_multi_select = "Enter: Select, Ctl+C: Cancel, Ctl\u2192: Done"
69
- self.help_string_normal = "Enter: Select, Ctl+C: Cancel"
70
- if multi_select:
71
- self.mode = Mode.MULTI_SELECT
72
- self.help_string = self.help_string_multi_select
73
- else:
74
- self.mode = Mode.NORMAL
75
- self.help_string = self.help_string_normal
76
-
77
- def show(self):
78
- self.printMenu()
79
- while True:
80
- something_changed = False
81
- char = self.getChar()
82
- if char == SPECIAL_KEY:
83
- char = self.getChar()
84
- if char == CTL_RIGHT:
85
- if self.mode == Mode.MULTI_SELECT:
86
- self.printSelected()
87
- return self.multiSelectGetValues(self.options_selected)
88
- elif 1 < len(self.options_current):
89
- # NOTE: Update selected index
90
- if char == UP_ARROW:
91
- self.selected_index = (self.selected_index - 1) % len(self.options_current)
92
- something_changed = True
93
- elif char == DOWN_ARROW:
94
- self.selected_index = (self.selected_index + 1) % len(self.options_current)
95
- something_changed = True
96
-
97
- # NOTE: Update window
98
- if something_changed:
99
- bottom = self.window_top + self.window_size_current
100
- if self.selected_index < self.window_top:
101
- self.window_top = self.selected_index
102
- elif bottom <= self.selected_index:
103
- self.window_top = self.selected_index - self.window_size_current + 1
104
- elif self.isSearchableChar(char):
105
- char = chr(char)
106
- self.search_string = f"{self.search_string}{char}".lstrip()
107
- if self.search_string:
108
- print(char, end="", flush=True, file=sys.stderr)
109
- self.search(self.options_current)
110
- something_changed = True
111
- elif char == BACKSPACE and 0 < len(self.search_string):
112
- self.search_string = self.search_string[:-1]
113
- # NOTE: Move left once ([1D), print space, move left again
114
- print("\x1b[1D \x1b[1D", end="", flush=True, file=sys.stderr)
115
- self.search(self.options_original)
116
- something_changed = True
117
- elif char == ENTER_KEY and self.options_current:
118
- if self.mode == Mode.MULTI_SELECT:
119
- self.multiSelectAdd()
120
- something_changed = True
121
- elif self.mode == Mode.NORMAL:
122
- self.printSelected()
123
- return self.options_current[self.selected_index].value
124
- elif char == CTL_C:
125
- self.clearMenu()
126
- print("cancelled", file=sys.stderr)
127
- return None
128
-
129
- if something_changed:
130
- self.clearMenu()
131
- self.printMenu()
132
-
133
- def search(self, search_list: list) -> None:
134
- found_options = []
135
- for o in search_list:
136
- found_i = str(o.value).lower().find(self.search_string.lower())
137
- if found_i != -1:
138
- o.sub_string_start = found_i
139
- found_options.append(o)
140
- self.options_current = found_options
141
- self.resetWindow()
142
-
143
- def multiSelectAdd(self) -> None:
144
- selected_option = self.options_current[self.selected_index]
145
- self.options_current.remove(selected_option)
146
- self.options_original.remove(selected_option)
147
- self.options_selected.append(selected_option)
148
- self.resetWindowMultiSelect()
149
-
150
- def multiSelectGetValues(self, options: list[Option]) -> list[Any]:
151
- return [op.value for op in options]
152
-
153
- def getChar(self) -> int:
154
- return ord(msvcrt.getch())
155
-
156
- def isSearchableChar(self, char):
157
- return (32 <= char and char <= 126)
158
-
159
- def resetWindow(self):
160
- self.window_top = 0
161
- self.selected_index = 0
162
- self.window_size_current = min((len(self.options_current), self.window_size_original))
163
-
164
- def resetWindowMultiSelect(self):
165
- """
166
- After selecting item and shrinking the list we try would like to keep
167
- the selected_index and window the same. This visually looks like the
168
- list is being pulled up and the selected index goes to the next item.
169
-
170
- First we will try and shift the window up, keeping the selected index
171
- the same. Visually this looks a bit weird as the selected index looks
172
- to be moving down the list, but the behavior I think is good because
173
- the index goes to the next item in the list. If we kept the index static
174
- on the screen it would be moving to the previous item instead of the next
175
- which I don't think I want.
176
-
177
- If we can't keep the list the same or shift the window up, that means the
178
- list is too small and we will just shrink the window.
179
- """
180
- window_bottom_current = self.window_top + self.window_size_current
181
- if len(self.options_current) < window_bottom_current:
182
- if 0 < self.window_top:
183
- self.window_top -= 1
184
- else:
185
- self.window_size_current -= 1
186
- # NOTE: this must be calculated after we change top/size above
187
- window_bottom_new = self.window_top + self.window_size_current
188
- if window_bottom_new <= self.selected_index:
189
- self.selected_index -= 1
190
-
191
- def clearMenu(self):
192
- """
193
- Clears from beginning of current line to end of screen (not end of line).
194
- This assumes the cursor is on the top line (label), which is currently always true.
195
-
196
- [0G move cursor to column 0.
197
- [J clears to end of screen (not end of line).
198
- """
199
- print("\x1b[0G\x1b[J", end="", flush=True, file=sys.stderr)
200
-
201
- def printMenu(self):
202
- header = f"{ANSI_BLUE}{self.label}>{ANSI_RESET}{self.search_string}"
203
- header_num_lines = 1
204
- if self.mode == Mode.MULTI_SELECT:
205
- header += f"\n{ANSI_GREEN}{len(self.options_selected)} items selected!{ANSI_RESET}"
206
- header_num_lines = 2
207
- print(header, file=sys.stderr)
208
-
209
- bottom = self.window_top + self.window_size_current
210
- if not self.options_current:
211
- print(f"{ANSI_BLUE}no matches found{ANSI_RESET}", file=sys.stderr)
212
- # NOTE: window size is 0 here after empty search, but we are printing 1 line
213
- # so we need to set it so the footer prints correctly
214
- self.window_size_current = 1
215
- else:
216
- for i in range(self.window_top, bottom):
217
- option_to_print = str(self.options_current[i].value)
218
- highlight_beg = self.options_current[i].sub_string_start
219
- highlight_end = highlight_beg + len(self.search_string)
220
- opt_beg = option_to_print[0 : highlight_beg]
221
- opt_mid = option_to_print[highlight_beg : highlight_end]
222
- opt_end = option_to_print[highlight_end :]
223
- if i == self.selected_index:
224
- option_to_print = f"{ANSI_HIGHLIGHT}{opt_beg}{ANSI_HIGHLIGHT_SEARCH_STRING}{opt_mid}{ANSI_HIGHLIGHT}{opt_end}{ANSI_RESET}"
225
- else:
226
- option_to_print = f"{opt_beg}{ANSI_MAGENTA}{opt_mid}{ANSI_RESET}{opt_end}"
227
- print(option_to_print, file=sys.stderr)
228
-
229
- footer = f"{ANSI_YELLOW}{self.help_string}{ANSI_RESET}\n"
230
- footer_num_lines = 1
231
- print(f"{footer}{ANSI_MOVE_CURSOR.format(up=self.window_size_current + header_num_lines + footer_num_lines, right=len(self.label) + len(self.search_string) + 1)}", end="", flush=True, file=sys.stderr)
232
-
233
- def printSelected(self):
234
- self.clearMenu()
235
- if self.mode == Mode.MULTI_SELECT:
236
- if len(self.options_selected) == 0:
237
- print(f"{self.label}> (0 items) []", file=sys.stderr)
238
- elif len(self.options_selected) == 1:
239
- print(f"{self.label}> (1 item) {self.multiSelectGetValues(self.options_selected)}", file=sys.stderr)
240
- else:
241
- print(f"{self.label}> ({len(self.options_selected)} items) [{self.options_selected[0].value}, ...]", file=sys.stderr)
242
- else:
243
- print(f"{self.label}> {self.options_current[self.selected_index].value}", file=sys.stderr)
244
-
245
- def makeSelection(options: list[Any], label: str, window_size: int=None, multi_select: bool=False) -> Any:
246
- """
247
- Entry point for menu selection.
248
-
249
- Parameters
250
- ----------
251
- options
252
- List of str-able objects.
253
- label
254
- Label to describe the items being selected.
255
- window_size
256
- Max number of items to show at once.
257
- multi_select
258
- Select list of items.
259
-
260
- Returns
261
- -------
262
- Selected value or list of selected values.
263
- """
264
- if window_size:
265
- return Menu(options, label, window_size=window_size, multi_select=multi_select).show()
266
- else:
267
- return Menu(options, label, multi_select=multi_select).show()
268
-
269
- if __name__ == "__main__":
270
- print(f"Returns: '{makeSelection(['interactive', 'cli', 'menu'], 'make_selection')}'", file=sys.stderr)
271
- print(f"Returns: '{makeSelection(['interactive', 'cli', 'menu'], 'make_selection', multi_select=True)}'", file=sys.stderr)
1
+ """
2
+ Module for interactive command line menu. Simply accepts a list of str-able objects
3
+ and allows user to select using arrow keys.
4
+
5
+ Ansi escape codes are used as described here: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
6
+ """
7
+ # TODO: multi_select: Maintain original index for re-insertion.
8
+ # TODO: multi_select: TAB switches to delete mode.
9
+ import sys
10
+ if sys.platform == "win32":
11
+ from mappings.windows import getChar
12
+ multi_select_modifier_string = "Ctl"
13
+ elif sys.platform == "darwin":
14
+ from mappings.mac import getChar
15
+ multi_select_modifier_string = "Cmd"
16
+ else:
17
+ raise NotImplementedError(f"Platform '{sys.platform}' not supported!")
18
+ from typing import Any
19
+ from copy import copy
20
+ from enum import Enum
21
+ from key_codes import KeyCode
22
+
23
+ ANSI_MOVE_CURSOR = "\x1b[{up}F\r\x1b[{right}C"
24
+ ANSI_HIGHLIGHT = "\x1b[30;47m"
25
+ ANSI_HIGHLIGHT_SEARCH_STRING = "\x1b[95;47m"
26
+ ANSI_YELLOW = "\x1b[93m"
27
+ ANSI_BLUE = "\x1b[94m"
28
+ ANSI_MAGENTA = "\x1b[95m"
29
+ ANSI_GREEN = "\x1b[92m"
30
+ ANSI_RED = "\x1b[91m"
31
+ ANSI_RESET = "\x1b[0m"
32
+
33
+ class Mode(Enum):
34
+ NORMAL = 0
35
+ MULTI_SELECT = 1
36
+ MULTI_DELETE = 2
37
+
38
+ class Option:
39
+ def __init__(self, obj: Any, sub_string_start: int=0) -> None:
40
+ self.value = obj
41
+ self.sub_string_start = sub_string_start
42
+
43
+ class Menu:
44
+ def __init__(self, options: list, label: str, window_size: int=10, multi_select: bool=False) -> None:
45
+ assert options
46
+ assert label
47
+ assert 1 <= window_size and window_size <= 25
48
+ window_size = min((len(options)), window_size)
49
+
50
+ self.options_original = [Option(op) for op in options]
51
+ self.options_current = copy(self.options_original)
52
+ self.options_selected = []
53
+ self.search_string = ""
54
+ self.label = label
55
+ self.selected_index = 0
56
+ self.window_top = 0
57
+ self.window_size_original = window_size
58
+ self.window_size_current = window_size
59
+ self.help_string_multi_select = f"Enter: Select, Ctl+C: Cancel, {multi_select_modifier_string}\u2192: Done"
60
+ self.help_string_normal = "Enter: Select, Ctl+C: Cancel"
61
+ if multi_select:
62
+ self.mode = Mode.MULTI_SELECT
63
+ self.help_string = self.help_string_multi_select
64
+ else:
65
+ self.mode = Mode.NORMAL
66
+ self.help_string = self.help_string_normal
67
+
68
+ def show(self):
69
+ self.printMenu()
70
+ while True:
71
+ something_changed = False
72
+ key_code, char = getChar()
73
+ if key_code in (KeyCode.UP, KeyCode.DOWN) and 1 < len(self.options_current):
74
+ window_shift = 1 if key_code == KeyCode.DOWN else -1
75
+ self.selected_index = (self.selected_index + window_shift) % len(self.options_current)
76
+ bottom = self.window_top + self.window_size_current
77
+ if self.selected_index < self.window_top:
78
+ self.window_top = self.selected_index
79
+ elif bottom <= self.selected_index:
80
+ self.window_top = self.selected_index - self.window_size_current + 1
81
+ something_changed = True
82
+ elif key_code == KeyCode.SELECT and self.options_current:
83
+ if self.mode == Mode.MULTI_SELECT:
84
+ self.multiSelectAdd()
85
+ something_changed = True
86
+ elif self.mode == Mode.NORMAL:
87
+ self.printSelected()
88
+ return self.options_current[self.selected_index].value
89
+ elif key_code == KeyCode.SEARCHABLE:
90
+ self.search_string = f"{self.search_string}{char}".lstrip()
91
+ if self.search_string:
92
+ print(char, end="", flush=True, file=sys.stderr)
93
+ self.search(self.options_current)
94
+ something_changed = True
95
+ elif key_code == KeyCode.DELETE_CHAR and 0 < len(self.search_string):
96
+ self.search_string = self.search_string[:-1]
97
+ # NOTE: Move left once ([1D), print space, move left again
98
+ print("\x1b[1D \x1b[1D", end="", flush=True, file=sys.stderr)
99
+ self.search(self.options_original)
100
+ something_changed = True
101
+ elif key_code == KeyCode.CANCEL:
102
+ self.clearMenu()
103
+ print("cancelled", file=sys.stderr)
104
+ return None
105
+ elif key_code == KeyCode.SELECT_MULTI and self.mode == Mode.MULTI_SELECT:
106
+ self.printSelected()
107
+ return self.multiSelectGetValues(self.options_selected)
108
+
109
+ if something_changed:
110
+ self.clearMenu()
111
+ self.printMenu()
112
+
113
+ def search(self, search_list: list) -> None:
114
+ found_options = []
115
+ for o in search_list:
116
+ found_i = str(o.value).lower().find(self.search_string.lower())
117
+ if found_i != -1:
118
+ o.sub_string_start = found_i
119
+ found_options.append(o)
120
+ self.options_current = found_options
121
+ self.resetWindow()
122
+
123
+ def multiSelectAdd(self) -> None:
124
+ selected_option = self.options_current[self.selected_index]
125
+ self.options_current.remove(selected_option)
126
+ self.options_original.remove(selected_option)
127
+ self.options_selected.append(selected_option)
128
+ self.resetWindowMultiSelect()
129
+
130
+ def multiSelectGetValues(self, options: list[Option]) -> list[Any]:
131
+ return [op.value for op in options]
132
+
133
+ def resetWindow(self):
134
+ self.window_top = 0
135
+ self.selected_index = 0
136
+ self.window_size_current = min((len(self.options_current), self.window_size_original))
137
+
138
+ def resetWindowMultiSelect(self):
139
+ """
140
+ After selecting item and shrinking the list we try would like to keep
141
+ the selected_index and window the same. This visually looks like the
142
+ list is being pulled up and the selected index goes to the next item.
143
+
144
+ First we will try and shift the window up, keeping the selected index
145
+ the same. Visually this looks a bit weird as the selected index looks
146
+ to be moving down the list, but the behavior I think is good because
147
+ the index goes to the next item in the list. If we kept the index static
148
+ on the screen it would be moving to the previous item instead of the next
149
+ which I don't think I want.
150
+
151
+ If we can't keep the list the same or shift the window up, that means the
152
+ list is too small and we will just shrink the window.
153
+ """
154
+ window_bottom_current = self.window_top + self.window_size_current
155
+ if len(self.options_current) < window_bottom_current:
156
+ if 0 < self.window_top:
157
+ self.window_top -= 1
158
+ else:
159
+ self.window_size_current -= 1
160
+ # NOTE: this must be calculated after we change top/size above
161
+ window_bottom_new = self.window_top + self.window_size_current
162
+ if window_bottom_new <= self.selected_index:
163
+ self.selected_index -= 1
164
+
165
+ def clearMenu(self):
166
+ """
167
+ Clears from beginning of current line to end of screen (not end of line).
168
+ This assumes the cursor is on the top line (label), which is currently always true.
169
+
170
+ [0G move cursor to column 0.
171
+ [J clears to end of screen (not end of line).
172
+ """
173
+ print("\x1b[0G\x1b[J", end="", flush=True, file=sys.stderr)
174
+
175
+ def printMenu(self):
176
+ header = f"{ANSI_BLUE}{self.label}>{ANSI_RESET}{self.search_string}"
177
+ header_num_lines = 1
178
+ if self.mode == Mode.MULTI_SELECT:
179
+ header += f"\n{ANSI_GREEN}{len(self.options_selected)} items selected!{ANSI_RESET}"
180
+ header_num_lines = 2
181
+ print(header, file=sys.stderr)
182
+
183
+ bottom = self.window_top + self.window_size_current
184
+ if not self.options_current:
185
+ print(f"{ANSI_BLUE}no matches found{ANSI_RESET}", file=sys.stderr)
186
+ # NOTE: window size is 0 here after empty search, but we are printing 1 line
187
+ # so we need to set it so the footer prints correctly
188
+ self.window_size_current = 1
189
+ else:
190
+ for i in range(self.window_top, bottom):
191
+ option_to_print = str(self.options_current[i].value)
192
+ highlight_beg = self.options_current[i].sub_string_start
193
+ highlight_end = highlight_beg + len(self.search_string)
194
+ opt_beg = option_to_print[0 : highlight_beg]
195
+ opt_mid = option_to_print[highlight_beg : highlight_end]
196
+ opt_end = option_to_print[highlight_end :]
197
+ if i == self.selected_index:
198
+ option_to_print = f"{ANSI_HIGHLIGHT}{opt_beg}{ANSI_HIGHLIGHT_SEARCH_STRING}{opt_mid}{ANSI_HIGHLIGHT}{opt_end}{ANSI_RESET}"
199
+ else:
200
+ option_to_print = f"{opt_beg}{ANSI_MAGENTA}{opt_mid}{ANSI_RESET}{opt_end}"
201
+ print(option_to_print, file=sys.stderr)
202
+
203
+ footer = f"{ANSI_YELLOW}{self.help_string}{ANSI_RESET}\n"
204
+ footer_num_lines = 1
205
+ print(f"{footer}{ANSI_MOVE_CURSOR.format(up=self.window_size_current + header_num_lines + footer_num_lines, right=len(self.label) + len(self.search_string) + 1)}", end="", flush=True, file=sys.stderr)
206
+
207
+ def printSelected(self):
208
+ self.clearMenu()
209
+ if self.mode == Mode.MULTI_SELECT:
210
+ if len(self.options_selected) == 0:
211
+ print(f"{self.label}> (0 items) []", file=sys.stderr)
212
+ elif len(self.options_selected) == 1:
213
+ print(f"{self.label}> (1 item) {self.multiSelectGetValues(self.options_selected)}", file=sys.stderr)
214
+ else:
215
+ print(f"{self.label}> ({len(self.options_selected)} items) [{self.options_selected[0].value}, ...]", file=sys.stderr)
216
+ else:
217
+ print(f"{self.label}> {self.options_current[self.selected_index].value}", file=sys.stderr)
218
+
219
+ def makeSelection(options: list[Any], label: str, window_size: int=None, multi_select: bool=False) -> Any:
220
+ """
221
+ Entry point for menu selection.
222
+
223
+ Parameters
224
+ ----------
225
+ options
226
+ List of str-able objects.
227
+ label
228
+ Label to describe the items being selected.
229
+ window_size
230
+ Max number of items to show at once.
231
+ multi_select
232
+ Select list of items.
233
+
234
+ Returns
235
+ -------
236
+ Selected value or list of selected values.
237
+ """
238
+ if window_size:
239
+ return Menu(options, label, window_size=window_size, multi_select=multi_select).show()
240
+ else:
241
+ return Menu(options, label, multi_select=multi_select).show()
242
+
243
+ if __name__ == "__main__":
244
+ import argparse
245
+ def showcase():
246
+ print(f"Returns: '{makeSelection(['interactive', 'cli', 'menu'], 'make_selection')}'", file=sys.stderr)
247
+ print(f"Returns: '{makeSelection(['interactive', 'cli', 'menu'], 'make_selection', multi_select=True)}'", file=sys.stderr)
248
+
249
+ def test_input():
250
+ print(f"{ANSI_YELLOW}Testing keyboard interactively!{ANSI_RESET}")
251
+ test_cases = [
252
+ ("Press up arrow \u2191", (KeyCode.UP, None)),
253
+ ("Press down arrow \u2193", (KeyCode.DOWN, None)),
254
+ ("Press right arrow \u2192", (None, None)),
255
+ ("Press left arrow \u2190", (None, None)),
256
+ ("Press enter \u21B5", (KeyCode.SELECT, None)),
257
+ (f"Press {multi_select_modifier_string}+right", (KeyCode.SELECT_MULTI, None)),
258
+ ("Press backspace \u232b", (KeyCode.DELETE_CHAR, None)),
259
+ ("Press lowercase a", (KeyCode.SEARCHABLE, 'a')),
260
+ ("Press uppercase A", (KeyCode.SEARCHABLE, 'A')),
261
+ ("Press number 7", (KeyCode.SEARCHABLE, '7')),
262
+ ("Press pound #", (KeyCode.SEARCHABLE, '#')),
263
+ ("Press ctl+c", (KeyCode.CANCEL, None)),
264
+ ]
265
+ num_errors = 0
266
+ for msg, expected in test_cases:
267
+ print(msg, end="", flush=True)
268
+ if getChar() == expected:
269
+ print(" [\u2705]")
270
+ else:
271
+ num_errors += 1
272
+ print(" [\u274C]")
273
+ if num_errors == 0:
274
+ print(f"{ANSI_GREEN}Working :){ANSI_RESET}")
275
+ else:
276
+ print(f"{ANSI_RED}not working :({ANSI_RESET}")
277
+
278
+ arg_parser = argparse.ArgumentParser(description="Interactive testing.")
279
+ sub_parsers = arg_parser.add_subparsers(required=True)
280
+
281
+ arg_parser_showcase = sub_parsers.add_parser("showcase", help="Interactive example.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
282
+ arg_parser_showcase.set_defaults(func=showcase)
283
+
284
+ arg_parser_test_keys = sub_parsers.add_parser("test_keys", help="Test keyboard input.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
285
+ arg_parser_test_keys.set_defaults(func=test_input)
286
+
287
+ args_main = arg_parser.parse_args()
288
+ args_main.func()
@@ -0,0 +1,51 @@
1
+ import os
2
+ import sys
3
+ import termios
4
+ import tty
5
+ from key_codes import KeyCode
6
+
7
+ ARROW_UP = b"\x1b[A"
8
+ ARROW_DOWN = b"\x1b[B"
9
+ CMD_RIGHT = b"\x05"
10
+ CTL_C = b"\x03"
11
+ ENTER_PATTERNS = b"\r\n"
12
+ BACKSPACE_PATTERNS = b"\x08\x7f"
13
+
14
+ def _is_searchable(key_press: bytes) -> bool:
15
+ try:
16
+ key_press = ord(key_press.decode())
17
+ return (32 <= key_press and key_press <= 126)
18
+ except:
19
+ return False
20
+
21
+ def _read_key_press() -> bytes:
22
+ try:
23
+ fd = sys.stdin.fileno()
24
+ old_settings = termios.tcgetattr(fd)
25
+ tty.setraw(fd)
26
+ # NOTE: Read up to 3 bytes for escape sequences.
27
+ return os.read(fd, 3)
28
+ finally:
29
+ termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
30
+
31
+ def getChar() -> tuple[KeyCode|None, str|None]:
32
+ key_press: bytes = _read_key_press()
33
+
34
+ key_code = None
35
+ char = None
36
+ if key_press == ARROW_UP:
37
+ key_code = KeyCode.UP
38
+ elif key_press == ARROW_DOWN:
39
+ key_code = KeyCode.DOWN
40
+ elif key_press in ENTER_PATTERNS:
41
+ key_code = KeyCode.SELECT
42
+ elif key_press == CMD_RIGHT:
43
+ key_code = KeyCode.SELECT_MULTI
44
+ elif key_press == CTL_C:
45
+ key_code = KeyCode.CANCEL
46
+ elif key_press in BACKSPACE_PATTERNS:
47
+ key_code = KeyCode.DELETE_CHAR
48
+ elif _is_searchable(key_press):
49
+ key_code = KeyCode.SEARCHABLE
50
+ char = key_press.decode()
51
+ return key_code, char
@@ -0,0 +1,43 @@
1
+ import msvcrt
2
+ import ctypes
3
+ from key_codes import KeyCode
4
+
5
+ stdout = -11
6
+ enable_ansi_codes = 7
7
+ kernel32 = ctypes.windll.kernel32
8
+ kernel32.SetConsoleMode(kernel32.GetStdHandle(stdout), enable_ansi_codes)
9
+
10
+ SPECIAL_KEY = 224
11
+ ARROW_UP = 72
12
+ ARROW_DOWN = 80
13
+ ENTER = 13
14
+ CTL_C = 3
15
+ CTL_RIGHT = 116
16
+ BACKSPACE = 8
17
+
18
+ def isSearchable(char: int) -> bool:
19
+ return (32 <= char and char <= 126)
20
+
21
+ def getChar() -> tuple[KeyCode|None, str|None]:
22
+ key_code = None
23
+ char = None
24
+
25
+ key_press = ord(msvcrt.getch())
26
+ if key_press == SPECIAL_KEY:
27
+ key_press = ord(msvcrt.getch())
28
+ if key_press == ARROW_UP:
29
+ key_code = KeyCode.UP
30
+ elif key_press == ARROW_DOWN:
31
+ key_code = KeyCode.DOWN
32
+ elif key_press == CTL_RIGHT:
33
+ key_code = KeyCode.SELECT_MULTI
34
+ elif key_press == ENTER:
35
+ key_code = KeyCode.SELECT
36
+ elif key_press == CTL_C:
37
+ key_code = KeyCode.CANCEL
38
+ elif key_press == BACKSPACE:
39
+ key_code = KeyCode.DELETE_CHAR
40
+ elif isSearchable(key_press):
41
+ key_code = KeyCode.SEARCHABLE
42
+ char = chr(key_press)
43
+ return key_code, char
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: make_selection
3
- Version: 1.0.7
3
+ Version: 1.0.9
4
4
  Summary: Package for interactive command line menu
5
5
  Author-email: Steven Frazee <stevefrazee123@gmail.com>
6
6
  Classifier: License :: OSI Approved :: MIT License
7
7
  Classifier: Development Status :: 4 - Beta
8
- Classifier: Programming Language :: Python :: 3.9
8
+ Classifier: Programming Language :: Python :: 3.14
9
9
  Classifier: Operating System :: Microsoft :: Windows
10
10
  Requires-Python: >=3.9
11
11
  Description-Content-Type: text/markdown
@@ -2,8 +2,12 @@ LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
4
  src/make_selection/__init__.py
5
+ src/make_selection/key_codes.py
5
6
  src/make_selection/make_selection.py
6
7
  src/make_selection.egg-info/PKG-INFO
7
8
  src/make_selection.egg-info/SOURCES.txt
8
9
  src/make_selection.egg-info/dependency_links.txt
9
- src/make_selection.egg-info/top_level.txt
10
+ src/make_selection.egg-info/top_level.txt
11
+ src/make_selection/mappings/mac.py
12
+ src/make_selection/mappings/windows.py
13
+ test/test_windows.py
@@ -0,0 +1,62 @@
1
+ import sys
2
+ import os
3
+ sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../src/make_selection")
4
+
5
+ import unittest
6
+ import mappings.windows as windows
7
+ from key_codes import KeyCode
8
+ from unittest.mock import patch, MagicMock
9
+
10
+
11
+ class TestWindowsMappings(unittest.TestCase):
12
+ @patch("msvcrt.getch")
13
+ def test_up(self, getch_mock: MagicMock):
14
+ getch_mock.side_effect = [chr(windows.SPECIAL_KEY), chr(windows.ARROW_UP)]
15
+ key_code, char = windows.getChar()
16
+ self.assertEqual(key_code, KeyCode.UP)
17
+ self.assertEqual(char, None)
18
+
19
+ @patch("msvcrt.getch")
20
+ def test_down(self, getch_mock: MagicMock):
21
+ getch_mock.side_effect = [chr(windows.SPECIAL_KEY), chr(windows.ARROW_DOWN)]
22
+ key_code, char = windows.getChar()
23
+ self.assertEqual(key_code, KeyCode.DOWN)
24
+ self.assertEqual(char, None)
25
+
26
+ @patch("msvcrt.getch")
27
+ def test_select_multi(self, getch_mock: MagicMock):
28
+ getch_mock.side_effect = [chr(windows.SPECIAL_KEY), chr(windows.CTL_RIGHT)]
29
+ key_code, char = windows.getChar()
30
+ self.assertEqual(key_code, KeyCode.SELECT_MULTI)
31
+ self.assertEqual(char, None)
32
+
33
+ @patch("msvcrt.getch", return_value=chr(windows.ENTER))
34
+ def test_select(self, getch_mock: MagicMock):
35
+ key_code, char = windows.getChar()
36
+ self.assertEqual(key_code, KeyCode.SELECT)
37
+ self.assertEqual(char, None)
38
+
39
+ @patch("msvcrt.getch", return_value=chr(windows.CTL_C))
40
+ def test_cancel(self, getch_mock: MagicMock):
41
+ key_code, char = windows.getChar()
42
+ self.assertEqual(key_code, KeyCode.CANCEL)
43
+ self.assertEqual(char, None)
44
+
45
+ @patch("msvcrt.getch", return_value=chr(windows.BACKSPACE))
46
+ def test_delete_char(self, getch_mock: MagicMock):
47
+ key_code, char = windows.getChar()
48
+ self.assertEqual(key_code, KeyCode.DELETE_CHAR)
49
+ self.assertEqual(char, None)
50
+
51
+ @patch("msvcrt.getch")
52
+ def test_searchables(self, getch_mock: MagicMock):
53
+ test_cases = [chr(i) for i in range(32, 127)]
54
+ for expected_char in test_cases:
55
+ getch_mock.return_value = expected_char
56
+ with self.subTest(value=expected_char):
57
+ key_code, char = windows.getChar()
58
+ self.assertEqual(key_code, KeyCode.SEARCHABLE)
59
+ self.assertEqual(char, expected_char)
60
+
61
+ if __name__ == '__main__':
62
+ unittest.main()
File without changes