make-selection 0.0.1__py3-none-any.whl

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.
@@ -0,0 +1 @@
1
+ from .make_selection import makeSelection
@@ -0,0 +1,140 @@
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
+ import sys
6
+ if sys.platform != "win32":
7
+ raise NotImplementedError("This module is only available on Windows.")
8
+ from typing import Any
9
+ import msvcrt
10
+ import ctypes
11
+
12
+ stdout = -11
13
+ enable_ansi_codes = 7
14
+ kernel32 = ctypes.windll.kernel32
15
+ kernel32.SetConsoleMode(kernel32.GetStdHandle(stdout), enable_ansi_codes)
16
+
17
+ ANSI_MOVE_CURSOR = "\x1b[{up}F\r\x1b[{right}C"
18
+ ANSI_HIGHLIGHT = "\x1b[30;47m"
19
+ ANSI_YELLOW = "\x1b[93m"
20
+ ANSI_BLUE = "\x1b[94m"
21
+ ANSI_RESET = "\x1b[0m"
22
+ SPECIAL_KEY = 224
23
+ UP_ARROW = 72
24
+ DOWN_ARROW = 80
25
+ ENTER_KEY = 13
26
+ CTL_C = 3
27
+ BACKSPACE = 8
28
+ SPACEBAR = 32
29
+
30
+ class Menu:
31
+ def __init__(self, options: list, label: str, window_size: int=10) -> None:
32
+ assert options
33
+ assert label
34
+ assert 1 < window_size
35
+ if len(options) < window_size:
36
+ window_size = len(options)
37
+
38
+ self.options_original = options
39
+ self.options_current = options
40
+ self.indices = []
41
+ self.search_string = ""
42
+ self.label = label
43
+ self.selected_index = 0
44
+ self.window_top = 0
45
+ self.window_original_size = window_size
46
+ self.window_current_size = window_size
47
+ self.help_string = "Enter: Select, Ctl+C: Cancel"
48
+
49
+ def show(self):
50
+ print(f"{ANSI_BLUE}{self.label}>{ANSI_RESET}")
51
+ self.printMenu()
52
+ while True:
53
+ something_changed = False
54
+ char = self.getChar()
55
+ if char == SPECIAL_KEY:
56
+ char = self.getChar()
57
+ if 1 < len(self.options_current):
58
+ # Update selected index
59
+ if char == UP_ARROW:
60
+ self.selected_index = (self.selected_index - 1) % len(self.options_current)
61
+ something_changed = True
62
+ elif char == DOWN_ARROW:
63
+ self.selected_index = (self.selected_index + 1) % len(self.options_current)
64
+ something_changed = True
65
+
66
+ # Update window
67
+ if something_changed:
68
+ bottom = self.window_top + self.window_current_size
69
+ if self.selected_index < self.window_top:
70
+ self.window_top = self.selected_index
71
+ elif bottom <= self.selected_index:
72
+ self.window_top = self.selected_index - self.window_current_size + 1
73
+ elif self.isAscii(char) or (char == SPACEBAR and 0 < len(self.search_string)):
74
+ # TODO: search string
75
+ pass
76
+ elif char == BACKSPACE and 0 < len(self.search_string):
77
+ # TODO: search string
78
+ pass
79
+ elif char == ENTER_KEY:
80
+ if self.options_current:
81
+ self.printSelected()
82
+ return self.options_original[self.selected_index]
83
+ elif char == CTL_C:
84
+ self.clearMenu(clear_label=True)
85
+ print("cancelled")
86
+ return None
87
+
88
+ if something_changed:
89
+ self.clearMenu()
90
+ self.printMenu()
91
+
92
+ def getChar(self):
93
+ return ord(msvcrt.getch())
94
+
95
+ def isAscii(self, char):
96
+ return (33 <= char and char <= 126)
97
+
98
+ def clearMenu(self, clear_label=False):
99
+ if clear_label:
100
+ print("\x1b[0G\x1b[J", end="", flush=True)
101
+ else:
102
+ print("\x1b[J")
103
+
104
+ def printMenu(self):
105
+ bottom = self.window_top + self.window_current_size
106
+ for i in range(self.window_top, bottom):
107
+ if i == self.selected_index:
108
+ print(f"{ANSI_HIGHLIGHT}{self.options_current[i]}{ANSI_RESET}")
109
+ else:
110
+ print(self.options_current[i])
111
+ print(f"{ANSI_YELLOW}{self.help_string}{ANSI_RESET}\n{ANSI_MOVE_CURSOR.format(up=self.window_current_size + 2, right=len(self.label) + len(self.search_string) + 1)}", end="", flush=True)
112
+
113
+ def printSelected(self):
114
+ self.clearMenu(clear_label=True)
115
+ print(f"{self.label}> {self.options_current[self.selected_index]}")
116
+
117
+ def makeSelection(options: list[Any], label: str, window_size: int=None) -> Any:
118
+ """
119
+ Entry point for menu selection.
120
+
121
+ Parameters
122
+ ----------
123
+ options
124
+ List of str-able objects.
125
+ label
126
+ Label to describe the items being selected.
127
+ window_size
128
+ Max number of items to show at once.
129
+
130
+ Returns
131
+ -------
132
+ Selected value.
133
+ """
134
+ if window_size:
135
+ return Menu(options, label, window_size).show()
136
+ else:
137
+ return Menu(options, label).show()
138
+
139
+ if __name__ == "__main__":
140
+ makeSelection(["interactive", "cli", "menu"], "make_selection")
@@ -0,0 +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
19
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.1
2
+ Name: make_selection
3
+ Version: 0.0.1
4
+ Summary: Package for interactive command line menu
5
+ Author-email: Steven Frazee <stevefrazee123@gmail.com>
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Programming Language :: Python :: 3.9
9
+ Classifier: Operating System :: Microsoft :: Windows
10
+ Requires-Python: >=3.9
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+
14
+ <p align="center">
15
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/logo.png?raw=true" alt="Make selection logo">
16
+ </p>
17
+
18
+
19
+ ## Setup
20
+ ```
21
+ pip install make_selection
22
+ ```
23
+
24
+ ## Example
25
+ #### file.py
26
+ ```python
27
+ from make_selection import makeSelection
28
+
29
+ options = ["one", "two", "three"]
30
+ label = "choose option"
31
+ selected = makeSelection(options, label)
32
+ ```
33
+
34
+ #### Interacting with menu
35
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/using_menu.png?raw=true" alt="image of cli while using the menu">
36
+ <br>
37
+
38
+ #### After making selection
39
+ <img src="https://github.com/steve3424/make_selection/blob/main/images/item_selected.png?raw=true" alt="image of cli after item is selected">
@@ -0,0 +1,7 @@
1
+ make_selection/__init__.py,sha256=NtV-iIr0EcWNHj3KCF1Y09fwkhS6pLnnIDFjSceNq2w,41
2
+ make_selection/make_selection.py,sha256=MMILn0nJrIMbFE0HRhuBZQRvRQwt1dOKM0bM9Xb9Iq8,4839
3
+ make_selection-0.0.1.dist-info/LICENSE,sha256=LFtv4AT1hdd5yEJmN2r5KjtjmI3p0iMiz0R1JF-fVxg,1055
4
+ make_selection-0.0.1.dist-info/METADATA,sha256=epoPQnAoippZWqoVA6rVRfVylwqRBtKTZzTZrd4D1TY,1196
5
+ make_selection-0.0.1.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
6
+ make_selection-0.0.1.dist-info/top_level.txt,sha256=md_80U2NNa3Ni6gFbctLsZ8jE4hqSgu_HdMul1gTEgg,15
7
+ make_selection-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (73.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ make_selection