X Tutup
Skip to content

Commit be07983

Browse files
Only write UTF-8 BOM on Windows where it is needed
Fixes cli#5239
1 parent 31c6ba3 commit be07983

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

pkg/surveyext/editor_manual.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"os/exec"
9+
"runtime"
910

1011
"github.com/cli/safeexec"
1112
shellquote "github.com/kballard/go-shellquote"
@@ -27,6 +28,19 @@ func defaultLookPath(name string) ([]string, []string, error) {
2728
return []string{exe}, nil, nil
2829
}
2930

31+
func needsBom() bool {
32+
// The reason why we do this is because notepad.exe on Windows determines the
33+
// encoding of an "empty" text file by the locale, for example, GBK in China,
34+
// while golang string only handles utf8 well. However, a text file with utf8
35+
// BOM header is not considered "empty" on Windows, and the encoding will then
36+
// be determined utf8 by notepad.exe, instead of GBK or other encodings.
37+
38+
// This could be enhanced in the future by doing this only when a non-utf8
39+
// locale is in use, and possibly doing that for any OS, not just windows.
40+
41+
return runtime.GOOS == "windows"
42+
}
43+
3044
func edit(editorCommand, fn, initialValue string, stdin io.Reader, stdout io.Writer, stderr io.Writer, cursor showable, lookPath func(string) ([]string, []string, error)) (string, error) {
3145
// prepare the temp file
3246
pattern := fn
@@ -39,14 +53,11 @@ func edit(editorCommand, fn, initialValue string, stdin io.Reader, stdout io.Wri
3953
}
4054
defer os.Remove(f.Name())
4155

42-
// write utf8 BOM header
43-
// The reason why we do this is because notepad.exe on Windows determines the
44-
// encoding of an "empty" text file by the locale, for example, GBK in China,
45-
// while golang string only handles utf8 well. However, a text file with utf8
46-
// BOM header is not considered "empty" on Windows, and the encoding will then
47-
// be determined utf8 by notepad.exe, instead of GBK or other encodings.
48-
if _, err := f.Write(bom); err != nil {
49-
return "", err
56+
// write utf8 BOM header if necessary for the current platform and/or locale
57+
if needsBom() {
58+
if _, err := f.Write(bom); err != nil {
59+
return "", err
60+
}
5061
}
5162

5263
// write initial value

0 commit comments

Comments
 (0)
X Tutup