diff --git a/install.ps1 b/install.ps1 index 65916f5c7..c6a28f116 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 = "e61c63aa2277919b50c0b952236979c4bf67630cecc7d58315bb8fe8e84d4673" + $defaultInstallSha256 = "fdc57a4a1625e1ccc69f247423bb471dd76e59bd9ee902b9fad5d0a3bee632ed" # 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 08fca1c74..f2191a1ec 100644 --- a/install.py +++ b/install.py @@ -29395,7 +29395,7 @@ def tui_view(state: TuiState, cols: int, rows: int) -> list[str]: return lines -# --- installer/tui_actions.py ------------------------------------------------ +# --- installer/tui_steps.py -------------------------------------------------- def _choose_board(state: TuiState, name: str) -> list[tuple]: """A board chosen on Device, back on Change. The board the run reads @@ -29846,6 +29846,8 @@ def tui_interrupt(state: TuiState) -> list[tuple]: return tui_quit(state, asked=False) +# --- installer/tui_folder_actions.py ----------------------------------------- + def _closed_status(state: TuiState, closed: tuple[Path, str]) -> TuiLine: folder = _short_path(closed[0], state.home) return _styled(f"This user cannot open {folder} ({closed[1]}): choose another folder, " @@ -30304,6 +30306,15 @@ def _activate_cardroot(state: TuiState, row: TuiRow) -> list[tuple]: return _back(state) +def _platform_focus(state: TuiState, plat: str) -> int: + """The platform list's row for plat, below its filter row.""" + rows = _platform_rows(state) + return next((index for index, row in enumerate(rows) + if row.action == "platform" and row.value == plat), 1) + + +# --- installer/tui_actions.py ------------------------------------------------ + def _plan_close(state: TuiState, row: TuiRow) -> list[tuple]: """Close on the plan: every file in place.""" state.exit_code = 0 @@ -30522,13 +30533,6 @@ def _checklist(state: TuiState, row: TuiRow) -> list[tuple]: return [] -def _platform_focus(state: TuiState, plat: str) -> int: - """The platform list's row for plat, below its filter row.""" - rows = _platform_rows(state) - return next((index for index, row in enumerate(rows) - if row.action == "platform" and row.value == plat), 1) - - def _option_flag(state: TuiState, action: str, step: int) -> bool: """A yes or no of the options, or the jobs, set: whether action is one.""" if action == "game_data": diff --git a/install.sh b/install.sh index 36f932377..f9d068782 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="e61c63aa2277919b50c0b952236979c4bf67630cecc7d58315bb8fe8e84d4673" +DEFAULT_INSTALL_SHA256="fdc57a4a1625e1ccc69f247423bb471dd76e59bd9ee902b9fad5d0a3bee632ed" # 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 951018b3a..b22c8a060 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": 1501966, - "sha256": "e61c63aa2277919b50c0b952236979c4bf67630cecc7d58315bb8fe8e84d4673", + "size": 1502128, + "sha256": "fdc57a4a1625e1ccc69f247423bb471dd76e59bd9ee902b9fad5d0a3bee632ed", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1555591, - "sha256": "1cef48c6a0ff4479dbebe3e7626569928f91c155cee6d30e4ec1c67baa8e8ed3" + "size": 1555753, + "sha256": "07394a7c63aa88b10680a90b8916615f9d83e161d1d05376fb48473659e80d2d" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1501966, - "sha256": "e61c63aa2277919b50c0b952236979c4bf67630cecc7d58315bb8fe8e84d4673", + "size": 1502128, + "sha256": "fdc57a4a1625e1ccc69f247423bb471dd76e59bd9ee902b9fad5d0a3bee632ed", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1501966, - "sha256": "e61c63aa2277919b50c0b952236979c4bf67630cecc7d58315bb8fe8e84d4673", + "size": 1502128, + "sha256": "fdc57a4a1625e1ccc69f247423bb471dd76e59bd9ee902b9fad5d0a3bee632ed", "executable": false }, { diff --git a/installer/tui_actions.py b/installer/tui_actions.py index 5c596f63e..85a788d04 100644 --- a/installer/tui_actions.py +++ b/installer/tui_actions.py @@ -1,1003 +1,64 @@ -"""What choosing a row does on each screen, and the moves between screens.""" +"""What choosing a row does on the plan, option, checklist and closing screens, by screen.""" from __future__ import annotations -import os -import shutil from collections.abc import Callable from pathlib import Path from typing import TYPE_CHECKING -from installer.cards import ( - _card_boundary, - _mount_name, - _mount_points, - _mounted_cards, - card_bound, - card_platform, - card_setup, - card_system, -) -from installer.choices import emulator_names, known_regions, trim_recorded -from installer.console import QUIT_CODE, _typed_folder -from installer.cores import CORE_PLATFORMS, core_dirs, installed_cores from installer.destination import ( - _card_is, _last_name, - card_choice, - card_root_above, - copy_source_refusal, - dest_drive, - missing_folder, - mounted_cards_of, - on_its_card, ) -from installer.detect_os import _DETECTED_OS, _FROM_CARD, detected_os, detected_shown -from installer.embedded import CARD_PLATFORMS, _a -from installer.frontend_folders import _retroarch_config_at from installer.games import games_probe -from installer.host_fs import _exists, _folder_writable, _is_dir, _nearest_folder, closed_folder -from installer.install_record import ( - RECORD_NARROWING, - _RECORD_SAID, - choice_words, - options_without, - record_options, - setup_record, - take_recorded_choice, -) -from installer.lists import local_targets +from installer.host_fs import _is_dir, _nearest_folder from installer.offline import LOCAL from installer.pack import _listed from installer.pack_source import PackSource -from installer.paths import shown_path -from installer.platforms import _tui_system_label, display_name -from installer.regions import resolve_regions -from installer.setups import setup_folder +from installer.platforms import display_name from installer.tui_choice import ( - _board_seen, - _choice_in_place, _cores_dir_given, - _counts_predate_options, - _dest_card, _device_choice, _needed, - _no_setup_reads, - _none_ticked, _save_dir_given, - _short_of_space, - _system_titles, - _tui_card_bound, core_ticked, tui_choices, ) -from installer.tui_commands import _tui_card, tui_flags, tui_pack_dir, tui_record_kept -from installer.tui_folder_scan import card_mounts, scan_folder -from installer.tui_paint import _CHECK_FIELDS, _short_path, _styled +from installer.tui_commands import _tui_card, tui_flags, tui_record_kept +from installer.tui_paint import _CHECK_FIELDS, _styled from installer.tui_rows import ( TUI_TICK_ROWS, _ACTION_SCREENS, _CHECKLISTS, - _browse_rows, _device_rows, _first_item, - _os_folder, - _platform_rows, tui_rows, ) from installer.tui_screens import _only_match from installer.tui_state import TuiRow if TYPE_CHECKING: - from installer.destination import CardChoice - from installer.tui_paint import TuiLine from installer.tui_state import TuiState - - -def _choose_board(state: TuiState, name: str) -> list[tuple]: - """A board chosen on Device, back on Change. The board the run reads - itself is no option: the command stays the one with nothing chosen. - Every board is always said, the run otherwise reading a board itself - (one started with --target or --every-board read none).""" - seen = _board_seen(state) - read = seen[0] if seen is not None and seen[1] else "" - state.every_board = not name - state.target = "" if not name or name == read else name - return _back(state) - - -def _focus_plan(state: TuiState, action: str) -> None: - """The plan's focus on the row doing action. A plan that keeps no file, - a choice emptied or already in place, opens on Choose systems: the - focus kept its index, and Enter opened Options.""" - actions = [row.action for row in tui_rows(state)] - keeps_none = _choice_in_place(state) or (state.emptied and state.systems - and not state.ticked) - if action in actions: - state.focus["plan"] = actions.index(action) - elif keeps_none and "systems" in actions: - state.focus["plan"] = actions.index("systems") - - -def _push(state: TuiState, screen: str, focus: int = 0) -> list[tuple]: - state.stack.append(screen) - state.focus[screen] = focus - return [] - - -def _replace(state: TuiState, screen: str) -> None: - state.stack[-1] = screen - state.focus[screen] = 0 - - -def _back(state: TuiState) -> list[tuple]: - if state.screen == "packdone" and not state.running and "more" in state.stack: - # Made or not, its plan is spent: back to More. - del state.stack[state.stack.index("more") + 1:] - return [] - if state.screen == "actdone" and not state.running: - # Done: what was planned is gone, and the plan is counted again. - return _activate_action(state, TuiRow("recheck", "")) - effects = [("kill",)] if state.running else [] - if state.running: - # What the stopped run still reports belongs to no screen. - state.running = False - state.run_id += 1 - if len(state.stack) == 1: - state.stack.append("quit") - state.focus["quit"] = 0 - return effects - refused = _none_ticked(state) - if refused: - # Done and B leave a list of ticks alike, never with nothing - # ticked: said on every length of list, in the line the rows - # leave. - state.status = refused - return effects - chose = state.screen == "systems" and state.ticked != (state.ticked_before or set()) - if state.screen == "systems" and state.widened_from is not None: - before, state.widened_from = state.widened_from, None - if state.ticked == {row["system"] for row in before["systems"]}: - # The systems --system named, ticked again: the exact plan of - # their own check, not an estimate over every system. - for field, value in before.items(): - setattr(state, field, value) - chose = False - if state.screen == "regions": - state.region_order = [] - state.stack.pop() - if chose and state.screen == "plan": - # The systems chosen, the next press is Install: Back left the focus - # on Choose systems. - _focus_plan(state, "install") - if state.screen == "checking" and not state.running: - # Stay after Esc on a check started from the command line: a - # Checking screen nothing feeds counted seconds forever. - return effects + _start_check(state) - if state.screen == "plan" and _counts_predate_options(state): - # The plan's numbers were read under the options before: they are - # read again, the systems ticked kept. - return effects + _start_check(state) - return effects - - -def _keep_choice(state: TuiState) -> None: - """The systems ticked, kept for the next check of the same platform's - list when they are not every system to install: one choice for the - whole run, whatever is changed on the way (game data, an option, a - folder, a folder to copy from). A choice already set aside (widening a - --system) is left as it is.""" - if (state.keep_ticked is not None or not state.systems - or state.manifest_for != state.platform): - return - if state.emptied and not state.ticked: - # Still the choice the options emptied: the same systems again. - state.keep_ticked = set(state.ticked_names) - return - needed = {row["system"] for row in state.systems if _needed(row)} - state.keep_ticked = (None if needed <= state.ticked and not state.hidden_ticks - else state.ticked | state.hidden_ticks) - titles = _system_titles(state) - state.ticked_names = { - **{name: label for name, label in state.ticked_names.items() - if name in state.hidden_ticks}, - **{row["system"]: _tui_system_label(row, titles) - for row in state.systems if row["system"] in state.ticked}} - - -def _start_run(state: TuiState, run: str) -> list[tuple]: - state.run = run - state.running = True - state.run_id += 1 - state.started = state.now - if run == "check": - # The systems of an earlier check go first: ticks read against them - # dropped the --system the command line gave. - state.check, state.files, state.systems, state.reading = None, [], [], None - state.checked_dests = set() - state.choices = state.set_aside = None - state.found, state.scanned, state.unreadable = [], [], [] - state.check_summary = state.saves = None - state.unplaced_why = {} - state.version = state.version_short = state.board = "" - state.games = state.games_reading = state.games_unavailable = None - state.checked_flags = tui_flags(state) - elif run in ("confirm", "preview"): - state.dry, state.dry_files, state.dry_summary = None, [], None - state.dry_saves = None - else: - state.done_saves = state.dest_error = None - state.download = state.progress = state.reading = state.games_reading = None - state.samples, state.local_dests, state.network = [], {}, 0 - state.finished, state.summary, state.code, state.stopping = [], None, None, False - return [("run", run)] - - -def _tui_record_take(state: TuiState) -> None: - """The recorded choice made the screen's, as its options would make it.""" - setup = state.record or {} - taken = take_recorded_choice(setup.get("choice") or []) - if taken is None: - state.status = "The install record names options this installer lacks, so it is ignored." - state.record = None - return - parsed = taken.parsed - left_out, choice = list(taken.left_out), taken.choice - recorded_regions = list(parsed.region or []) - regions = known_regions(recorded_regions) - if regions != recorded_regions: - # One region at a time, as line mode keeps them: a known region - # beside an unknown one stays. - left_out.append("--region (a region this installer does not know)") - choice = choice_words(options_without(record_options(choice), "--region", regions)) - if left_out: - # What the run takes, as line mode keeps it: the plan reads as the - # record's own choice again. - setup = state.record = {**setup, "choice": choice} - state.status = f"Left out of the recorded choice: {', '.join(left_out)}." - state.cli_systems = list(parsed.system or []) - state.cores = [token.strip() for value in parsed.core or [] for token in value.split(",") - if token.strip()] - state.regions = resolve_regions(regions) if regions else [] - state.no_game_data = bool(parsed.no_game_data) - state.installed_only = bool(parsed.installed_cores) - state.for_games = parsed.for_games is not None - state.games_folder = parsed.for_games if isinstance(parsed.for_games, Path) else None - state.required_only = bool(parsed.required_only) and state.for_games - state.games_choice = "record" - # Device and Emulators show these: a choice no row showed was kept apart. - state.target = str(parsed.target or "") - state.every_board = bool(parsed.every_board) - state.emulators = list(parsed.emulator or []) - state.record_flags = [(flag, value) for flag, value in record_options(setup["choice"]) - if flag == "--declared-only"] - if state.manifest is not None and state.manifest_for == state.platform: - _tui_record_trim(state, state.platform, state.manifest) - - -def _tui_record_trim(state: TuiState, plat: str, manifest: dict) -> None: - """The systems, cores, regions and emulators of the recorded choice - that this list cannot take, left out by the rule line mode follows - (trim_recorded) and said on the status line.""" - if state.record is None: - return - asked = {"--system": list(state.cli_systems), "--core": list(state.cores), - "--region": list(state.regions), "--emulator": list(state.emulators)} - parts = trim_recorded(asked, plat, manifest, manifest.get("files") or [], - manifest.get("omitted_files") or []) - if not parts: - return - choice = record_options(state.record["choice"]) - for part in parts: - if part.flag == "--system": - state.cli_systems = list(part.keep) - elif part.flag == "--core": - state.cores = list(part.keep) - elif part.flag == "--region": - state.regions = list(part.keep) - else: - state.emulators = list(part.keep) - choice = options_without(choice, part.flag, part.keep) - state.record = {**state.record, "choice": choice_words(choice)} - words = ", ".join(f"{part.flag} ({part.why})" for part in parts) - state.status = f"Left out of the recorded choice: {words}." - - -def _tui_record_drop(state: TuiState) -> None: - """The recorded choice given up: the choice nothing narrows.""" - state.cli_systems, state.cores, state.regions, state.record_flags = [], [], [], [] - state.no_game_data = state.installed_only = state.required_only = False - state.target, state.every_board, state.emulators = "", False, [] - state.for_games, state.games_folder, state.games_choice = None, None, "" - # The systems of the recorded check go, and with them its ticks. - state.systems, state.ticked, state.keep_ticked, state.widened_from = [], set(), None, None - - -def _tui_record_load(state: TuiState) -> None: - """Read the record of the setup and folder chosen, once for each, and - take its choice where nothing chose: the command line, or the screen - for an earlier folder.""" - key = (state.platform, state.dest) - if state.dest is None or state.record_for == key: - return - taken = tui_record_kept(state) - state.record_for = key - setup, path, problem = setup_record(state.platform, state.dest) - if problem and str(path) not in _RECORD_SAID: - _RECORD_SAID.add(str(path)) - state.status = f"{problem}, so it is ignored." - if taken: - _tui_record_drop(state) - # Games read on a card are what the plan found, not a choice. - found = ("--for-games", "--required-only") if state.games_choice in ("", "probe") else () - state.record = None - untouched = not any(flag in RECORD_NARROWING and flag not in found - for flag, _value in tui_flags(state)) - state.record = setup - if setup is not None and not state.cli_choice and untouched: - _tui_record_take(state) - - -def _start_check(state: TuiState) -> list[tuple]: - """Check the chosen folder, after fetching the file list once, the - systems ticked kept (_keep_choice).""" - _tui_record_load(state) - _keep_choice(state) - _games_look(state) - state.refocus = "" - if "plan" in state.stack: - del state.stack[state.stack.index("plan"):] - while state.stack and state.stack[-1] in ("checking", "error", "create"): - state.stack.pop() - state.stack.append("checking") - state.facts, state.ticked = {}, set() - if state.manifest is not None and state.manifest_for == state.platform: - return _start_run(state, "check") - state.manifest = None - state.run, state.run_id = "check", state.run_id + 1 - state.started = state.now - return [("fetch", state.platform)] - - -def _games_look(state: TuiState) -> None: - """For a folder chosen once a card was looked at for games, the new one - looked at too, from what this run already read (no network): its games - make "For the games on this card" the choice, unless the user or the - command line decided.""" - if (state.games_choice in ("user", "cli", "record") or state.games_probed is None - or state.dest is None or state.games_probed == (state.platform, state.dest)): - return - count = games_probe(state.platform, state.dest, network=False, os_type=state.os_type) - state.games_probed = (state.platform, state.dest) - state.games_here = count or 0 - state.for_games, state.games_choice = bool(count), "probe" - - -def _browse_to(state: TuiState, folder: Path | None, came_from: Path | None = None) -> None: - state.browse_dir = folder - copying = state.browse_for == "from" - state.browse_rows, state.status = _browse_rows(folder, copying or state.browse_for == "pack", - _dest_card(state) if copying else None) - if state.browse_for == "pack": - # A pack is a ZIP, never a folder. - state.browse_rows = [row for row in state.browse_rows if row.action != "use"] - focus = 0 - if (folder is not None and state.browse_for not in ("from", "pack", "packto") - and folder in (state.home, Path(folder.anchor))): - # No frontend reads its BIOS files from HOME or a drive's root: A - # pressed through the screens installed into ~. A goes into a - # visible subfolder there, never a card or drive listed first (A - # then took /mnt/d, the Windows D: drive, for the BIOS folder) nor - # .cache. With none, Use this folder keeps the focus and says where. - focus = next((index for index, row in enumerate(state.browse_rows) - if row.action == "dir" and isinstance(row.value, Path) - and row.label == f"{row.value.name}/" - and not _os_folder(row.value, folder)), 0) - if not focus and not state.status: - state.status = _no_setup_reads(state, folder) - # Never on the row the sentence warns against: A, A, A put a - # BizHawk install into the home folder. The first folder below - # it, hidden ones too, else the folder above. - focus = next((index for index, row in enumerate(state.browse_rows) - if row.action == "dir" and isinstance(row.value, Path) - and row.label == f"{row.value.name}/"), 0) or next( - (index for index, row in enumerate(state.browse_rows) - if row.action == "up"), 0) - if (folder is not None and state.browse_for == "from" and came_from is None - and folder in (state.home, Path(folder.anchor))): - # The old card is most often a mounted one: the docs say the copy - # browser opens on them, and A on Use this folder read all of HOME. - # Not the card being installed to: it was focused first, and taken. - mounts = [mount for mount in _mounted_cards() if mount != _dest_card(state)] - focus = next((index for index, row in enumerate(state.browse_rows) - if row.action == "dir" and row.value in mounts), focus) - for index, row in enumerate(state.browse_rows): - if came_from is not None and row.action == "dir" and row.value == came_from: - focus = index - state.focus["browse"] = focus - - -def _open_backups(state: TuiState, purpose: str) -> list[tuple]: - """The backups of the folder, read by a child: to look at them, or to - put one back or delete one.""" - state.backups_for, state.backups = purpose, None - _push(state, "backups") - return _start_run(state, "backups") - - -def _activate_pack_action(state: TuiState, row: TuiRow) -> list[tuple] | None: - """A and Enter on the pack screens, None on another row.""" - screen, action = state.screen, row.action - if screen == "more" and action == "packmake": - state.pack_plan = None - _push(state, "packplan") - return _start_run(state, "pack_plan") - if screen == "packplan" and action == "packwhere": - state.browse_for = "packto" - _push(state, "browse") - _browse_to(state, _nearest_folder(tui_pack_dir(state))) - return [] - if screen == "packplan" and action == "packgo": - state.pack_done, state.pack_code, state.pack_fetched = None, None, None - _push(state, "packdone") - return _start_run(state, "pack") - if screen == "packdone" and action == "close": - state.exit_code = state.pack_code if state.pack_code is not None else 1 - return [] - return None - - -def _activate_removal_action(state: TuiState, row: TuiRow) -> list[tuple] | None: - """A and Enter on More and the removal screens, None on another row.""" - screen, action = state.screen, row.action - if screen == "more" and action == "remove": - return _push(state, "remove") - if screen == "more": - return _open_backups(state, {"backups": "look", "putback": "restore", - "dropbackup": "delete_backup"}[action]) - if screen == "remove": - state.removal_scope = ([("--system", str(row.value))] if action == "rmsystem" - else [("--misplaced", "")] if action == "rmmisplaced" - else []) - state.removal = None - _push(state, "removal") - return _start_run(state, "removal_plan") - if screen == "removal" and action == "rmbackup": - state.removal_backup = not state.removal_backup - return [] - if screen == "removal" and action == "rmgo": - state.action = "remove" - return _push(state, "actconfirm") - return None - - -def _activate_backup_action(state: TuiState, row: TuiRow) -> list[tuple] | None: - """A and Enter on the backup and action screens, None on another row.""" - screen, action = state.screen, row.action - if screen == "backups" and action == "backup": - state.backup_chosen = (state.backups or [])[int(str(row.value))] - if state.backups_for == "look": - return _push(state, "backup") - state.action = state.backups_for - return _push(state, "actconfirm") - if screen == "backup": - state.action = "restore" if action == "bkrestore" else "delete_backup" - return _push(state, "actconfirm") - if screen == "actconfirm" and action == "actgo": - state.action_events, state.action_code = [], None - _replace(state, "actdone") - return _start_run(state, state.action) - if screen == "actdone" and action == "close": - state.exit_code = state.action_code if state.action_code is not None else 1 - return [] - if screen == "actdone" and action == "recheck": - # The record changed with the folder: read again, with its choice. - state.record_for = None - del state.stack[1:] - state.stack.append("checking") - return _start_check(state) - return None - - -def _activate_action(state: TuiState, row: TuiRow) -> list[tuple]: - """A and Enter on the More screens.""" - if row.action == "back": - return _back(state) - for activate in (_activate_pack_action, _activate_removal_action, _activate_backup_action): - effects = activate(state, row) - if effects is not None: - return effects - return [] - - -def _open_details(state: TuiState) -> list[tuple]: - if state.screen == "result": - # Looked at, they are said again in the terminal after Exit. - state.details_seen = True - return _push(state, "details") - - -def tui_quit(state: TuiState, code: int | None = None, asked: bool = True) -> list[tuple]: - """Leave now: with the install's own code once it ran, else QUIT_CODE - for a quit the user chose (q, Quit) and 130 for Ctrl-C. asked is a quit - the user chose, said as one: it read "stopped", the word of an - interrupted run.""" - state.quit_asked = asked - if code is None: - code = QUIT_CODE if asked else 130 - if state.screen == "result": - code = state.code if state.code is not None else 1 - elif state.screen == "error": - code = state.error_code - state.exit_code = code - return [("kill",)] if state.running else [] - - -def tui_interrupt(state: TuiState) -> list[tuple]: - """Ctrl-C: an install stops and says what it did, anything else ends.""" - if state.run == "install" and state.running: - if state.stopping: - return [("kill",)] - state.stopping = True - return [("stop",)] - return tui_quit(state, asked=False) - - -def _closed_status(state: TuiState, closed: tuple[Path, str]) -> TuiLine: - folder = _short_path(closed[0], state.home) - return _styled(f"This user cannot open {folder} ({closed[1]}): choose another folder, " - "or give this user access to it.", "refusal") - - -def _set_platform(state: TuiState, plat: str) -> None: - """Another setup chosen: its list names other boards and emulators, so - the ones chosen for the setup before are not carried over.""" - if state.platform and plat != state.platform: - state.target, state.every_board, state.emulators = "", False, [] - state.targets, state.targets_for, state.targets_problem = None, "", "" - state.platform = plat - - -def _known_choice(state: TuiState, plat: str, manifest: dict) -> None: - """The emulators and the board chosen, kept to those the list now read - names, the others left out and said in one line: a pack's list can - lack what the network's held, and the check then failed.""" - if tui_record_kept(state): - _tui_record_trim(state, plat, manifest) - names = emulator_names(manifest, manifest.get("files") or [], - manifest.get("omitted_files") or []) - gone = [f"--emulator {name}" for name in state.emulators if name not in names] - state.emulators = [name for name in state.emulators if name in names] - boards = state.targets if state.targets_for == plat else None - if LOCAL.manifest is not None or LOCAL.pack is not None: - try: - boards = local_targets(plat) - except SystemExit: - # A board list beside the list that is no list: the check says so. - boards = None - if state.target and boards is not None and state.target not in boards: - gone.append(f"--target {state.target}") - state.target = "" - if gone: - state.status = f"Left out, not in this list: {', '.join(gone)}." - - -def _choose(state: TuiState, plat: str, dest: Path, proposed: bool = False) -> list[tuple]: - """dest taken for plat. proposed is a folder the installer offered (its - usual or detected folder, a share drive's bios): created without the - guard a typed path gets, which Enter on it met as Choose another folder, - and Enter there brought the same two screens back.""" - state.card_note = "" - closed = closed_folder(dest) - if closed is not None: - # Nothing can be checked or written there: the folder is asked - # again, on the screen that chose it when it can say why. - _set_platform(state, plat) - state.status = _closed_status(state, closed) - if state.stack and state.screen in ("folder", "browse", "path"): - return [] - # The folder found here is the one the frontend reads: the built-in - # default beside it is one it does not, offered as "reads its BIOS - # files from" and filled with Enter. - state.folder_default, state.folder_kind = None, "closed" - state.folder_scan = scan_folder(state) - return _push(state, "folder") - card = card_system(dest) - if card and card[0] == plat and card[1] != dest: - # A muOS or KNULLI card's root: that system reads a folder below it. - dest = card[1] - _set_platform(state, plat) - state.dest = dest - if plat in CARD_PLATFORMS and card_platform(dest) is None: - # The engine refuses a folder inside the card, the list writing - # Bios// below the root: the root is offered instead. - for parent in list(dest.absolute().parents)[:2]: - holder = card_platform(parent) - if holder: - state.facts = {"card_root": parent, "card": holder} - return _push(state, "cardroot") - root = _own_card_root(dest) - if root is not None and not _exists(dest) and _is_dir(dest.parent): - # The card's own files name it, so its folder is known: created as - # line mode creates it, and said on the plan, not asked. - state.card_note = (f"{dest.name} is created on {_mount_name(root)}, where " - f"{detected_shown(plat, dest) or display_name(plat)} reads it.") - return _start_check(state) - if proposed and not _exists(dest): - # Where no setup is there yet, nothing reads it: said as it is. - reads = (f", where {detected_shown(plat, dest) or display_name(plat)} reads it" - if _is_dir(dest.parent) else ", a folder that does not exist yet") - state.card_note = (f"{dest.name} is created in " - f"{_short_path(dest.parent, state.home)}{reads}.") - return _start_check(state) - found = missing_folder(dest) - if found is None: - return _start_check(state) - if found.verdict == "create": - # One level is a BIOS folder not made yet: line mode makes it and - # says so, the plan says so here. - state.card_note = f"{dest.name} is created in {_short_path(dest.parent, state.home)}." - return _start_check(state) - # Asked with no as the answer, as line mode asks. A path whose start - # was mistyped would be created from the root: said on the screen. - state.facts = {"nearest": found.nearest, "risky": found.nearest.parent == found.nearest} - return _push(state, "create", 1) - - -def _card_folder(state: TuiState, folder: Path) -> CardChoice | None: - """What a folder chosen for a setup that runs from its SD card resolves - to, as a card root typed in line mode does: None when no card is asked - for, on the device itself, whose folder is taken as typed (a KNULLI got - bios added to it), or when the folder is inside a MinUI card, whose - root _choose offers.""" - bound = _tui_card_bound(state) - if bound is None and state.platform: - # Read as line mode reads a folder typed (ask_platform_folder): a - # Batocera SHARE drive's root goes to its bios folder, another - # system's drive is refused, a card's root is said. - return card_choice(state.platform, state.platform, folder) - if bound is None or (state.platform in CARD_PLATFORMS and card_root_above(folder)): - return None - if state.folder_kind == "found" and state.folder_default is not None: - return None - return card_choice(bound[0], state.platform, folder) - - -def _card_status(state: TuiState, choice: CardChoice, given: bool = False) -> None: - """Say why a folder is not the card: one short sentence first, which a - 40-column screen shows whole, the folder and what was looked for on it - after, shown where there is room and in the help. given is a folder - --dest names, which no choice here can change.""" - if given: - # --dest is fixed for this run: only another system's card gets - # "choose another setup". A folder inside a card, or one that is no - # card, is named again in the command. - then = (f"Run it again with --dest {shown_path(choice.root)}." if choice.root - else "--dest names it: choose another setup." if choice.other - else "Run it again with the card's top folder.") - short = choice.short or choice.refused + "." - elif choice.other: - # The top folder of a card, only another system's: "Choose the - # card's top folder" sent the user back to the same one. - bound = _tui_card_bound(state) - kind = "SHARE drive" if "SHARE drive" in choice.short else "card" - then = (f"Choose the {display_name(state.platform)} SHARE drive, or go back and " - f"choose {choice.other}." if kind != "card" and not bound else - f"Insert the {bound[0] if bound else 'right'} card, or go back and choose " - f"{choice.other}.") - short = f"That is {_a(choice.other)} {kind}." - else: - then = "Choose the card's top folder." - short = choice.short or choice.refused + "." - state.status = _styled(f"{short} {then}", "refusal") - state.status_more = f"{choice.refused}." + (f" {choice.detail}." if choice.detail else "") - - -def _card_refusal(state: TuiState, folder: Path) -> bool: - """Whether folder is refused as the card the chosen setup runs from, - said in the status when it is.""" - choice = _card_folder(state, folder) - if choice is None or not choice.refused: - return False - _card_status(state, choice) - return True - - -def _choose_card(state: TuiState, folder: Path) -> list[tuple]: - """_choose for a folder picked on the folder, browse or path screen: for - a setup that runs from its SD card, the card's own BIOS folder, and a - folder that is not its card refused in words.""" - choice = _card_folder(state, folder) - if choice is None: - return _choose(state, state.platform, folder) - if choice.refused: - _card_status(state, choice) - if state.stack and state.screen in ("folder", "browse", "path"): - return [] - state.folder_default, state.folder_kind = None, "usual" - state.folder_scan = scan_folder(state) - return _push(state, "folder") - return _take_card(state, choice, folder) - - -def _own_card_root(folder: Path) -> Path | None: - """The root of the card whose own files name folder as its BIOS folder, - or None: an Onion card's BIOS, a muOS card's MUOS/bios. No folder above - a mount point is read, as _card_above reads none: it is another drive.""" - points = _mount_points() - below = folder - for parent in list(folder.parents)[:2]: - if _card_boundary(below, points): - return None - below = parent - card = card_system(parent) - if card and card[1] == folder: - return parent - return None - - -def _take_card(state: TuiState, choice: CardChoice, root: Path) -> list[tuple]: - """_choose for the folder card_choice gave root, its note kept on the - screens up to the plan: --dest, the setup list with --dest and a folder - picked here said nothing of a stick no file names, where line mode did.""" - card = card_system(root) - if card and card[1] == choice.folder: - # Titled as the same card found by itself: "Onion (RetroArch files)". - _DETECTED_OS.setdefault((choice.plat, choice.folder), card[2]) - _FROM_CARD.add((choice.plat, choice.folder)) - elif state.card_bound: - # A card no file names, chosen as ArkOS's: titled ArkOS, not RetroArch. - _DETECTED_OS.setdefault((choice.plat, choice.folder), state.card_bound[0]) - # The system's folder below a card that exists is made as line mode - # makes it (_missing_dest, one level): the screen asked Create the - # folder? for KNULLI's bios on a blank stick, line mode did not. - below = choice.folder != root and _is_dir(choice.folder.parent) - effects = _choose(state, choice.plat, choice.folder, proposed=below) - # Said once: a note that names the folder already says where the files go. - named = bool(choice.note) and shown_path(choice.folder) in choice.note - state.card_note = choice.note if named else " ".join(filter(None, (choice.note, - state.card_note))) - return effects - - -def _open_folder_screen(state: TuiState, default: Path | None, - kind: str = "usual", rebind: bool = False) -> list[tuple]: - """The folder screen for the chosen setup. rebind is another folder - asked for from the plan: a card setup found mounted (an Onion card) is - asked for its card again, never for any folder. A folder off the card - is one Onion does not read, and the screen said RetroArch.""" - state.folder_default, state.folder_kind = default, kind - # On the device itself its folder is no card in a reader. - system = (detected_os(state.platform, state.dest) - if rebind and state.dest is not None and dest_drive(state.dest) is not None - else "") - if system and not state.card_bound and card_bound(system, state.platform): - state.card_bound = card_bound(system, state.platform) - # The drives are looked for when the screen opens and on Look again: a - # card inserted meanwhile moved the rows under the focus. - state.folder_scan = scan_folder(state) - refused = kind == "chosen" and (state.facts.get("writable") is False - or _short_of_space(state, state.check or {})) - # A folder just refused as full or read-only: Browse, not Use this - # folder, is under A, or A, A came back to the same warning. - _push(state, "folder") - if refused and default is not None: - state.focus["folder"] = next((index for index, row in enumerate(tui_rows(state)) - if row.action == "browse"), 1) - return [] - - -def typed_path(text: str) -> Path | None: - """A folder typed or pasted: quotes off, ~ and variables expanded.""" - if not text.strip().strip("\"'").strip(): - return None - return _typed_folder(text) - - -def _tui_folder_facts(state: TuiState) -> dict: - """Free space, whether the folder takes files, and the cores found.""" - dest = state.dest - assert dest is not None - probe = _nearest_folder(dest) - try: - free: int | None = shutil.disk_usage(probe).free - except OSError: - free = None - cores = (installed_cores(core_dirs(state.platform, dest)) - if state.platform in CORE_PLATFORMS else []) - # The retroarch.cfg that names dest, said under it on the plan. Whether - # dest is the card of its system is read here with the rest, never by - # a frame: a card pulled out moved the plan rows under the focus. - return {"free": free, "writable": _folder_writable(dest), "cores": cores, - "config": _retroarch_config_at(dest), - "on_card": on_its_card(state.platform, dest, - state.card_bound[0] if state.card_bound else "")} - - -def _row_folder(row: TuiRow) -> Path: - """The folder or archive a row names.""" - value = row.value - assert isinstance(value, Path) - return value - - -def _activate_setup(state: TuiState, row: TuiRow) -> list[tuple]: - """A on a setup found here, a drive two systems fit, or Another.""" - if row.action == "another": - return _push(state, "platforms", _platform_focus(state, "retroarch")) - if row.action == "doubt": - # Taken as the system chosen, as --platform with --dest takes it. - value = row.value - assert isinstance(value, tuple) - plat, mount = value - state.card_bound = card_bound(plat, plat) - _set_platform(state, plat) - choice = card_choice(plat, plat, mount) - if choice.refused: - _card_status(state, choice) - return [] - return _take_card(state, choice, mount) - assert isinstance(row.value, int) - plat, path, _label = state.setups[row.value] - # A setup found here is its folder: a card looked at and backed - # out of described the RetroArch chosen next as that card. - state.card_bound = None - return _choose(state, plat, path, proposed=True) - - -def _activate_platforms(state: TuiState, row: TuiRow) -> list[tuple]: - """A on a setup of the list of every setup.""" - value = row.value - if row.action == "cardsystem": - assert isinstance(value, tuple) - plat, system = value - else: - assert isinstance(value, str) - plat, system = value, value - _set_platform(state, plat) - state.card_bound = card_bound(system, plat) - if state.dest_given is not None: - # Read as --platform with --dest reads it, for the same folder: - # ROCKNIX's row added roms/bios to a --dest the line mode kept. - choice = card_choice(system, plat, state.dest_given) - if choice.refused: - _card_status(state, choice, given=True) - state.platform, state.card_bound = "", None - return [] - return _take_card(state, choice, state.dest_given) - return _open_folder_screen(state, *setup_folder( - state.os_type, state.setups, state.mounted_setups, plat, system)) - - -def _rescan_cards(state: TuiState) -> list[tuple]: - """Look again for the cards of the setup chosen.""" - bound = _tui_card_bound(state) - assert bound is not None - state.folder_scan = scan_folder(state) - mounts = card_mounts(state.folder_scan) - cards = [index for index, item in enumerate(tui_rows(state)) - if item.action == "card" and item.value in mounts] - state.focus["folder"] = cards[0] if cards else state.focus.get("folder", 0) - state.status = ("" if cards else f"Still no {bound[0]} card " - "is mounted.") - return [] - - -def _browse_for_dest(state: TuiState) -> list[tuple]: - """Browse for a folder, from the folder screen.""" - state.browse_for = "dest" - _push(state, "browse") - _browse_to(state, _nearest_folder(state.folder_default or state.home)) - bound = _tui_card_bound(state) - if bound and state.folder_kind != "found": - # A card is asked for: the drives come first, and the home - # folder's "go back to type a path" sent the user away from - # the cards listed under it. - # Its own cards first: the muOS card listed first was focused - # for Onion. - own = mounted_cards_of(bound[0], state.platform) - setup = card_setup(bound[0], state.platform) - # Another system's card is never focused for this one: the - # muOS card was, under "Choose the Onion card below". - free = [mount for mount in _mounted_cards() - if setup is None or _card_is(setup, mount)[0] != "other"] - drives = ([index for index, item in enumerate(state.browse_rows) - if item.action == "dir" and item.value in own] - or [index for index, item in enumerate(state.browse_rows) - if item.action == "dir" and item.value in free]) - if drives: - state.focus["browse"] = drives[0] - state.status = (f"Choose the {bound[0]} card below." if own - else f"No {bound[0]} card is mounted: insert it, or open " - "its top folder.") - return [] - - -def _activate_folder(state: TuiState, row: TuiRow) -> list[tuple]: - """A on a row of the BIOS folder question.""" - action = row.action - if action == "default": - assert state.folder_default is not None - return _choose(state, state.platform, state.folder_default, proposed=True) - if action in ("share", "drive"): - return _choose(state, state.platform, _row_folder(row), proposed=True) - if action == "rescan": - return _rescan_cards(state) - if action == "card": - return _choose_card(state, _row_folder(row)) - if action == "browse": - return _browse_for_dest(state) - state.path_text = str(state.folder_default or "") - state.path_selected = bool(state.path_text) - return _push(state, "path") - - -def _use_from_folder(state: TuiState, row: TuiRow) -> list[tuple]: - """A folder or archive to copy BIOS files from, chosen in the browser.""" - chosen = state.browse_dir if row.action == "use" else _row_folder(row) - refused = copy_source_refusal(chosen, state.dest) - if refused: - state.status = _styled(refused, "refusal") - return [] - if chosen not in state.from_folders: - state.from_folders.append(chosen) - state.browse_for = "dest" - # The check runs again with it: the plan then says what it holds. - return _start_check(state) - - -def _activate_browse(state: TuiState, row: TuiRow) -> list[tuple]: - """A on a row of the folder browser.""" - action = row.action - if action == "zip" and state.browse_for == "pack": - # Opened in this process (perform), which answers pack_opened. - return [("pack", row.value)] - if action == "use" and state.browse_for == "packto": - state.pack_dir = state.browse_dir - state.browse_for = "dest" - state.stack.pop() - state.pack_plan = None - return _start_run(state, "pack_plan") - if action == "use" and state.browse_for == "save": - state.passthrough = [flag for flag in state.passthrough - if flag[0] != "--save-dir"] - state.passthrough.append(("--save-dir", str(state.browse_dir))) - state.browse_for = "dest" - return _start_check(state) - if action in ("use", "zip") and state.browse_for == "from": - return _use_from_folder(state, row) - if action == "use": - assert state.browse_dir is not None - return _choose_card(state, state.browse_dir) - if action == "up" and state.browse_dir is not None: - if row.value == state.browse_dir: - _browse_to(state, None if os.name == "nt" else state.browse_dir) - else: - _browse_to(state, _row_folder(row), came_from=state.browse_dir) - return [] - _browse_to(state, _row_folder(row)) - return [] - - -def _activate_create(state: TuiState, row: TuiRow) -> list[tuple]: - """A on Create the folder?.""" - if row.action == "create": - state.stack.pop() - return _start_check(state) - if len(state.stack) == 1: - # The folder came from --dest: nothing is under this screen, and - # Back asked Quit? with Quit in focus. The - # folder screen takes its place, as the plan's row opens it. - state.stack.pop() - return _open_folder_screen(state, state.dest, "chosen", rebind=True) - return _back(state) - - -def _activate_cardroot(state: TuiState, row: TuiRow) -> list[tuple]: - """A on the card root question.""" - if row.action == "cardroot": - state.stack.pop() - return _choose(state, state.platform, state.facts["card_root"]) - return _back(state) +from installer.tui_folder_actions import ( + _activate_browse, + _activate_cardroot, + _activate_create, + _activate_folder, + _activate_platforms, + _activate_setup, + _open_folder_screen, +) +from installer.tui_steps import ( + _activate_action, + _back, + _browse_to, + _choose_board, + _open_details, + _push, + _start_check, + _start_run, + _tui_record_drop, + _tui_record_take, + tui_quit, +) def _plan_close(state: TuiState, row: TuiRow) -> list[tuple]: @@ -1218,13 +279,6 @@ def _checklist(state: TuiState, row: TuiRow) -> list[tuple]: return [] -def _platform_focus(state: TuiState, plat: str) -> int: - """The platform list's row for plat, below its filter row.""" - rows = _platform_rows(state) - return next((index for index, row in enumerate(rows) - if row.action == "platform" and row.value == plat), 1) - - def _option_flag(state: TuiState, action: str, step: int) -> bool: """A yes or no of the options, or the jobs, set: whether action is one.""" if action == "game_data": diff --git a/installer/tui_folder_actions.py b/installer/tui_folder_actions.py new file mode 100644 index 000000000..3051ebdbd --- /dev/null +++ b/installer/tui_folder_actions.py @@ -0,0 +1,518 @@ +"""What choosing a setup, a platform, a card or a folder does.""" + +from __future__ import annotations + +import os +import shutil +from pathlib import Path +from typing import TYPE_CHECKING + +from installer.cards import ( + _card_boundary, + _mount_name, + _mount_points, + _mounted_cards, + card_bound, + card_platform, + card_setup, + card_system, +) +from installer.choices import emulator_names +from installer.console import _typed_folder +from installer.cores import CORE_PLATFORMS, core_dirs, installed_cores +from installer.destination import ( + _card_is, + card_choice, + card_root_above, + copy_source_refusal, + dest_drive, + missing_folder, + mounted_cards_of, + on_its_card, +) +from installer.detect_os import _DETECTED_OS, _FROM_CARD, detected_os, detected_shown +from installer.embedded import CARD_PLATFORMS, _a +from installer.frontend_folders import _retroarch_config_at +from installer.host_fs import _exists, _folder_writable, _is_dir, _nearest_folder, closed_folder +from installer.lists import local_targets +from installer.offline import LOCAL +from installer.paths import shown_path +from installer.platforms import display_name +from installer.setups import setup_folder +from installer.tui_choice import _short_of_space, _tui_card_bound +from installer.tui_commands import tui_record_kept +from installer.tui_folder_scan import card_mounts, scan_folder +from installer.tui_paint import _short_path, _styled +from installer.tui_rows import _platform_rows, tui_rows +from installer.tui_state import TuiRow +from installer.tui_steps import _back, _browse_to, _push, _start_check, _start_run, _tui_record_trim + +if TYPE_CHECKING: + from installer.destination import CardChoice + from installer.tui_paint import TuiLine + from installer.tui_state import TuiState + + +def _closed_status(state: TuiState, closed: tuple[Path, str]) -> TuiLine: + folder = _short_path(closed[0], state.home) + return _styled(f"This user cannot open {folder} ({closed[1]}): choose another folder, " + "or give this user access to it.", "refusal") + + +def _set_platform(state: TuiState, plat: str) -> None: + """Another setup chosen: its list names other boards and emulators, so + the ones chosen for the setup before are not carried over.""" + if state.platform and plat != state.platform: + state.target, state.every_board, state.emulators = "", False, [] + state.targets, state.targets_for, state.targets_problem = None, "", "" + state.platform = plat + + +def _known_choice(state: TuiState, plat: str, manifest: dict) -> None: + """The emulators and the board chosen, kept to those the list now read + names, the others left out and said in one line: a pack's list can + lack what the network's held, and the check then failed.""" + if tui_record_kept(state): + _tui_record_trim(state, plat, manifest) + names = emulator_names(manifest, manifest.get("files") or [], + manifest.get("omitted_files") or []) + gone = [f"--emulator {name}" for name in state.emulators if name not in names] + state.emulators = [name for name in state.emulators if name in names] + boards = state.targets if state.targets_for == plat else None + if LOCAL.manifest is not None or LOCAL.pack is not None: + try: + boards = local_targets(plat) + except SystemExit: + # A board list beside the list that is no list: the check says so. + boards = None + if state.target and boards is not None and state.target not in boards: + gone.append(f"--target {state.target}") + state.target = "" + if gone: + state.status = f"Left out, not in this list: {', '.join(gone)}." + + +def _choose(state: TuiState, plat: str, dest: Path, proposed: bool = False) -> list[tuple]: + """dest taken for plat. proposed is a folder the installer offered (its + usual or detected folder, a share drive's bios): created without the + guard a typed path gets, which Enter on it met as Choose another folder, + and Enter there brought the same two screens back.""" + state.card_note = "" + closed = closed_folder(dest) + if closed is not None: + # Nothing can be checked or written there: the folder is asked + # again, on the screen that chose it when it can say why. + _set_platform(state, plat) + state.status = _closed_status(state, closed) + if state.stack and state.screen in ("folder", "browse", "path"): + return [] + # The folder found here is the one the frontend reads: the built-in + # default beside it is one it does not, offered as "reads its BIOS + # files from" and filled with Enter. + state.folder_default, state.folder_kind = None, "closed" + state.folder_scan = scan_folder(state) + return _push(state, "folder") + card = card_system(dest) + if card and card[0] == plat and card[1] != dest: + # A muOS or KNULLI card's root: that system reads a folder below it. + dest = card[1] + _set_platform(state, plat) + state.dest = dest + if plat in CARD_PLATFORMS and card_platform(dest) is None: + # The engine refuses a folder inside the card, the list writing + # Bios// below the root: the root is offered instead. + for parent in list(dest.absolute().parents)[:2]: + holder = card_platform(parent) + if holder: + state.facts = {"card_root": parent, "card": holder} + return _push(state, "cardroot") + root = _own_card_root(dest) + if root is not None and not _exists(dest) and _is_dir(dest.parent): + # The card's own files name it, so its folder is known: created as + # line mode creates it, and said on the plan, not asked. + state.card_note = (f"{dest.name} is created on {_mount_name(root)}, where " + f"{detected_shown(plat, dest) or display_name(plat)} reads it.") + return _start_check(state) + if proposed and not _exists(dest): + # Where no setup is there yet, nothing reads it: said as it is. + reads = (f", where {detected_shown(plat, dest) or display_name(plat)} reads it" + if _is_dir(dest.parent) else ", a folder that does not exist yet") + state.card_note = (f"{dest.name} is created in " + f"{_short_path(dest.parent, state.home)}{reads}.") + return _start_check(state) + found = missing_folder(dest) + if found is None: + return _start_check(state) + if found.verdict == "create": + # One level is a BIOS folder not made yet: line mode makes it and + # says so, the plan says so here. + state.card_note = f"{dest.name} is created in {_short_path(dest.parent, state.home)}." + return _start_check(state) + # Asked with no as the answer, as line mode asks. A path whose start + # was mistyped would be created from the root: said on the screen. + state.facts = {"nearest": found.nearest, "risky": found.nearest.parent == found.nearest} + return _push(state, "create", 1) + + +def _card_folder(state: TuiState, folder: Path) -> CardChoice | None: + """What a folder chosen for a setup that runs from its SD card resolves + to, as a card root typed in line mode does: None when no card is asked + for, on the device itself, whose folder is taken as typed (a KNULLI got + bios added to it), or when the folder is inside a MinUI card, whose + root _choose offers.""" + bound = _tui_card_bound(state) + if bound is None and state.platform: + # Read as line mode reads a folder typed (ask_platform_folder): a + # Batocera SHARE drive's root goes to its bios folder, another + # system's drive is refused, a card's root is said. + return card_choice(state.platform, state.platform, folder) + if bound is None or (state.platform in CARD_PLATFORMS and card_root_above(folder)): + return None + if state.folder_kind == "found" and state.folder_default is not None: + return None + return card_choice(bound[0], state.platform, folder) + + +def _card_status(state: TuiState, choice: CardChoice, given: bool = False) -> None: + """Say why a folder is not the card: one short sentence first, which a + 40-column screen shows whole, the folder and what was looked for on it + after, shown where there is room and in the help. given is a folder + --dest names, which no choice here can change.""" + if given: + # --dest is fixed for this run: only another system's card gets + # "choose another setup". A folder inside a card, or one that is no + # card, is named again in the command. + then = (f"Run it again with --dest {shown_path(choice.root)}." if choice.root + else "--dest names it: choose another setup." if choice.other + else "Run it again with the card's top folder.") + short = choice.short or choice.refused + "." + elif choice.other: + # The top folder of a card, only another system's: "Choose the + # card's top folder" sent the user back to the same one. + bound = _tui_card_bound(state) + kind = "SHARE drive" if "SHARE drive" in choice.short else "card" + then = (f"Choose the {display_name(state.platform)} SHARE drive, or go back and " + f"choose {choice.other}." if kind != "card" and not bound else + f"Insert the {bound[0] if bound else 'right'} card, or go back and choose " + f"{choice.other}.") + short = f"That is {_a(choice.other)} {kind}." + else: + then = "Choose the card's top folder." + short = choice.short or choice.refused + "." + state.status = _styled(f"{short} {then}", "refusal") + state.status_more = f"{choice.refused}." + (f" {choice.detail}." if choice.detail else "") + + +def _card_refusal(state: TuiState, folder: Path) -> bool: + """Whether folder is refused as the card the chosen setup runs from, + said in the status when it is.""" + choice = _card_folder(state, folder) + if choice is None or not choice.refused: + return False + _card_status(state, choice) + return True + + +def _choose_card(state: TuiState, folder: Path) -> list[tuple]: + """_choose for a folder picked on the folder, browse or path screen: for + a setup that runs from its SD card, the card's own BIOS folder, and a + folder that is not its card refused in words.""" + choice = _card_folder(state, folder) + if choice is None: + return _choose(state, state.platform, folder) + if choice.refused: + _card_status(state, choice) + if state.stack and state.screen in ("folder", "browse", "path"): + return [] + state.folder_default, state.folder_kind = None, "usual" + state.folder_scan = scan_folder(state) + return _push(state, "folder") + return _take_card(state, choice, folder) + + +def _own_card_root(folder: Path) -> Path | None: + """The root of the card whose own files name folder as its BIOS folder, + or None: an Onion card's BIOS, a muOS card's MUOS/bios. No folder above + a mount point is read, as _card_above reads none: it is another drive.""" + points = _mount_points() + below = folder + for parent in list(folder.parents)[:2]: + if _card_boundary(below, points): + return None + below = parent + card = card_system(parent) + if card and card[1] == folder: + return parent + return None + + +def _take_card(state: TuiState, choice: CardChoice, root: Path) -> list[tuple]: + """_choose for the folder card_choice gave root, its note kept on the + screens up to the plan: --dest, the setup list with --dest and a folder + picked here said nothing of a stick no file names, where line mode did.""" + card = card_system(root) + if card and card[1] == choice.folder: + # Titled as the same card found by itself: "Onion (RetroArch files)". + _DETECTED_OS.setdefault((choice.plat, choice.folder), card[2]) + _FROM_CARD.add((choice.plat, choice.folder)) + elif state.card_bound: + # A card no file names, chosen as ArkOS's: titled ArkOS, not RetroArch. + _DETECTED_OS.setdefault((choice.plat, choice.folder), state.card_bound[0]) + # The system's folder below a card that exists is made as line mode + # makes it (_missing_dest, one level): the screen asked Create the + # folder? for KNULLI's bios on a blank stick, line mode did not. + below = choice.folder != root and _is_dir(choice.folder.parent) + effects = _choose(state, choice.plat, choice.folder, proposed=below) + # Said once: a note that names the folder already says where the files go. + named = bool(choice.note) and shown_path(choice.folder) in choice.note + state.card_note = choice.note if named else " ".join(filter(None, (choice.note, + state.card_note))) + return effects + + +def _open_folder_screen(state: TuiState, default: Path | None, + kind: str = "usual", rebind: bool = False) -> list[tuple]: + """The folder screen for the chosen setup. rebind is another folder + asked for from the plan: a card setup found mounted (an Onion card) is + asked for its card again, never for any folder. A folder off the card + is one Onion does not read, and the screen said RetroArch.""" + state.folder_default, state.folder_kind = default, kind + # On the device itself its folder is no card in a reader. + system = (detected_os(state.platform, state.dest) + if rebind and state.dest is not None and dest_drive(state.dest) is not None + else "") + if system and not state.card_bound and card_bound(system, state.platform): + state.card_bound = card_bound(system, state.platform) + # The drives are looked for when the screen opens and on Look again: a + # card inserted meanwhile moved the rows under the focus. + state.folder_scan = scan_folder(state) + refused = kind == "chosen" and (state.facts.get("writable") is False + or _short_of_space(state, state.check or {})) + # A folder just refused as full or read-only: Browse, not Use this + # folder, is under A, or A, A came back to the same warning. + _push(state, "folder") + if refused and default is not None: + state.focus["folder"] = next((index for index, row in enumerate(tui_rows(state)) + if row.action == "browse"), 1) + return [] + + +def typed_path(text: str) -> Path | None: + """A folder typed or pasted: quotes off, ~ and variables expanded.""" + if not text.strip().strip("\"'").strip(): + return None + return _typed_folder(text) + + +def _tui_folder_facts(state: TuiState) -> dict: + """Free space, whether the folder takes files, and the cores found.""" + dest = state.dest + assert dest is not None + probe = _nearest_folder(dest) + try: + free: int | None = shutil.disk_usage(probe).free + except OSError: + free = None + cores = (installed_cores(core_dirs(state.platform, dest)) + if state.platform in CORE_PLATFORMS else []) + # The retroarch.cfg that names dest, said under it on the plan. Whether + # dest is the card of its system is read here with the rest, never by + # a frame: a card pulled out moved the plan rows under the focus. + return {"free": free, "writable": _folder_writable(dest), "cores": cores, + "config": _retroarch_config_at(dest), + "on_card": on_its_card(state.platform, dest, + state.card_bound[0] if state.card_bound else "")} + + +def _row_folder(row: TuiRow) -> Path: + """The folder or archive a row names.""" + value = row.value + assert isinstance(value, Path) + return value + + +def _activate_setup(state: TuiState, row: TuiRow) -> list[tuple]: + """A on a setup found here, a drive two systems fit, or Another.""" + if row.action == "another": + return _push(state, "platforms", _platform_focus(state, "retroarch")) + if row.action == "doubt": + # Taken as the system chosen, as --platform with --dest takes it. + value = row.value + assert isinstance(value, tuple) + plat, mount = value + state.card_bound = card_bound(plat, plat) + _set_platform(state, plat) + choice = card_choice(plat, plat, mount) + if choice.refused: + _card_status(state, choice) + return [] + return _take_card(state, choice, mount) + assert isinstance(row.value, int) + plat, path, _label = state.setups[row.value] + # A setup found here is its folder: a card looked at and backed + # out of described the RetroArch chosen next as that card. + state.card_bound = None + return _choose(state, plat, path, proposed=True) + + +def _activate_platforms(state: TuiState, row: TuiRow) -> list[tuple]: + """A on a setup of the list of every setup.""" + value = row.value + if row.action == "cardsystem": + assert isinstance(value, tuple) + plat, system = value + else: + assert isinstance(value, str) + plat, system = value, value + _set_platform(state, plat) + state.card_bound = card_bound(system, plat) + if state.dest_given is not None: + # Read as --platform with --dest reads it, for the same folder: + # ROCKNIX's row added roms/bios to a --dest the line mode kept. + choice = card_choice(system, plat, state.dest_given) + if choice.refused: + _card_status(state, choice, given=True) + state.platform, state.card_bound = "", None + return [] + return _take_card(state, choice, state.dest_given) + return _open_folder_screen(state, *setup_folder( + state.os_type, state.setups, state.mounted_setups, plat, system)) + + +def _rescan_cards(state: TuiState) -> list[tuple]: + """Look again for the cards of the setup chosen.""" + bound = _tui_card_bound(state) + assert bound is not None + state.folder_scan = scan_folder(state) + mounts = card_mounts(state.folder_scan) + cards = [index for index, item in enumerate(tui_rows(state)) + if item.action == "card" and item.value in mounts] + state.focus["folder"] = cards[0] if cards else state.focus.get("folder", 0) + state.status = ("" if cards else f"Still no {bound[0]} card " + "is mounted.") + return [] + + +def _browse_for_dest(state: TuiState) -> list[tuple]: + """Browse for a folder, from the folder screen.""" + state.browse_for = "dest" + _push(state, "browse") + _browse_to(state, _nearest_folder(state.folder_default or state.home)) + bound = _tui_card_bound(state) + if bound and state.folder_kind != "found": + # A card is asked for: the drives come first, and the home + # folder's "go back to type a path" sent the user away from + # the cards listed under it. + # Its own cards first: the muOS card listed first was focused + # for Onion. + own = mounted_cards_of(bound[0], state.platform) + setup = card_setup(bound[0], state.platform) + # Another system's card is never focused for this one: the + # muOS card was, under "Choose the Onion card below". + free = [mount for mount in _mounted_cards() + if setup is None or _card_is(setup, mount)[0] != "other"] + drives = ([index for index, item in enumerate(state.browse_rows) + if item.action == "dir" and item.value in own] + or [index for index, item in enumerate(state.browse_rows) + if item.action == "dir" and item.value in free]) + if drives: + state.focus["browse"] = drives[0] + state.status = (f"Choose the {bound[0]} card below." if own + else f"No {bound[0]} card is mounted: insert it, or open " + "its top folder.") + return [] + + +def _activate_folder(state: TuiState, row: TuiRow) -> list[tuple]: + """A on a row of the BIOS folder question.""" + action = row.action + if action == "default": + assert state.folder_default is not None + return _choose(state, state.platform, state.folder_default, proposed=True) + if action in ("share", "drive"): + return _choose(state, state.platform, _row_folder(row), proposed=True) + if action == "rescan": + return _rescan_cards(state) + if action == "card": + return _choose_card(state, _row_folder(row)) + if action == "browse": + return _browse_for_dest(state) + state.path_text = str(state.folder_default or "") + state.path_selected = bool(state.path_text) + return _push(state, "path") + + +def _use_from_folder(state: TuiState, row: TuiRow) -> list[tuple]: + """A folder or archive to copy BIOS files from, chosen in the browser.""" + chosen = state.browse_dir if row.action == "use" else _row_folder(row) + refused = copy_source_refusal(chosen, state.dest) + if refused: + state.status = _styled(refused, "refusal") + return [] + if chosen not in state.from_folders: + state.from_folders.append(chosen) + state.browse_for = "dest" + # The check runs again with it: the plan then says what it holds. + return _start_check(state) + + +def _activate_browse(state: TuiState, row: TuiRow) -> list[tuple]: + """A on a row of the folder browser.""" + action = row.action + if action == "zip" and state.browse_for == "pack": + # Opened in this process (perform), which answers pack_opened. + return [("pack", row.value)] + if action == "use" and state.browse_for == "packto": + state.pack_dir = state.browse_dir + state.browse_for = "dest" + state.stack.pop() + state.pack_plan = None + return _start_run(state, "pack_plan") + if action == "use" and state.browse_for == "save": + state.passthrough = [flag for flag in state.passthrough + if flag[0] != "--save-dir"] + state.passthrough.append(("--save-dir", str(state.browse_dir))) + state.browse_for = "dest" + return _start_check(state) + if action in ("use", "zip") and state.browse_for == "from": + return _use_from_folder(state, row) + if action == "use": + assert state.browse_dir is not None + return _choose_card(state, state.browse_dir) + if action == "up" and state.browse_dir is not None: + if row.value == state.browse_dir: + _browse_to(state, None if os.name == "nt" else state.browse_dir) + else: + _browse_to(state, _row_folder(row), came_from=state.browse_dir) + return [] + _browse_to(state, _row_folder(row)) + return [] + + +def _activate_create(state: TuiState, row: TuiRow) -> list[tuple]: + """A on Create the folder?.""" + if row.action == "create": + state.stack.pop() + return _start_check(state) + if len(state.stack) == 1: + # The folder came from --dest: nothing is under this screen, and + # Back asked Quit? with Quit in focus. The + # folder screen takes its place, as the plan's row opens it. + state.stack.pop() + return _open_folder_screen(state, state.dest, "chosen", rebind=True) + return _back(state) + + +def _activate_cardroot(state: TuiState, row: TuiRow) -> list[tuple]: + """A on the card root question.""" + if row.action == "cardroot": + state.stack.pop() + return _choose(state, state.platform, state.facts["card_root"]) + return _back(state) + + +def _platform_focus(state: TuiState, plat: str) -> int: + """The platform list's row for plat, below its filter row.""" + rows = _platform_rows(state) + return next((index for index, row in enumerate(rows) + if row.action == "platform" and row.value == plat), 1) diff --git a/installer/tui_input.py b/installer/tui_input.py index 7d419d088..fc5c5515b 100644 --- a/installer/tui_input.py +++ b/installer/tui_input.py @@ -7,23 +7,9 @@ from typing import TYPE_CHECKING, NamedTuple from installer.host_fs import closed_folder from installer.terminal import TUI_MIN_COLS, TUI_MIN_ROWS from installer.text import cell_width -from installer.tui_actions import ( - _activate, - _back, - _card_refusal, - _checklist, - _choose_card, - _closed_status, - _open_details, - _option, - _push, - _start_run, - _tick, - _toggle_region, - tui_interrupt, - tui_quit, - typed_path, -) +from installer.tui_actions import _activate, _checklist, _option, _tick, _toggle_region +from installer.tui_steps import _back, _open_details, _push, _start_run, tui_interrupt, tui_quit +from installer.tui_folder_actions import _card_refusal, _choose_card, _closed_status, typed_path from installer.tui_commands import _tui_standalone_fits from installer.tui_frame import _armed, _bar_click, _bar_commits, _pager_starts from installer.tui_keys import TuiKey, TuiMouse diff --git a/installer/tui_receive.py b/installer/tui_receive.py index ce9cd7e48..7fb832720 100644 --- a/installer/tui_receive.py +++ b/installer/tui_receive.py @@ -7,18 +7,9 @@ from typing import TYPE_CHECKING from installer.device import detect_board from installer.platforms import display_name from installer.removal import list_backups -from installer.tui_actions import ( - _activate, - _device_focus, - _focus_plan, - _install_now, - _known_choice, - _replace, - _same_plan, - _start_check, - _start_run, - _tui_folder_facts, -) +from installer.tui_actions import _activate, _device_focus, _install_now, _same_plan +from installer.tui_steps import _focus_plan, _replace, _start_check, _start_run +from installer.tui_folder_actions import _known_choice, _tui_folder_facts from installer.tui_choice import _needed from installer.tui_commands import TUI_ACTION_RUNS from installer.tui_outcome import full_drive_words diff --git a/installer/tui_session.py b/installer/tui_session.py index 8b214e71d..6d33e6a70 100644 --- a/installer/tui_session.py +++ b/installer/tui_session.py @@ -44,16 +44,15 @@ from installer.regions import resolve_regions from installer.setups import _default_dest, found_setups, setup_list from installer.terminal import _enable_ansi from installer.text import _pad, cell_width, screen_text -from installer.tui_actions import ( +from installer.tui_folder_actions import ( _card_status, _choose, _open_folder_screen, _platform_focus, _take_card, - open_tui_pack, - tui_interrupt, - use_tui_pack, ) +from installer.tui_actions import open_tui_pack, use_tui_pack +from installer.tui_steps import tui_interrupt from installer.tui_commands import _tui_passthrough, tui_argv, tui_run_env from installer.tui_engine import ( _EngineRun, diff --git a/installer/tui_steps.py b/installer/tui_steps.py new file mode 100644 index 000000000..0c4237132 --- /dev/null +++ b/installer/tui_steps.py @@ -0,0 +1,488 @@ +"""Moves between screens: opening and leaving them, starting a check or a run, the action menus.""" + +from __future__ import annotations + +from pathlib import Path +from typing import TYPE_CHECKING + +from installer.cards import _mounted_cards +from installer.choices import known_regions, trim_recorded +from installer.console import QUIT_CODE +from installer.games import games_probe +from installer.host_fs import _nearest_folder +from installer.install_record import ( + RECORD_NARROWING, + _RECORD_SAID, + choice_words, + options_without, + record_options, + setup_record, + take_recorded_choice, +) +from installer.platforms import _tui_system_label +from installer.regions import resolve_regions +from installer.tui_choice import ( + _board_seen, + _choice_in_place, + _counts_predate_options, + _dest_card, + _needed, + _no_setup_reads, + _none_ticked, + _system_titles, +) +from installer.tui_commands import tui_flags, tui_pack_dir, tui_record_kept +from installer.tui_rows import _browse_rows, _os_folder, tui_rows +from installer.tui_state import TuiRow + +if TYPE_CHECKING: + from installer.tui_state import TuiState + + +def _choose_board(state: TuiState, name: str) -> list[tuple]: + """A board chosen on Device, back on Change. The board the run reads + itself is no option: the command stays the one with nothing chosen. + Every board is always said, the run otherwise reading a board itself + (one started with --target or --every-board read none).""" + seen = _board_seen(state) + read = seen[0] if seen is not None and seen[1] else "" + state.every_board = not name + state.target = "" if not name or name == read else name + return _back(state) + + +def _focus_plan(state: TuiState, action: str) -> None: + """The plan's focus on the row doing action. A plan that keeps no file, + a choice emptied or already in place, opens on Choose systems: the + focus kept its index, and Enter opened Options.""" + actions = [row.action for row in tui_rows(state)] + keeps_none = _choice_in_place(state) or (state.emptied and state.systems + and not state.ticked) + if action in actions: + state.focus["plan"] = actions.index(action) + elif keeps_none and "systems" in actions: + state.focus["plan"] = actions.index("systems") + + +def _push(state: TuiState, screen: str, focus: int = 0) -> list[tuple]: + state.stack.append(screen) + state.focus[screen] = focus + return [] + + +def _replace(state: TuiState, screen: str) -> None: + state.stack[-1] = screen + state.focus[screen] = 0 + + +def _back(state: TuiState) -> list[tuple]: + if state.screen == "packdone" and not state.running and "more" in state.stack: + # Made or not, its plan is spent: back to More. + del state.stack[state.stack.index("more") + 1:] + return [] + if state.screen == "actdone" and not state.running: + # Done: what was planned is gone, and the plan is counted again. + return _activate_action(state, TuiRow("recheck", "")) + effects = [("kill",)] if state.running else [] + if state.running: + # What the stopped run still reports belongs to no screen. + state.running = False + state.run_id += 1 + if len(state.stack) == 1: + state.stack.append("quit") + state.focus["quit"] = 0 + return effects + refused = _none_ticked(state) + if refused: + # Done and B leave a list of ticks alike, never with nothing + # ticked: said on every length of list, in the line the rows + # leave. + state.status = refused + return effects + chose = state.screen == "systems" and state.ticked != (state.ticked_before or set()) + if state.screen == "systems" and state.widened_from is not None: + before, state.widened_from = state.widened_from, None + if state.ticked == {row["system"] for row in before["systems"]}: + # The systems --system named, ticked again: the exact plan of + # their own check, not an estimate over every system. + for field, value in before.items(): + setattr(state, field, value) + chose = False + if state.screen == "regions": + state.region_order = [] + state.stack.pop() + if chose and state.screen == "plan": + # The systems chosen, the next press is Install: Back left the focus + # on Choose systems. + _focus_plan(state, "install") + if state.screen == "checking" and not state.running: + # Stay after Esc on a check started from the command line: a + # Checking screen nothing feeds counted seconds forever. + return effects + _start_check(state) + if state.screen == "plan" and _counts_predate_options(state): + # The plan's numbers were read under the options before: they are + # read again, the systems ticked kept. + return effects + _start_check(state) + return effects + + +def _keep_choice(state: TuiState) -> None: + """The systems ticked, kept for the next check of the same platform's + list when they are not every system to install: one choice for the + whole run, whatever is changed on the way (game data, an option, a + folder, a folder to copy from). A choice already set aside (widening a + --system) is left as it is.""" + if (state.keep_ticked is not None or not state.systems + or state.manifest_for != state.platform): + return + if state.emptied and not state.ticked: + # Still the choice the options emptied: the same systems again. + state.keep_ticked = set(state.ticked_names) + return + needed = {row["system"] for row in state.systems if _needed(row)} + state.keep_ticked = (None if needed <= state.ticked and not state.hidden_ticks + else state.ticked | state.hidden_ticks) + titles = _system_titles(state) + state.ticked_names = { + **{name: label for name, label in state.ticked_names.items() + if name in state.hidden_ticks}, + **{row["system"]: _tui_system_label(row, titles) + for row in state.systems if row["system"] in state.ticked}} + + +def _start_run(state: TuiState, run: str) -> list[tuple]: + state.run = run + state.running = True + state.run_id += 1 + state.started = state.now + if run == "check": + # The systems of an earlier check go first: ticks read against them + # dropped the --system the command line gave. + state.check, state.files, state.systems, state.reading = None, [], [], None + state.checked_dests = set() + state.choices = state.set_aside = None + state.found, state.scanned, state.unreadable = [], [], [] + state.check_summary = state.saves = None + state.unplaced_why = {} + state.version = state.version_short = state.board = "" + state.games = state.games_reading = state.games_unavailable = None + state.checked_flags = tui_flags(state) + elif run in ("confirm", "preview"): + state.dry, state.dry_files, state.dry_summary = None, [], None + state.dry_saves = None + else: + state.done_saves = state.dest_error = None + state.download = state.progress = state.reading = state.games_reading = None + state.samples, state.local_dests, state.network = [], {}, 0 + state.finished, state.summary, state.code, state.stopping = [], None, None, False + return [("run", run)] + + +def _tui_record_take(state: TuiState) -> None: + """The recorded choice made the screen's, as its options would make it.""" + setup = state.record or {} + taken = take_recorded_choice(setup.get("choice") or []) + if taken is None: + state.status = "The install record names options this installer lacks, so it is ignored." + state.record = None + return + parsed = taken.parsed + left_out, choice = list(taken.left_out), taken.choice + recorded_regions = list(parsed.region or []) + regions = known_regions(recorded_regions) + if regions != recorded_regions: + # One region at a time, as line mode keeps them: a known region + # beside an unknown one stays. + left_out.append("--region (a region this installer does not know)") + choice = choice_words(options_without(record_options(choice), "--region", regions)) + if left_out: + # What the run takes, as line mode keeps it: the plan reads as the + # record's own choice again. + setup = state.record = {**setup, "choice": choice} + state.status = f"Left out of the recorded choice: {', '.join(left_out)}." + state.cli_systems = list(parsed.system or []) + state.cores = [token.strip() for value in parsed.core or [] for token in value.split(",") + if token.strip()] + state.regions = resolve_regions(regions) if regions else [] + state.no_game_data = bool(parsed.no_game_data) + state.installed_only = bool(parsed.installed_cores) + state.for_games = parsed.for_games is not None + state.games_folder = parsed.for_games if isinstance(parsed.for_games, Path) else None + state.required_only = bool(parsed.required_only) and state.for_games + state.games_choice = "record" + # Device and Emulators show these: a choice no row showed was kept apart. + state.target = str(parsed.target or "") + state.every_board = bool(parsed.every_board) + state.emulators = list(parsed.emulator or []) + state.record_flags = [(flag, value) for flag, value in record_options(setup["choice"]) + if flag == "--declared-only"] + if state.manifest is not None and state.manifest_for == state.platform: + _tui_record_trim(state, state.platform, state.manifest) + + +def _tui_record_trim(state: TuiState, plat: str, manifest: dict) -> None: + """The systems, cores, regions and emulators of the recorded choice + that this list cannot take, left out by the rule line mode follows + (trim_recorded) and said on the status line.""" + if state.record is None: + return + asked = {"--system": list(state.cli_systems), "--core": list(state.cores), + "--region": list(state.regions), "--emulator": list(state.emulators)} + parts = trim_recorded(asked, plat, manifest, manifest.get("files") or [], + manifest.get("omitted_files") or []) + if not parts: + return + choice = record_options(state.record["choice"]) + for part in parts: + if part.flag == "--system": + state.cli_systems = list(part.keep) + elif part.flag == "--core": + state.cores = list(part.keep) + elif part.flag == "--region": + state.regions = list(part.keep) + else: + state.emulators = list(part.keep) + choice = options_without(choice, part.flag, part.keep) + state.record = {**state.record, "choice": choice_words(choice)} + words = ", ".join(f"{part.flag} ({part.why})" for part in parts) + state.status = f"Left out of the recorded choice: {words}." + + +def _tui_record_drop(state: TuiState) -> None: + """The recorded choice given up: the choice nothing narrows.""" + state.cli_systems, state.cores, state.regions, state.record_flags = [], [], [], [] + state.no_game_data = state.installed_only = state.required_only = False + state.target, state.every_board, state.emulators = "", False, [] + state.for_games, state.games_folder, state.games_choice = None, None, "" + # The systems of the recorded check go, and with them its ticks. + state.systems, state.ticked, state.keep_ticked, state.widened_from = [], set(), None, None + + +def _tui_record_load(state: TuiState) -> None: + """Read the record of the setup and folder chosen, once for each, and + take its choice where nothing chose: the command line, or the screen + for an earlier folder.""" + key = (state.platform, state.dest) + if state.dest is None or state.record_for == key: + return + taken = tui_record_kept(state) + state.record_for = key + setup, path, problem = setup_record(state.platform, state.dest) + if problem and str(path) not in _RECORD_SAID: + _RECORD_SAID.add(str(path)) + state.status = f"{problem}, so it is ignored." + if taken: + _tui_record_drop(state) + # Games read on a card are what the plan found, not a choice. + found = ("--for-games", "--required-only") if state.games_choice in ("", "probe") else () + state.record = None + untouched = not any(flag in RECORD_NARROWING and flag not in found + for flag, _value in tui_flags(state)) + state.record = setup + if setup is not None and not state.cli_choice and untouched: + _tui_record_take(state) + + +def _start_check(state: TuiState) -> list[tuple]: + """Check the chosen folder, after fetching the file list once, the + systems ticked kept (_keep_choice).""" + _tui_record_load(state) + _keep_choice(state) + _games_look(state) + state.refocus = "" + if "plan" in state.stack: + del state.stack[state.stack.index("plan"):] + while state.stack and state.stack[-1] in ("checking", "error", "create"): + state.stack.pop() + state.stack.append("checking") + state.facts, state.ticked = {}, set() + if state.manifest is not None and state.manifest_for == state.platform: + return _start_run(state, "check") + state.manifest = None + state.run, state.run_id = "check", state.run_id + 1 + state.started = state.now + return [("fetch", state.platform)] + + +def _games_look(state: TuiState) -> None: + """For a folder chosen once a card was looked at for games, the new one + looked at too, from what this run already read (no network): its games + make "For the games on this card" the choice, unless the user or the + command line decided.""" + if (state.games_choice in ("user", "cli", "record") or state.games_probed is None + or state.dest is None or state.games_probed == (state.platform, state.dest)): + return + count = games_probe(state.platform, state.dest, network=False, os_type=state.os_type) + state.games_probed = (state.platform, state.dest) + state.games_here = count or 0 + state.for_games, state.games_choice = bool(count), "probe" + + +def _browse_to(state: TuiState, folder: Path | None, came_from: Path | None = None) -> None: + state.browse_dir = folder + copying = state.browse_for == "from" + state.browse_rows, state.status = _browse_rows(folder, copying or state.browse_for == "pack", + _dest_card(state) if copying else None) + if state.browse_for == "pack": + # A pack is a ZIP, never a folder. + state.browse_rows = [row for row in state.browse_rows if row.action != "use"] + focus = 0 + if (folder is not None and state.browse_for not in ("from", "pack", "packto") + and folder in (state.home, Path(folder.anchor))): + # No frontend reads its BIOS files from HOME or a drive's root: A + # pressed through the screens installed into ~. A goes into a + # visible subfolder there, never a card or drive listed first (A + # then took /mnt/d, the Windows D: drive, for the BIOS folder) nor + # .cache. With none, Use this folder keeps the focus and says where. + focus = next((index for index, row in enumerate(state.browse_rows) + if row.action == "dir" and isinstance(row.value, Path) + and row.label == f"{row.value.name}/" + and not _os_folder(row.value, folder)), 0) + if not focus and not state.status: + state.status = _no_setup_reads(state, folder) + # Never on the row the sentence warns against: A, A, A put a + # BizHawk install into the home folder. The first folder below + # it, hidden ones too, else the folder above. + focus = next((index for index, row in enumerate(state.browse_rows) + if row.action == "dir" and isinstance(row.value, Path) + and row.label == f"{row.value.name}/"), 0) or next( + (index for index, row in enumerate(state.browse_rows) + if row.action == "up"), 0) + if (folder is not None and state.browse_for == "from" and came_from is None + and folder in (state.home, Path(folder.anchor))): + # The old card is most often a mounted one: the docs say the copy + # browser opens on them, and A on Use this folder read all of HOME. + # Not the card being installed to: it was focused first, and taken. + mounts = [mount for mount in _mounted_cards() if mount != _dest_card(state)] + focus = next((index for index, row in enumerate(state.browse_rows) + if row.action == "dir" and row.value in mounts), focus) + for index, row in enumerate(state.browse_rows): + if came_from is not None and row.action == "dir" and row.value == came_from: + focus = index + state.focus["browse"] = focus + + +def _open_backups(state: TuiState, purpose: str) -> list[tuple]: + """The backups of the folder, read by a child: to look at them, or to + put one back or delete one.""" + state.backups_for, state.backups = purpose, None + _push(state, "backups") + return _start_run(state, "backups") + + +def _activate_pack_action(state: TuiState, row: TuiRow) -> list[tuple] | None: + """A and Enter on the pack screens, None on another row.""" + screen, action = state.screen, row.action + if screen == "more" and action == "packmake": + state.pack_plan = None + _push(state, "packplan") + return _start_run(state, "pack_plan") + if screen == "packplan" and action == "packwhere": + state.browse_for = "packto" + _push(state, "browse") + _browse_to(state, _nearest_folder(tui_pack_dir(state))) + return [] + if screen == "packplan" and action == "packgo": + state.pack_done, state.pack_code, state.pack_fetched = None, None, None + _push(state, "packdone") + return _start_run(state, "pack") + if screen == "packdone" and action == "close": + state.exit_code = state.pack_code if state.pack_code is not None else 1 + return [] + return None + + +def _activate_removal_action(state: TuiState, row: TuiRow) -> list[tuple] | None: + """A and Enter on More and the removal screens, None on another row.""" + screen, action = state.screen, row.action + if screen == "more" and action == "remove": + return _push(state, "remove") + if screen == "more": + return _open_backups(state, {"backups": "look", "putback": "restore", + "dropbackup": "delete_backup"}[action]) + if screen == "remove": + state.removal_scope = ([("--system", str(row.value))] if action == "rmsystem" + else [("--misplaced", "")] if action == "rmmisplaced" + else []) + state.removal = None + _push(state, "removal") + return _start_run(state, "removal_plan") + if screen == "removal" and action == "rmbackup": + state.removal_backup = not state.removal_backup + return [] + if screen == "removal" and action == "rmgo": + state.action = "remove" + return _push(state, "actconfirm") + return None + + +def _activate_backup_action(state: TuiState, row: TuiRow) -> list[tuple] | None: + """A and Enter on the backup and action screens, None on another row.""" + screen, action = state.screen, row.action + if screen == "backups" and action == "backup": + state.backup_chosen = (state.backups or [])[int(str(row.value))] + if state.backups_for == "look": + return _push(state, "backup") + state.action = state.backups_for + return _push(state, "actconfirm") + if screen == "backup": + state.action = "restore" if action == "bkrestore" else "delete_backup" + return _push(state, "actconfirm") + if screen == "actconfirm" and action == "actgo": + state.action_events, state.action_code = [], None + _replace(state, "actdone") + return _start_run(state, state.action) + if screen == "actdone" and action == "close": + state.exit_code = state.action_code if state.action_code is not None else 1 + return [] + if screen == "actdone" and action == "recheck": + # The record changed with the folder: read again, with its choice. + state.record_for = None + del state.stack[1:] + state.stack.append("checking") + return _start_check(state) + return None + + +def _activate_action(state: TuiState, row: TuiRow) -> list[tuple]: + """A and Enter on the More screens.""" + if row.action == "back": + return _back(state) + for activate in (_activate_pack_action, _activate_removal_action, _activate_backup_action): + effects = activate(state, row) + if effects is not None: + return effects + return [] + + +def _open_details(state: TuiState) -> list[tuple]: + if state.screen == "result": + # Looked at, they are said again in the terminal after Exit. + state.details_seen = True + return _push(state, "details") + + +def tui_quit(state: TuiState, code: int | None = None, asked: bool = True) -> list[tuple]: + """Leave now: with the install's own code once it ran, else QUIT_CODE + for a quit the user chose (q, Quit) and 130 for Ctrl-C. asked is a quit + the user chose, said as one: it read "stopped", the word of an + interrupted run.""" + state.quit_asked = asked + if code is None: + code = QUIT_CODE if asked else 130 + if state.screen == "result": + code = state.code if state.code is not None else 1 + elif state.screen == "error": + code = state.error_code + state.exit_code = code + return [("kill",)] if state.running else [] + + +def tui_interrupt(state: TuiState) -> list[tuple]: + """Ctrl-C: an install stops and says what it did, anything else ends.""" + if state.run == "install" and state.running: + if state.stopping: + return [("kill",)] + state.stopping = True + return [("stop",)] + return tui_quit(state, asked=False) diff --git a/wiki/architecture.md b/wiki/architecture.md index 801445fb8..4ed4a485d 100644 --- a/wiki/architecture.md +++ b/wiki/architecture.md @@ -506,7 +506,7 @@ those it imports: | What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games`, `tally`, `space`, `fetch_plan` | | Line mode | `destination`, `line_folder`, `line_menu`, `plan_words`, `plan`, `failure_causes`, `outcome` | | Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack`, `removal`, `handheld_app`, `cli` | -| Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_screens`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_session` | +| Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_screens`, `tui_steps`, `tui_folder_actions`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_session` | ## Tests