fix: sanitize names on file operations (#2546)
* fix: sanitize names on file operations * dont strip internal colors * minimize sanitation * cover history entries * minor improvement * fix patch * optimize patch
This commit is contained in:
1 parent
42e567abbd
commit
0ca4d75b9b
3 files changed
+56
-20
No files matched your search
@@ -596,12 +596,12 @@ func (ui *ui) echo(msg string) {
|
||||
}
|
||||
|
||||
func (ui *ui) echomsg(msg string) {
|
||||
ui.echo(msg)
|
||||
ui.echo(sanitizeMessage(msg))
|
||||
log.Print(msg)
|
||||
}
|
||||
|
||||
func (ui *ui) echoerr(msg string) {
|
||||
ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg))
|
||||
ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), sanitizeName(msg)))
|
||||
log.Printf("error: %s", msg)
|
||||
}
|
||||
|
||||
@@ -1171,6 +1171,11 @@ func (ui *ui) draw(nav *nav) {
|
||||
}
|
||||
}
|
||||
|
||||
// sanitize cmd-line buffers at render time
|
||||
cmdPrefix := sanitizeName(ui.cmdPrefix)
|
||||
cmdAccLeft := sanitizeName(ui.cmdAccLeft)
|
||||
cmdAccRight := sanitizeName(ui.cmdAccRight)
|
||||
|
||||
switch ui.cmdPrefix {
|
||||
case "":
|
||||
if gOpts.rulerfmt == "" {
|
||||
@@ -1182,16 +1187,16 @@ func (ui *ui) draw(nav *nav) {
|
||||
ui.screen.HideCursor()
|
||||
case ">":
|
||||
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
||||
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
||||
left := truncateLeft(ui.cmdAccLeft, maxWidth-displaywidth.String(prefix)-printLength(ui.msg))
|
||||
prefix := truncateRight(cmdPrefix, maxWidth)
|
||||
left := truncateLeft(cmdAccLeft, maxWidth-displaywidth.String(prefix)-printLength(ui.msg))
|
||||
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg)
|
||||
ui.msgWin.print(ui.screen, displaywidth.String(prefix)+printLength(ui.msg), 0, st, left+ui.cmdAccRight)
|
||||
ui.msgWin.print(ui.screen, displaywidth.String(prefix)+printLength(ui.msg), 0, st, left+cmdAccRight)
|
||||
ui.screen.ShowCursor(ui.msgWin.x+displaywidth.String(prefix)+printLength(ui.msg)+displaywidth.String(left), ui.msgWin.y)
|
||||
default:
|
||||
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
||||
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
||||
left := truncateLeft(ui.cmdAccLeft, maxWidth-displaywidth.String(prefix))
|
||||
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+ui.cmdAccRight)
|
||||
prefix := truncateRight(cmdPrefix, maxWidth)
|
||||
left := truncateLeft(cmdAccLeft, maxWidth-displaywidth.String(prefix))
|
||||
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+cmdAccRight)
|
||||
ui.screen.ShowCursor(ui.msgWin.x+displaywidth.String(prefix)+displaywidth.String(left), ui.msgWin.y)
|
||||
}
|
||||
|
||||
@@ -1274,7 +1279,7 @@ func listMatchingBinds(binds map[string]expr, prefix string) string {
|
||||
fmt.Fprintln(t, "key\tcommand")
|
||||
for _, k := range slices.Sorted(maps.Keys(binds)) {
|
||||
remain, _ := strings.CutPrefix(k, prefix)
|
||||
fmt.Fprintf(t, "%s\t%v\n", remain, binds[k])
|
||||
fmt.Fprintf(t, "%s\t%v\n", sanitizeName(remain), binds[k])
|
||||
}
|
||||
t.Flush()
|
||||
|
||||
@@ -1305,13 +1310,14 @@ func listJumps(jumps []string, ind int) string {
|
||||
fmt.Fprintln(t, " jump\tpath")
|
||||
// print jumps in order of most recent, Vim uses the opposite order
|
||||
for i := len(jumps) - 1; i >= 0; i-- {
|
||||
path := jumps[i]
|
||||
switch {
|
||||
case i < ind:
|
||||
fmt.Fprintf(t, " %*d\t%s\n", maxlength, ind-i, jumps[i])
|
||||
fmt.Fprintf(t, " %*d\t%s\n", maxlength, ind-i, path)
|
||||
case i > ind:
|
||||
fmt.Fprintf(t, " %*d\t%s\n", maxlength, i-ind, jumps[i])
|
||||
fmt.Fprintf(t, " %*d\t%s\n", maxlength, i-ind, path)
|
||||
default:
|
||||
fmt.Fprintf(t, "> %*d\t%s\n", maxlength, 0, jumps[i])
|
||||
fmt.Fprintf(t, "> %*d\t%s\n", maxlength, 0, path)
|
||||
}
|
||||
}
|
||||
t.Flush()
|
||||
@@ -1342,7 +1348,7 @@ func listMarks(marks map[string]string) string {
|
||||
t.Init(b, 0, gOpts.tabstop, 2, '\t', 0)
|
||||
fmt.Fprintln(t, "mark\tpath")
|
||||
for _, k := range slices.Sorted(maps.Keys(marks)) {
|
||||
fmt.Fprintf(t, "%s\t%s\n", k, marks[k])
|
||||
fmt.Fprintf(t, "%s\t%s\n", sanitizeName(k), sanitizeName(marks[k]))
|
||||
}
|
||||
t.Flush()
|
||||
|
||||
@@ -1628,10 +1634,15 @@ func listMatches(screen tcell.Screen, matches []compMatch, selectedInd int) (str
|
||||
return "", nil
|
||||
}
|
||||
|
||||
names := make([]string, len(matches))
|
||||
for i, m := range matches {
|
||||
names[i] = sanitizeName(m.name)
|
||||
}
|
||||
|
||||
wtot, _ := screen.Size()
|
||||
wcol := 0
|
||||
for _, m := range matches {
|
||||
wcol = max(wcol, printLength(m.name))
|
||||
for _, n := range names {
|
||||
wcol = max(wcol, printLength(n))
|
||||
}
|
||||
wcol += gOpts.tabstop - wcol%gOpts.tabstop
|
||||
ncol := max(wtot/wcol, 1)
|
||||
@@ -1639,19 +1650,19 @@ func listMatches(screen tcell.Screen, matches []compMatch, selectedInd int) (str
|
||||
var b strings.Builder
|
||||
b.WriteString("possible matches")
|
||||
|
||||
for i, match := range matches {
|
||||
for i, n := range names {
|
||||
if i%ncol == 0 {
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
w := printLength(match.name)
|
||||
fmt.Fprintf(&b, "%s%*s", match.name, wcol-w, "")
|
||||
w := printLength(n)
|
||||
fmt.Fprintf(&b, "%s%*s", n, wcol-w, "")
|
||||
}
|
||||
|
||||
b.WriteByte('\n')
|
||||
|
||||
var selection *menuSelect
|
||||
if selectedInd != -1 {
|
||||
selection = &menuSelect{selectedInd % ncol * wcol, selectedInd/ncol + 1, matches[selectedInd].name}
|
||||
selection = &menuSelect{selectedInd % ncol * wcol, selectedInd/ncol + 1, names[selectedInd]}
|
||||
}
|
||||
|
||||
return b.String(), selection
|
||||
|
||||
Reference in new issue
Block a user