Skip to content
Snippets Groups Projects
Commit 31d202e2 authored by Thomas Bellman's avatar Thomas Bellman
Browse files

ini_file: Align = signs in generated file.

To make the file generated by cfgfile::ini_file prettier and easier
to read, align the equal signs for settings within each section.  I.e,
generate a file looking like

    [section-1]
    foo    = 10
    gazonk = 20

    [section-2]
    sarsaparilla = 4711
    smurf        = 90000

We limit how far out we align the equal signs, though, as otherwise
a single very long setting name would just make things more difficult
to read.  Thus, we will generate e.g.

    [section]
    foo          = 1
    gazonk       = 2
    setting_name_that_is_way_too_long = 3
    zarzaparilla = 4

The maximum alignment column is hardcoded to 24.
parent 3d9d48e3
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2014-2020 Thomas Bellman. # Copyright (C) 2014-2024 Thomas Bellman.
# Licensed under the GNU LGPL v3+; see the README file for more information. # Licensed under the GNU LGPL v3+; see the README file for more information.
...@@ -37,17 +37,10 @@ define cfgfile::ini_file($settings, $ensure='present', ...@@ -37,17 +37,10 @@ define cfgfile::ini_file($settings, $ensure='present',
{ {
if ($ensure == 'present') if ($ensure == 'present')
{ {
$content = inline_template(
'<%= @settings.sort.collect { |section_name,section_vars|
"[" + section_name + "]\n" +
section_vars.sort.collect { |k,v|
k + " = " + v.to_s
}.join("\n")
}.join("\n\n") + "\n"
%>')
file { file {
$name: $name:
ensure => file, content => $content, ensure => file,
content => template('cfgfile/ini_file.erb'),
owner => $owner, group => $group, mode => $mode; owner => $owner, group => $group, mode => $mode;
} }
} }
......
<%
# Copyright (C) 2024 Thomas Bellman.
# Licensed under the GNU LGPL v3+; see the README file for more information.
#
-%>
<% @settings.sort().each do |section_name, section_vars| -%>
[<%= section_name %>]
<% # Want to align = signs, but only for "short" names
max_align_pos = 24
shortnames = section_vars.keys().select { |name|
name.length() < max_align_pos - 1
}
namewidth = ( shortnames.max_by { |name| name.length() } or "" ).length()
section_vars.sort.each do |key,value|
-%>
<%= sprintf('%-*s = %s', namewidth, key, value.to_s).rstrip() %>
<% end -%>
<% end -%>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment