From 7b89a558ff02db09b2c5958a0ef20d1e1e92f4bf Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:47:41 +0100 Subject: [PATCH] fix: ask folder questions through one reader The copy question, the card question and the BIOS folder question each read q, Enter, a listed number or a folder. With no drive listed, the copy question took "2" as the folder ./2. They share one reader now, and the y/N questions read only y or yes: "yeah" created folders. --- install.ps1 | 2 +- install.py | 107 ++++++++++++++++------------------- install.sh | 2 +- install/launchers/index.json | 16 +++--- installer/console.py | 28 ++++++++- installer/destination.py | 36 +++++------- installer/handheld_app.py | 7 +-- installer/line_folder.py | 44 +++----------- tests/test_install.py | 6 ++ tests/test_install_tui.py | 9 +++ 10 files changed, 127 insertions(+), 130 deletions(-) diff --git a/install.ps1 b/install.ps1 index b1850e9db..18c76398b 100644 --- a/install.ps1 +++ b/install.ps1 @@ -16,7 +16,7 @@ # and Invoke-WebRequest throws on a cut transfer before the hash is read: # the site's build rewrites this line there (scripts/sitebootstrap.py). $mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." - $defaultInstallSha256 = "508ffd2832afe8e14dac09860ea73dcb770874c9683f26f8ea0edddb0f7a469b" + $defaultInstallSha256 = "c11a387a05a77fd98ad147971abf8ab12bb3caff592f8c28e894adaf57fefa39" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. $maximumInstallerBytes = 8MB diff --git a/install.py b/install.py index 6c463c76c..c4133c944 100644 --- a/install.py +++ b/install.py @@ -2336,6 +2336,32 @@ def _answer_yes(prompt: str) -> bool: return _ask(prompt).strip().lower() in ("y", "yes") +def _folder_answer(prompt: str, usage: str, default: bool, numbers: int = 0) -> str | None: + """A folder question's answer: None for Enter on a default, else the + text typed. q quits as at every other question, a folder named q being + typed ./q: q was a folder created in the current one and filled. Enter + with no default, or a number out of range that names no folder, says + what to type: the question came back with no word.""" + while True: + raw = _ask(prompt) + if not raw: + if default: + return None + print(usage) + continue + if raw.lower() == "q": + _quit() + if raw.isdigit() and not 1 <= int(raw) <= numbers and not _is_dir(_typed_folder(raw)): + print(usage) + continue + return raw + + +def _numbers_hint(count: int) -> str: + """" or 1 to N" after a question that takes a listed number, N rows.""" + return "" if not count else " or 1" if count == 1 else f" or 1 to {count}" + + class ReadCounter: """Files read so far, as event_kind events and on the live line: the program driving the installer gets the count the terminal gets.""" @@ -14538,15 +14564,14 @@ def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", (print if missing > 1 and ask else detail)( f" {shown_path(dest)} does not exist. The nearest folder that does: " f"{shown_path(nearest)}") - if missing > 1 and ask: - answer = _ask(f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] ") - if not answer.strip().lower().startswith("y"): - if again: - print(" Not created.") - return False - print(f" Nothing installed. Check the folder {named}, then run " - "the command again.") - sys.exit(2) + if missing > 1 and ask and not _answer_yes( + f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] "): + if again: + print(" Not created.") + return False + print(f" Nothing installed. Check the folder {named}, then run " + "the command again.") + sys.exit(2) return True @@ -14982,23 +15007,18 @@ def card_install_dest(named: str, plat: str, interactive: bool, ask: bool = True else "Mount the card, then type its top folder.")) listed = _list_cards(cards, blank) while True: - numbers = ("" if not listed else " or 1" if len(listed) == 1 - else f" or 1 to {len(listed)}") + usage = (f" Type a number from 1 to {len(listed)}, a folder, Enter to look " + "again, or q to quit." if listed else + " Type the card's top folder, Enter to look again, or q to quit.") # Enter looks again, as the screen's Look again does: a card # inserted after the question was never found. - raw = _ask(f"Card root{numbers}, Enter to look again, q to quit: ") - if not raw: + raw = _folder_answer(f"Card root{_numbers_hint(len(listed))}, Enter to look again, " + "q to quit: ", usage, True, len(listed)) + if raw is None: listed = _list_cards(mounted_cards_of(system, plat), blank_cards_of(system, plat), f"Still no {system} card is mounted.") continue - if raw.lower() == "q": - _quit() chosen = raw.isdigit() and 1 <= int(raw) <= len(listed) - if raw.isdigit() and not chosen and not _is_dir(_typed_folder(raw)): - print(f" Type a number from 1 to {len(listed)}, a folder, Enter to look " - "again, or q to quit." if listed else - " Type the card's top folder, Enter to look again, or q to quit.") - continue root = listed[int(raw) - 1] if chosen else _typed_folder(raw).absolute() choice = card_choice(system, plat, root) if choice.refused: @@ -15180,22 +15200,16 @@ def _ask_from_folder(files: list[dict] | None = None, bios_path: Path | None = N print("\nCopy from:") for number, mount in enumerate(drives, 1): print(f" {number}) {card_line(mount)}") - numbers = "" if not drives else " or 1" if len(drives) == 1 else f" or 1 to {len(drives)}" + count = "" if not drives else "1, " if len(drives) == 1 else f"a number from 1 to {len(drives)}, " + # A number out of range, said as one: "2" was read as the folder ./2. + usage = f" Type {count}a folder, or Enter." while True: - raw = _ask(f"Folder, old card or ZIP to copy from{numbers} (Enter to go back, " - "q to quit): ").strip() - if not raw: + raw = _folder_answer(f"Folder, old card or ZIP to copy from{_numbers_hint(len(drives))} " + "(Enter to go back, q to quit): ", usage, True, len(drives)) + if raw is None: return - if raw.lower() == "q": - _quit() if raw.isdigit() and 1 <= int(raw) <= len(drives): label = str(drives[int(raw) - 1]) - elif raw.isdigit() and drives and not _is_dir(_typed_folder(raw)): - # A number out of range, said as one: "2" was read as the folder - # ./2. - count = "1" if len(drives) == 1 else f"a number from 1 to {len(drives)}" - print(f" Type {count}, a folder, or Enter.") - continue else: label = os.path.normpath(str(_typed_folder(raw).absolute())) refused = copy_source_refusal(Path(label), bios_path) @@ -15399,8 +15413,7 @@ def ask_platform_folder(os_type: str, plat: str, ask: bool = True, unproven = "" if shared else unproven_words() if unproven: print(f" {unproven}") - numbers = "" if not targets else (" or 1" if len(targets) == 1 - else f" or 1 to {len(targets)}") + numbers = _numbers_hint(len(targets)) why = ("" if default is not None or shared else " (no default, a folder above could not be opened)" if _default_set_aside(os_type, plat) else " (no default on this system)") @@ -15432,27 +15445,6 @@ def ask_platform_folder(os_type: str, plat: str, ask: bool = True, return folder -def _folder_answer(prompt: str, usage: str, default: bool, numbers: int = 0) -> str | None: - """A folder question's answer: None for Enter on a default, else the - text typed. q quits as at every other question, a folder named q being - typed ./q: q was a folder created in the current one and filled. Enter - with no default, or a number out of range that names no folder, says - what to type: the question came back with no word.""" - while True: - raw = _ask(prompt) - if not raw: - if default: - return None - print(usage) - continue - if raw.lower() == "q": - _quit() - if raw.isdigit() and not 1 <= int(raw) <= numbers and not _is_dir(_typed_folder(raw)): - print(usage) - continue - return raw - - def other_side(os_type: str, plat: str) -> list[tuple[str, Path]]: """(label, folder) of the Windows side's usual folder under WSL, where the one offered first is this Linux's: a Windows RetroArch outside the @@ -17607,9 +17599,8 @@ def finish_app( continue if not interactive or args.yes or LOCAL.offline: continue - answer = _ask(f"\nPut RetroBIOS on this {card.system} card, to check and update " - "its BIOS files from the console later? [y/N] ") - if answer.lower() in ("y", "yes"): + if _answer_yes(f"\nPut RetroBIOS on this {card.system} card, to check and " + "update its BIOS files from the console later? [y/N] "): chosen.append(card) failed = False for card in chosen: diff --git a/install.sh b/install.sh index 50e5237ff..cae3e3302 100755 --- a/install.sh +++ b/install.sh @@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins # curl or wget stop on a cut transfer before the hash is read: the site's # build rewrites this line there (scripts/sitebootstrap.py). MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." -DEFAULT_INSTALL_SHA256="508ffd2832afe8e14dac09860ea73dcb770874c9683f26f8ea0edddb0f7a469b" +DEFAULT_INSTALL_SHA256="c11a387a05a77fd98ad147971abf8ab12bb3caff592f8c28e894adaf57fefa39" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. MAX_INSTALLER_BYTES=8388608 diff --git a/install/launchers/index.json b/install/launchers/index.json index 5b5b7b398..43b3fec31 100644 --- a/install/launchers/index.json +++ b/install/launchers/index.json @@ -27,8 +27,8 @@ { "dest": "roms/ports/retrobios/install.py", "repo_path": "install.py", - "size": 1450474, - "sha256": "508ffd2832afe8e14dac09860ea73dcb770874c9683f26f8ea0edddb0f7a469b", + "size": 1450086, + "sha256": "c11a387a05a77fd98ad147971abf8ab12bb3caff592f8c28e894adaf57fefa39", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1504099, - "sha256": "a968c9ea40f3abb96913e9c816b44a43431c60c8ce23bafefc715c1767259ff8" + "size": 1503711, + "sha256": "c489888589339375e3c62dec4bad0a5a36ab4c8a33c21725b15344099129e709" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1450474, - "sha256": "508ffd2832afe8e14dac09860ea73dcb770874c9683f26f8ea0edddb0f7a469b", + "size": 1450086, + "sha256": "c11a387a05a77fd98ad147971abf8ab12bb3caff592f8c28e894adaf57fefa39", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1450474, - "sha256": "508ffd2832afe8e14dac09860ea73dcb770874c9683f26f8ea0edddb0f7a469b", + "size": 1450086, + "sha256": "c11a387a05a77fd98ad147971abf8ab12bb3caff592f8c28e894adaf57fefa39", "executable": false }, { diff --git a/installer/console.py b/installer/console.py index 2422e8d91..0af8d2e61 100644 --- a/installer/console.py +++ b/installer/console.py @@ -9,7 +9,7 @@ from pathlib import Path from typing import Any, TextIO from installer.events import EVENTS, EVENT_PROGRESS_INTERVAL -from installer.host_fs import _host_path +from installer.host_fs import _host_path, _is_dir from installer.terminal import TUI_MIN_COLS, Terminal, terminal_size from installer.text import _split_word, cell_width, format_duration, format_size @@ -275,6 +275,32 @@ def _answer_yes(prompt: str) -> bool: return _ask(prompt).strip().lower() in ("y", "yes") +def _folder_answer(prompt: str, usage: str, default: bool, numbers: int = 0) -> str | None: + """A folder question's answer: None for Enter on a default, else the + text typed. q quits as at every other question, a folder named q being + typed ./q: q was a folder created in the current one and filled. Enter + with no default, or a number out of range that names no folder, says + what to type: the question came back with no word.""" + while True: + raw = _ask(prompt) + if not raw: + if default: + return None + print(usage) + continue + if raw.lower() == "q": + _quit() + if raw.isdigit() and not 1 <= int(raw) <= numbers and not _is_dir(_typed_folder(raw)): + print(usage) + continue + return raw + + +def _numbers_hint(count: int) -> str: + """" or 1 to N" after a question that takes a listed number, N rows.""" + return "" if not count else " or 1" if count == 1 else f" or 1 to {count}" + + class ReadCounter: """Files read so far, as event_kind events and on the live line: the program driving the installer gets the count the terminal gets.""" diff --git a/installer/destination.py b/installer/destination.py index 9b6a5e4bb..33b4827ca 100644 --- a/installer/destination.py +++ b/installer/destination.py @@ -23,7 +23,7 @@ from installer.cards import ( share_doubt, share_drive, ) -from installer.console import _ask, _quit, _typed_folder, detail +from installer.console import _answer_yes, _folder_answer, _numbers_hint, _typed_folder, detail from installer.detect_os import _DETECTED_OS, card_title, detect_os, detected_os, os_conflict from installer.embedded import CARD_MARKER, CARD_PLATFORMS, _EMBEDDED_RULES, _a, _built_on from installer.events import EVENTS @@ -95,15 +95,14 @@ def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", (print if missing > 1 and ask else detail)( f" {shown_path(dest)} does not exist. The nearest folder that does: " f"{shown_path(nearest)}") - if missing > 1 and ask: - answer = _ask(f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] ") - if not answer.strip().lower().startswith("y"): - if again: - print(" Not created.") - return False - print(f" Nothing installed. Check the folder {named}, then run " - "the command again.") - sys.exit(2) + if missing > 1 and ask and not _answer_yes( + f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] "): + if again: + print(" Not created.") + return False + print(f" Nothing installed. Check the folder {named}, then run " + "the command again.") + sys.exit(2) return True @@ -539,23 +538,18 @@ def card_install_dest(named: str, plat: str, interactive: bool, ask: bool = True else "Mount the card, then type its top folder.")) listed = _list_cards(cards, blank) while True: - numbers = ("" if not listed else " or 1" if len(listed) == 1 - else f" or 1 to {len(listed)}") + usage = (f" Type a number from 1 to {len(listed)}, a folder, Enter to look " + "again, or q to quit." if listed else + " Type the card's top folder, Enter to look again, or q to quit.") # Enter looks again, as the screen's Look again does: a card # inserted after the question was never found. - raw = _ask(f"Card root{numbers}, Enter to look again, q to quit: ") - if not raw: + raw = _folder_answer(f"Card root{_numbers_hint(len(listed))}, Enter to look again, " + "q to quit: ", usage, True, len(listed)) + if raw is None: listed = _list_cards(mounted_cards_of(system, plat), blank_cards_of(system, plat), f"Still no {system} card is mounted.") continue - if raw.lower() == "q": - _quit() chosen = raw.isdigit() and 1 <= int(raw) <= len(listed) - if raw.isdigit() and not chosen and not _is_dir(_typed_folder(raw)): - print(f" Type a number from 1 to {len(listed)}, a folder, Enter to look " - "again, or q to quit." if listed else - " Type the card's top folder, Enter to look again, or q to quit.") - continue root = listed[int(raw) - 1] if chosen else _typed_folder(raw).absolute() choice = card_choice(system, plat, root) if choice.refused: diff --git a/installer/handheld_app.py b/installer/handheld_app.py index b25f18053..4a5bb2bcd 100644 --- a/installer/handheld_app.py +++ b/installer/handheld_app.py @@ -18,7 +18,7 @@ from pathlib import Path, PurePosixPath from typing import NamedTuple from installer.cards import card_system -from installer.console import _ask +from installer.console import _answer_yes from installer.events import EVENTS from installer.host_fs import NOT_WRITABLE, _write_refused from installer.local_check import _sha256_file @@ -393,9 +393,8 @@ def finish_app( continue if not interactive or args.yes or LOCAL.offline: continue - answer = _ask(f"\nPut RetroBIOS on this {card.system} card, to check and update " - "its BIOS files from the console later? [y/N] ") - if answer.lower() in ("y", "yes"): + if _answer_yes(f"\nPut RetroBIOS on this {card.system} card, to check and " + "update its BIOS files from the console later? [y/N] "): chosen.append(card) failed = False for card in chosen: diff --git a/installer/line_folder.py b/installer/line_folder.py index 6af6a7c7f..439751cab 100644 --- a/installer/line_folder.py +++ b/installer/line_folder.py @@ -15,7 +15,7 @@ from installer.cards import ( setup_bios_folder, share_doubt, ) -from installer.console import _ask, _quit, _typed_folder, fold_line +from installer.console import _ask, _folder_answer, _numbers_hint, _quit, _typed_folder, fold_line from installer.destination import ( _last_name, _missing_dest, @@ -49,22 +49,16 @@ def _ask_from_folder(files: list[dict] | None = None, bios_path: Path | None = N print("\nCopy from:") for number, mount in enumerate(drives, 1): print(f" {number}) {card_line(mount)}") - numbers = "" if not drives else " or 1" if len(drives) == 1 else f" or 1 to {len(drives)}" + count = "" if not drives else "1, " if len(drives) == 1 else f"a number from 1 to {len(drives)}, " + # A number out of range, said as one: "2" was read as the folder ./2. + usage = f" Type {count}a folder, or Enter." while True: - raw = _ask(f"Folder, old card or ZIP to copy from{numbers} (Enter to go back, " - "q to quit): ").strip() - if not raw: + raw = _folder_answer(f"Folder, old card or ZIP to copy from{_numbers_hint(len(drives))} " + "(Enter to go back, q to quit): ", usage, True, len(drives)) + if raw is None: return - if raw.lower() == "q": - _quit() if raw.isdigit() and 1 <= int(raw) <= len(drives): label = str(drives[int(raw) - 1]) - elif raw.isdigit() and drives and not _is_dir(_typed_folder(raw)): - # A number out of range, said as one: "2" was read as the folder - # ./2. - count = "1" if len(drives) == 1 else f"a number from 1 to {len(drives)}" - print(f" Type {count}, a folder, or Enter.") - continue else: label = os.path.normpath(str(_typed_folder(raw).absolute())) refused = copy_source_refusal(Path(label), bios_path) @@ -268,8 +262,7 @@ def ask_platform_folder(os_type: str, plat: str, ask: bool = True, unproven = "" if shared else unproven_words() if unproven: print(f" {unproven}") - numbers = "" if not targets else (" or 1" if len(targets) == 1 - else f" or 1 to {len(targets)}") + numbers = _numbers_hint(len(targets)) why = ("" if default is not None or shared else " (no default, a folder above could not be opened)" if _default_set_aside(os_type, plat) else " (no default on this system)") @@ -301,27 +294,6 @@ def ask_platform_folder(os_type: str, plat: str, ask: bool = True, return folder -def _folder_answer(prompt: str, usage: str, default: bool, numbers: int = 0) -> str | None: - """A folder question's answer: None for Enter on a default, else the - text typed. q quits as at every other question, a folder named q being - typed ./q: q was a folder created in the current one and filled. Enter - with no default, or a number out of range that names no folder, says - what to type: the question came back with no word.""" - while True: - raw = _ask(prompt) - if not raw: - if default: - return None - print(usage) - continue - if raw.lower() == "q": - _quit() - if raw.isdigit() and not 1 <= int(raw) <= numbers and not _is_dir(_typed_folder(raw)): - print(usage) - continue - return raw - - def other_side(os_type: str, plat: str) -> list[tuple[str, Path]]: """(label, folder) of the Windows side's usual folder under WSL, where the one offered first is this Linux's: a Windows RetroArch outside the diff --git a/tests/test_install.py b/tests/test_install.py index d72801373..cbc689188 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -577,6 +577,12 @@ class AMissingDestIsSaid(unittest.TestCase): self.assertFalse((root / "retroarh").exists()) self.assertEqual(self._call(dest, True, "y")[1], None) + def test_only_y_or_yes_creates_the_folders(self): + # A default-no question read "yeah" as yes, as no other one does. + root = Path(_tempdir(self)) + self.assertEqual(self._call(root / "a" / "b", True, "yeah")[1], 2) + self.assertFalse((root / "a").exists()) + def test_one_missing_level_or_no_terminal_is_said_not_asked(self): root = Path(_tempdir(self)) # A "no" that was asked would stop the run with 2. Said with diff --git a/tests/test_install_tui.py b/tests/test_install_tui.py index 1638bc7e5..c4a8ed899 100644 --- a/tests/test_install_tui.py +++ b/tests/test_install_tui.py @@ -8385,6 +8385,15 @@ class LineModeAndScreenDecideAlike(_LineModeFindings, _OutsideTextFindings, self.assertIn("Type 1, a folder, or Enter.", out.getvalue()) self.assertNotIn("does not exist", out.getvalue()) + def test_a_number_with_no_drive_listed_is_said_as_one(self): + # With no drive listed, "2" was read as the folder ./2. + with mock.patch.object(install, "_mounted_cards", return_value=[]), \ + mock.patch.object(install, "_ask", side_effect=["2", ""]), \ + contextlib.redirect_stdout(io.StringIO()) as out: + install._ask_from_folder([], self.here) + self.assertIn("Type a folder, or Enter.", out.getvalue()) + self.assertNotIn("does not exist", out.getvalue()) + def test_the_copy_line_names_the_folder_it_copies_from(self): payload = b"gba" files = [self.entry("gba_bios.bin", payload, ["nintendo-gba"])]