Better backup behavior

This commit is contained in:
Zachary Yedidia
2019-12-21 23:26:53 -05:00
parent a9bb1f35da
commit c4d5d7c195
4 changed files with 25 additions and 7 deletions

View File

@@ -190,6 +190,10 @@ func main() {
if err := recover(); err != nil {
screen.Screen.Fini()
fmt.Println("Micro encountered an error:", err)
// backup all open buffers
for _, b := range buffer.OpenBuffers {
b.Backup(false)
}
// Print the stack trace too
fmt.Print(errors.Wrap(err, 2).ErrorStack())
os.Exit(1)

View File

@@ -25,15 +25,17 @@ The backup was created at %s.
Options: [r]ecover, [i]gnore: `
// Backup saves the current buffer to ConfigDir/backups
func (b *Buffer) Backup() error {
func (b *Buffer) Backup(checkTime bool) error {
if !b.Settings["backup"].(bool) {
return nil
}
sub := time.Now().Sub(b.lastbackup)
if sub < time.Duration(backup_time)*time.Millisecond {
log.Println("Backup event but not enough time has passed", sub)
return nil
if checkTime {
sub := time.Now().Sub(b.lastbackup)
if sub < time.Duration(backup_time)*time.Millisecond {
log.Println("Backup event but not enough time has passed", sub)
return nil
}
}
b.lastbackup = time.Now()

View File

@@ -217,6 +217,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
if choice%2 == 0 {
// recover
b.LineArray = NewLineArray(uint64(size), FFAuto, backup)
b.isModified = true
} else if choice%2 == 1 {
// delete
os.Remove(backupfile)
@@ -334,7 +335,7 @@ func (b *Buffer) Insert(start Loc, text string) {
b.EventHandler.active = b.curCursor
b.EventHandler.Insert(start, text)
go b.Backup()
go b.Backup(true)
}
}
@@ -344,7 +345,7 @@ func (b *Buffer) Remove(start, end Loc) {
b.EventHandler.active = b.curCursor
b.EventHandler.Remove(start, end)
go b.Backup()
go b.Backup(true)
}
}

View File

@@ -22,6 +22,17 @@ Here are the options that you can set:
default value: `0`
* `backup`: micro will automatically keep backups of all open buffers. Backups
are stored in `~/.config/micro/backups` and are removed when the buffer is
closed cleanly. In the case of a system crash or a micro crash, the contents
of the buffer can be recovered automatically by opening the file that
was being edited before the crash, or manually by searching for the backup
in the backup directory. Backups are made in the background when a buffer is
modified and the latest backup is more than 8 seconds old, or when micro
detects a crash. It is highly recommended that you leave this feature enabled.
default value: `true`
* `basename`: in the infobar, show only the basename of the file being edited
rather than the full path.