parent
31c6ba3807
commit
be07983a62
1 changed files with 19 additions and 8 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
|
||||
"github.com/cli/safeexec"
|
||||
shellquote "github.com/kballard/go-shellquote"
|
||||
|
|
@ -27,6 +28,19 @@ func defaultLookPath(name string) ([]string, []string, error) {
|
|||
return []string{exe}, nil, nil
|
||||
}
|
||||
|
||||
func needsBom() bool {
|
||||
// The reason why we do this is because notepad.exe on Windows determines the
|
||||
// encoding of an "empty" text file by the locale, for example, GBK in China,
|
||||
// while golang string only handles utf8 well. However, a text file with utf8
|
||||
// BOM header is not considered "empty" on Windows, and the encoding will then
|
||||
// be determined utf8 by notepad.exe, instead of GBK or other encodings.
|
||||
|
||||
// This could be enhanced in the future by doing this only when a non-utf8
|
||||
// locale is in use, and possibly doing that for any OS, not just windows.
|
||||
|
||||
return runtime.GOOS == "windows"
|
||||
}
|
||||
|
||||
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) {
|
||||
// prepare the temp file
|
||||
pattern := fn
|
||||
|
|
@ -39,14 +53,11 @@ func edit(editorCommand, fn, initialValue string, stdin io.Reader, stdout io.Wri
|
|||
}
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
// write utf8 BOM header
|
||||
// The reason why we do this is because notepad.exe on Windows determines the
|
||||
// encoding of an "empty" text file by the locale, for example, GBK in China,
|
||||
// while golang string only handles utf8 well. However, a text file with utf8
|
||||
// BOM header is not considered "empty" on Windows, and the encoding will then
|
||||
// be determined utf8 by notepad.exe, instead of GBK or other encodings.
|
||||
if _, err := f.Write(bom); err != nil {
|
||||
return "", err
|
||||
// write utf8 BOM header if necessary for the current platform and/or locale
|
||||
if needsBom() {
|
||||
if _, err := f.Write(bom); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// write initial value
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue