feat: unix flag to normalize EOLs to LF

Vim doesn't normalize EOLs of pasted text according to the buffer's
filetype. Also see https://gitlab.gnome.org/GNOME/gtk/issues/2307
This commit is contained in:
yory8 2020-05-16 15:54:36 +02:00
parent bb95b603d0
commit 1eb93a5ff0

34
main.go
View file

@ -26,6 +26,7 @@ var (
storer = app.Command("store", "Record clipboard events (run as argument to `wl-paste --watch`)") storer = app.Command("store", "Record clipboard events (run as argument to `wl-paste --watch`)")
maxDemon = storer.Flag("max-items", "history size").Default("15").Int() maxDemon = storer.Flag("max-items", "history size").Default("15").Int()
noPersist = storer.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool() noPersist = storer.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
unix = storer.Flag("unix", "Normalize line endings to LF").Bool()
picker = app.Command("pick", "Pick an item from clipboard history") picker = app.Command("pick", "Pick an item from clipboard history")
maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int() maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int()
@ -200,14 +201,43 @@ func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if i := bytes.IndexByte(data, '\n'); i >= 0 { if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line. // We have a full newline-terminated line.
return i + 1, data[0 : i+1], nil b := data[0 : i+1]
if *unix {
b = dropCR(b)
}
return i + 1, b, nil
} }
// If we're at EOF, we have a final, non-terminated line. Return it. // If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF { if atEOF {
return len(data), data, nil b := data
if *unix {
b = dropCR(b)
}
return len(data), b, nil
} }
// Request more data. // Request more data.
return 0, nil, nil return 0, nil, nil
} }
// dropCR drops a terminal \r from the data. Modified from Go's Stdlib
func dropCR(data []byte) []byte {
orig := data
var lf bool
if len(data) > 0 && data[len(data)-1] == '\n' {
lf = true
data = data[0 : len(data)-1]
}
if len(data) > 0 && data[len(data)-1] == '\r' {
b := data[0 : len(data)-1]
if lf {
b = append(b, '\n')
}
return b
}
return orig
}