mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-05 22:50:21 +09:00
Start implementing backup system
This commit is contained in:
49
internal/buffer/backup.go
Normal file
49
internal/buffer/backup.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user