From c50e25558744b1c89ca43ba6deeda57e6fa7f745 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Thu, 9 Oct 2025 11:22:57 +0800 Subject: [PATCH] Allow saving default config on first run When running menuconfig.py or guiconfig.py for the first time without an existing '.config' file, users were unable to save the default config. The tools would exit without prompting to save. This matches the behavior of the Linux kernel's 'make menuconfig' and improves the user experience for first-time users. --- guiconfig.py | 28 ++++++++++++++++++++++------ menuconfig.py | 23 +++++++++++++++++------ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/guiconfig.py b/guiconfig.py index 89dfe90..e059077 100755 --- a/guiconfig.py +++ b/guiconfig.py @@ -257,12 +257,13 @@ def menuconfig(kconf): def _load_config(): # Loads any existing .config file. See the Kconfig.load_config() docstring. # - # Returns True if .config is missing or outdated. We always prompt for - # saving the configuration in that case. + # Returns True if .config is missing or outdated. We prompt for saving the + # configuration if there are actual changes or if no .config file exists + # (so user can save the default configuration). print(_kconf.load_config()) if not os.path.exists(_conf_filename): - # No .config + # No .config exists - treat as changed so user can save defaults return True return _needs_save() @@ -1774,17 +1775,32 @@ def _vis_after(item): def _on_quit(_=None): # Called when the user wants to exit - if not _conf_changed: + config_exists = os.path.exists(_conf_filename) + + if not _conf_changed and config_exists: _quit("No changes to save (for '{}')".format(_conf_filename)) return + # Adjust dialog message if .config doesn't exist + if not config_exists: + dialog_title = "Quit" + dialog_message = ( + "No configuration file found.\nSave new configuration before quitting?" + ) + else: + dialog_title = "Quit" + dialog_message = "Save changes?" + while True: - ync = messagebox.askyesnocancel("Quit", "Save changes?") + ync = messagebox.askyesnocancel(dialog_title, dialog_message) if ync is None: return if not ync: - _quit("Configuration ({}) was not saved".format(_conf_filename)) + if not config_exists: + _quit("Configuration was not saved") + else: + _quit("Configuration ({}) was not saved".format(_conf_filename)) return if _try_save(_kconf.write_config, _conf_filename, "configuration"): diff --git a/menuconfig.py b/menuconfig.py index f4aed51..cd301bd 100755 --- a/menuconfig.py +++ b/menuconfig.py @@ -863,13 +863,14 @@ def _wrapper(func): def _load_config(): # Loads any existing .config file. See the Kconfig.load_config() docstring. # - # Returns True if .config exists and is outdated. We prompt for saving the - # configuration only if there are actual changes. + # Returns True if .config exists and is outdated, or if .config doesn't exist + # at all. We prompt for saving the configuration if there are actual changes + # or if no .config file exists (so user can save the default configuration). print(_kconf.load_config()) if not os.path.exists(_conf_filename): - # No .config - no changes yet - return False + # No .config exists - treat as changed so user can save defaults + return True return _needs_save() @@ -1071,13 +1072,21 @@ def _menuconfig(stdscr): def _quit_dialog(): - if not _conf_changed: + config_exists = os.path.exists(_conf_filename) + + if not _conf_changed and config_exists: return "No changes to save (for '{}')".format(_conf_filename) # Use button dialog with Yes/No/Cancel buttons (matching lxdialog style) + # Adjust message if .config doesn't exist + if not config_exists: + dialog_text = "No configuration file found.\nSave new configuration?" + else: + dialog_text = "Save configuration?" + result = _button_dialog( None, # No title in yesno dialog - "Save configuration?", + dialog_text, [" Yes ", " No ", " Cancel "], default_button=0, ) @@ -1094,6 +1103,8 @@ def _quit_dialog(): return None elif result == 1: # No + if not config_exists: + return "Configuration was not saved" return "Configuration ({}) was not saved".format(_conf_filename)