From a86a6c464ecf43f49ced55f87ce45ec593694d0e Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 21 Dec 2019 16:35:09 -0500 Subject: [PATCH] Start implementing backup system --- internal/buffer/backup.go | 49 +++++++++++++++++++++++++++++++++++++ internal/config/settings.go | 1 + 2 files changed, 50 insertions(+) create mode 100644 internal/buffer/backup.go diff --git a/internal/buffer/backup.go b/internal/buffer/backup.go new file mode 100644 index 00000000..e9b91bcb --- /dev/null +++ b/internal/buffer/backup.go @@ -0,0 +1,49 @@ +package buffer + +import ( + "io" + + "github.com/zyedidia/micro/internal/config" + "github.com/zyedidia/micro/internal/util" + "golang.org/x/text/encoding" +) + +// Backup saves the current buffer to ConfigDir/backups +func (b *Buffer) Backup() error { + if !b.Settings["backup"].(bool) { + return nil + } + + name := config.ConfigDir + "/backups" + util.EscapePath(b.AbsPath) + + err := overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) { + if len(b.lines) == 0 { + return + } + + // end of line + eol := []byte{'\n'} + + // write lines + if _, e = file.Write(b.lines[0].data); e != nil { + return + } + + for _, l := range b.lines[1:] { + if _, e = file.Write(eol); e != nil { + return + } + if _, e = file.Write(l.data); e != nil { + return + } + } + return + }) + + return err +} + +// ApplyBackup applies the corresponding backup file to this buffer (if one exists) +func (b *Buffer) ApplyBackup() error { + return nil +} diff --git a/internal/config/settings.go b/internal/config/settings.go index 19d2586c..f8b42675 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -155,6 +155,7 @@ func GetGlobalOption(name string) interface{} { var defaultCommonSettings = map[string]interface{}{ "autoindent": true, + "backup": true, "basename": false, "colorcolumn": float64(0), "cursorline": true,