From 1818bc0067ed8d272b38fcd81542c93d099acfd7 Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@nsc.liu.se>
Date: Fri, 19 Jul 2024 03:16:43 +0200
Subject: [PATCH] ini_file: Sort [default] sections before others.

It is common (but not universal) for INI-style files to have a section
named [default], or [DEFAULT], which holds defaults for all other
sections.  For example, the 'configparser' Python module implements
that behavious.

While the [default] section typically doesn't *have* to be placed
before the sections it provides defaults for, it tends to make the
file easier to read if it *is* placed first in the file.

We thus here make sure to put all sections named "default" (case-
insensitive) first in the generated file.
---
 templates/ini_file.erb | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/templates/ini_file.erb b/templates/ini_file.erb
index 59b9019..9efc154 100644
--- a/templates/ini_file.erb
+++ b/templates/ini_file.erb
@@ -3,7 +3,17 @@
  # Licensed under the GNU LGPL v3+; see the README file for more information.
  #
 -%>
-<% @settings.sort().each do |section_name, section_vars| -%>
+<%
+    # Sort sections named "default" (case in-sensitive) before others
+    sorted_sections = @settings.keys().select { |k|
+       k.downcase() == 'default'
+    }.sort()
+    sorted_sections += @settings.keys().reject { |k|
+       sorted_sections.include?(k)
+    }.sort()
+    sorted_sections.each do |section_name|
+       section_vars = @settings[section_name]
+-%>
 [<%= section_name %>]
 <%    # Want to align = signs, but only for "short" names
       max_align_pos = 24
-- 
GitLab