aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-06-25 00:06:42 +0700
committerShulhan <ms@kilabit.info>2019-07-09 22:15:44 +0700
commit9283d0f1385a63e1955150a4d2a36d5207a1f66b (patch)
tree2ac04ac404dcc602dea353a8a74f43ed49137aee
parent7cd0290b065ea9f6a09bd180aeafd8f113b7c617 (diff)
downloadpakakeh.go-9283d0f1385a63e1955150a4d2a36d5207a1f66b.tar.xz
ini: add an empty line before section statement
For readibility, each section should start with an empty line.
-rw-r--r--lib/ini/ini.go19
-rw-r--r--lib/ini/ini_example_test.go22
2 files changed, 30 insertions, 11 deletions
diff --git a/lib/ini/ini.go b/lib/ini/ini.go
index 1aca965e..c50c537b 100644
--- a/lib/ini/ini.go
+++ b/lib/ini/ini.go
@@ -511,12 +511,29 @@ func (in *Ini) Vars(sectionPath string) (vars map[string]string) {
// Write the current parsed Ini into writer `w`.
//
func (in *Ini) Write(w io.Writer) (err error) {
+ var (
+ endWithVar bool
+ v *variable
+ )
+
for x := 0; x < len(in.secs); x++ {
+ // Add an empty line before section statement.
+ if endWithVar {
+ fmt.Fprintln(w)
+ }
+
fmt.Fprint(w, in.secs[x])
- for _, v := range in.secs[x].vars {
+ for _, v = range in.secs[x].vars {
fmt.Fprint(w, v)
}
+
+ // Check if the last variable is an empty line.
+ if isLineModeVar(v.mode) {
+ endWithVar = true
+ } else {
+ endWithVar = false
+ }
}
return
diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go
index be51fa49..eeece746 100644
--- a/lib/ini/ini_example_test.go
+++ b/lib/ini/ini_example_test.go
@@ -32,8 +32,10 @@ func ExampleIni_Add() {
// k1 = true
// k1 = v1
// k1 = v2
+ //
// [s1 "sub"]
// k1 = v1
+ //
// [s2 "sub"]
// k1 = v1
}
@@ -136,11 +138,9 @@ key=value1
in.Prune()
- for _, sec := range in.secs {
- fmt.Printf("%s", sec)
- for _, v := range sec.vars {
- fmt.Printf("%s", v)
- }
+ err = in.Write(os.Stdout)
+ if err != nil {
+ log.Fatal(err)
}
// Output:
// [section]
@@ -148,6 +148,7 @@ key=value1
// key2 = true
// key = value2
// key2 = false
+ //
// [section "sub"]
// key = value2
// key = value1
@@ -188,11 +189,9 @@ func ExampleIni_Rebase() {
in.Rebase(in2)
- for _, sec := range in.secs {
- fmt.Printf("%s", sec)
- for _, v := range sec.vars {
- fmt.Printf("%s", v)
- }
+ err = in.Write(os.Stdout)
+ if err != nil {
+ log.Fatal(err)
}
// Output:
// [section]
@@ -200,6 +199,7 @@ func ExampleIni_Rebase() {
// key2 = true
// key = value2
// key2 = false
+ //
// [section "sub"]
// key = value2
// key = value1
@@ -286,9 +286,11 @@ key=value1
//key2=false
//
//keynotexist = value4
+ //
//[section "sub"]
//key=value2
//key=value3
+ //
//[sectionnotexist "sub"]
//key = value3
}