From 25ded8c464024e6bef81d6678c69110fdbf15618 Mon Sep 17 00:00:00 2001 From: cos Date: Tue, 25 May 2021 20:42:51 +0200 Subject: Wrap question about how to handle config in a loop This commit attempts to address these two issues: Whenever calendar-cli is run on a clean new user account lacking a ~/.config directory, configuration is lost and has to be remade. There might be other cases for an Exception here and they are all annoying enough to warrant a change in behaviour. From a usability perspective, it is easy to be pressing enter a bit too quickly to stop at the save prompt. This is especially true if merely changing one or a couple of values while leaving the rest of them at their old value. Reasking to require an actual answer on what to do would be an improvement. --- calendar-cli.py | 60 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/calendar-cli.py b/calendar-cli.py index e9a36a5..da7a615 100755 --- a/calendar-cli.py +++ b/calendar-cli.py @@ -274,31 +274,43 @@ def interactive_config(args, config, remaining_argv): if not modified: print("No configuration changes have been done") else: - options = [] - if section: - options.append(('save', 'save configuration into section %s' % section)) - if backup or not section: - options.append(('save_other', 'add this new configuration into a new section in the configuration file')) - if remaining_argv: - options.append(('use', 'use this configuration without saving')) - options.append(('abort', 'abort without saving')) - print("CONFIGURATION DONE ...") - for o in options: - print("Type %s if you want to %s" % o) - cmd = raw_input("Enter a command: ") - if cmd in ('save', 'save_other'): - if cmd == 'save_other': - new_section = raw_input("New config section name: ") - config[new_section] = config[section] - if backup: - config[section] = backup + state = 'start' + while state == 'start': + options = [] + if section: + options.append(('save', 'save configuration into section %s' % section)) + if backup or not section: + options.append(('save_other', 'add this new configuration into a new section in the configuration file')) + if remaining_argv: + options.append(('use', 'use this configuration without saving')) + options.append(('abort', 'abort without saving')) + print("CONFIGURATION DONE ...") + for o in options: + print("Type %s if you want to %s" % o) + cmd = raw_input("Enter a command: ") + if cmd in ('use', 'abort'): + state = 'done' + if cmd in ('save', 'save_other'): + if cmd == 'save_other': + new_section = raw_input("New config section name: ") + config[new_section] = config[section] + if backup: + config[section] = backup + else: + del config[section] + section = new_section + try: + if os.path.isfile(args.config_file): + os.rename(args.config_file, "%s.%s.bak" % (args.config_file, int(time.time()))) + with open(args.config_file, 'w') as outfile: + json.dump(config, outfile, indent=4) + except Exception as e: + print(e) else: - del config[section] - section = new_section - if os.path.isfile(args.config_file): - os.rename(args.config_file, "%s.%s.bak" % (args.config_file, int(time.time()))) - with open(args.config_file, 'w') as outfile: - json.dump(config, outfile, indent=4) + print("Saved config") + state = 'done' + + if args.config_section == 'default' and section != 'default': -- cgit v1.2.3