Repository: pimutils/khal

Issue:
First time configuration did not set up a 'default' section.
This was on version 0.9.10 installed on Ubuntu 20.04
`khal configure` did not create a default section the first time I ran it. This produced a very confusing error saying
```
    user@user-desktop:~$ khal new dentist
    Usage: khal new [OPTIONS] [START [END | DELTA] [TIMEZONE] [SUMMARY] [::
                                DESCRIPTION]]

    Error: Invalid value: No default calendar is configured, please provide one explicitly.
```

It's not a good user experience that the calendar is not ready to add new events after initial configuration.
One would expect that the default_calendar should be set to (at the moment of initial configuration) the only calendar listed in the config, or some other comment be created in the config file indicating how to set it up.

Discussion:

Patch:
diff --git a/khal/configwizard.py b/khal/configwizard.py
index 3194889..bb67f81 100644
--- a/khal/configwizard.py
+++ b/khal/configwizard.py
@@ -28,7 +28,7 @@ from os import makedirs
 from os.path import exists, expanduser, expandvars, isdir, join, normpath
 
 import xdg
-from click import UsageError, confirm, prompt
+from click import UsageError, confirm, prompt, Choice
 
 from .exceptions import FatalError
 from .settings import settings
@@ -110,6 +110,19 @@ def choose_time_format():
     return timeformat
 
 
+def choose_default_calendar(vdirs):
+    names = [name for name, _, _ in sorted(vdirs or ())]
+    print("Which calendar do you want as a default calendar?")
+    print("(The default calendar is specified, when no calendar is specified.)")
+    print("Configured calendars: {}".format(', '.join(names)))
+    default_calendar = prompt(
+        "Please type one of the above options",
+        default=names[0],
+        type=Choice(names),
+    )
+    return default_calendar
+
+
 def get_vdirs_from_vdirsyncer_config():
     """trying to load vdirsyncer's config and read all vdirs from it"""
     print("If you use vdirsyncer to sync with CalDAV servers, we can try to "
@@ -171,7 +184,7 @@ def create_vdir(names=[]):
     return [(name, path, 'calendar')]
 
 
-def create_config(vdirs, dateformat, timeformat):
+def create_config(vdirs, dateformat, timeformat, default_calendar=None):
     config = ['[calendars]']
     for name, path, type_ in sorted(vdirs or ()):
         config.append('\n[[{name}]]'.format(name=name))
@@ -187,8 +200,11 @@ def create_config(vdirs, dateformat, timeformat):
                   .format(timeformat=timeformat,
                           dateformat=dateformat,
                           longdateformat=dateformat))
-
+    if default_calendar:
+        config.append('[default]')
+        config.append('default_calendar = {}\n'.format(default_calendar))
     config = '\n'.join(config)
+
     return config
 
 
@@ -215,7 +231,16 @@ def configwizard():
     if not vdirs:
         print("\nWARNING: no vdir configured, khal will not be usable like this!\n")
 
-    config = create_config(vdirs, dateformat=dateformat, timeformat=timeformat)
+    print()
+    if vdirs:
+        default_calendar = choose_default_calendar(vdirs)
+    else:
+        default_calendar = None
+
+    config = create_config(
+        vdirs, dateformat=dateformat, timeformat=timeformat,
+        default_calendar=default_calendar,
+    )
     config_path = join(xdg.BaseDirectory.xdg_config_home, 'khal', 'config')
     if not confirm(
             "Do you want to write the config to {}? "


Test patch:
diff --git a/tests/cli_test.py b/tests/cli_test.py
index 5e354a5..75f341e 100644
--- a/tests/cli_test.py
+++ b/tests/cli_test.py
@@ -553,6 +553,7 @@ def test_color_option(runner):
 def choices(dateformat=0, timeformat=0,
             parse_vdirsyncer_conf=True,
             create_vdir=False,
+            default_calendar='',
             write_config=True):
     """helper function to generate input for testing `configure`"""
     confirm = {True: 'y', False: 'n'}
@@ -563,7 +564,9 @@ def choices(dateformat=0, timeformat=0,
     ]
     if not parse_vdirsyncer_conf:
         out.append(confirm[create_vdir])
+    out.append(default_calendar)
     out.append(confirm[write_config])
+    out.append('')
     return '\n'.join(out)
 
 
@@ -642,6 +645,9 @@ dateformat = %Y-%m-%d
 longdateformat = %Y-%m-%d
 datetimeformat = %Y-%m-%d %H:%M
 longdatetimeformat = %Y-%m-%d %H:%M
+
+[default]
+default_calendar = events_local
 '''
 
     # if aborting, no config file should be written
@@ -735,6 +741,9 @@ dateformat = %Y-%m-%d
 longdateformat = %Y-%m-%d
 datetimeformat = %Y-%m-%d %H:%M
 longdatetimeformat = %Y-%m-%d %H:%M
+
+[default]
+default_calendar = private
 '''.format(runner.xdg_data_home)
 
     # running configure again, should yield another vdir path, as the old