Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,22 @@ func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
}
}

// Count and generate alignment length and buffer spaces
// Count and generate alignment length and buffer spaces using the
// longest key. Keys may be modifed if they contain certain characters so
// we need to take that into account in our calculation.
alignLength := 0
if PrettyFormat {
for i := 0; i < len(sec.keyList); i++ {
if len(sec.keyList[i]) > alignLength {
alignLength = len(sec.keyList[i])
for _, kname := range sec.keyList {
keyLength := len(kname)
// First case will surround key by ` and second by """
if strings.ContainsAny(kname, "\"=:") {
keyLength += 2
} else if strings.Contains(kname, "`") {
keyLength += 6
}

if keyLength > alignLength {
alignLength = keyLength
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func Test_File_SaveTo(t *testing.T) {
cfg.Section("author").Comment = `Information about package author
# Bio can be written in multiple lines.`
cfg.Section("advanced").Key("val w/ pound").SetValue("my#password")
cfg.Section("advanced").Key("longest key has a colon : yes/no").SetValue("yes")
So(cfg.SaveTo("testdata/conf_out.ini"), ShouldBeNil)

cfg.Section("author").Key("NAME").Comment = "This is author name"
Expand Down