From 1eb93a5ff0f956648a01fbacfe2a1ba974e373e6 Mon Sep 17 00:00:00 2001 From: yory8 Date: Sat, 16 May 2020 15:54:36 +0200 Subject: [PATCH] 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 --- main.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index e3d6081..30a9b60 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ var ( storer = app.Command("store", "Record clipboard events (run as argument to `wl-paste --watch`)") 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() + unix = storer.Flag("unix", "Normalize line endings to LF").Bool() picker = app.Command("pick", "Pick an item from clipboard history") 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 { // 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 atEOF { - return len(data), data, nil + b := data + if *unix { + b = dropCR(b) + } + return len(data), b, nil } // Request more data. 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 +}