From 4bedd33c593ab0cb750e17c42750048904fdf7fb Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 7 May 2024 16:58:17 +0900 Subject: Refactor the code to remove global variables --- src/functions.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/functions.go (limited to 'src/functions.go') diff --git a/src/functions.go b/src/functions.go new file mode 100644 index 00000000..f16371a2 --- /dev/null +++ b/src/functions.go @@ -0,0 +1,35 @@ +package fzf + +import ( + "os" + "strings" + "unsafe" +) + +func writeTemporaryFile(data []string, printSep string) string { + f, err := os.CreateTemp("", "fzf-preview-*") + if err != nil { + // Unable to create temporary file + // FIXME: Should we terminate the program? + return "" + } + defer f.Close() + + f.WriteString(strings.Join(data, printSep)) + f.WriteString(printSep) + return f.Name() +} + +func removeFiles(files []string) { + for _, filename := range files { + os.Remove(filename) + } +} + +func stringBytes(data string) []byte { + return unsafe.Slice(unsafe.StringData(data), len(data)) +} + +func byteString(data []byte) string { + return unsafe.String(unsafe.SliceData(data), len(data)) +} -- cgit v1.2.3