Git core.quotePath is enabled by default and causes unicode in filenames
to be quoted. Unquote filenames and requote them to escape control
characters BUT not unicode characters.
Fixes: https://github.com/charmbracelet/soft-serve/issues/457
@@ -2,6 +2,7 @@ package common
import (
"fmt"
+ "strconv"
"strings"
"github.com/alecthomas/chroma/v2/lexers"
@@ -54,3 +55,17 @@ func FormatHighlight(p, c string) (string, error) {
}
return r.String(), nil
}
+
+// UnquoteFilename unquotes a filename.
+// When Git is with "core.quotePath" set to "true" (default), it will quote
+// the filename with double quotes if it contains control characters or unicode.
+// this function will unquote the filename.
+func UnquoteFilename(s string) string {
+ name := s
+ if n, err := strconv.Unquote(`"` + s + `"`); err == nil {
+ name = n
+ }
+
+ name = strconv.Quote(name)
+ return strings.Trim(name, `"`)
+}
@@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/fs"
- "strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
@@ -23,16 +22,12 @@ type FileItem struct {
// ID returns the ID of the file item.
func (i FileItem) ID() string {
- name := i.entry.Name()- if n, err := strconv.Unquote(name); err == nil {- name = n- }- return name
+ return i.entry.Name()
}
// Title returns the title of the file item.
func (i FileItem) Title() string {
- return i.entry.Name()
+ return common.UnquoteFilename(i.entry.Name())
}
// Description returns the description of the file item.