]> git.lizzy.rs Git - micro.git/blobdiff - internal/buffer/backup.go
Fix v2 import path for go mod
[micro.git] / internal / buffer / backup.go
index 5b87e9067a4f54fe906ec7a79f392857251b6eeb..bf5e6b717159987fd46e061c8bb9ba8008b37a7a 100644 (file)
@@ -1,13 +1,15 @@
 package buffer
 
 import (
+       "fmt"
        "io"
-       "log"
        "os"
+       "path/filepath"
        "time"
 
-       "github.com/zyedidia/micro/internal/config"
-       "github.com/zyedidia/micro/internal/util"
+       "github.com/zyedidia/micro/v2/internal/config"
+       "github.com/zyedidia/micro/v2/internal/screen"
+       "github.com/zyedidia/micro/v2/internal/util"
        "golang.org/x/text/encoding"
 )
 
@@ -15,7 +17,9 @@ const backupMsg = `A backup was detected for this file. This likely means that m
 crashed while editing this file, or another instance of micro is currently
 editing this file.
 
-The backup was created at %s.
+The backup was created on %s, and the file is
+
+%s
 
 * 'recover' will apply the backup as unsaved changes to the current buffer.
   When the buffer is closed, the backup will be removed.
@@ -26,29 +30,25 @@ Options: [r]ecover, [i]gnore: `
 
 // Backup saves the current buffer to ConfigDir/backups
 func (b *Buffer) Backup(checkTime bool) error {
-       if !b.Settings["backup"].(bool) || b.Path == "" {
+       if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
                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)
+               if sub < time.Duration(backupTime)*time.Millisecond {
                        return nil
                }
        }
 
        b.lastbackup = time.Now()
 
-       backupdir := config.ConfigDir + "/backups/"
+       backupdir := filepath.Join(config.ConfigDir, "backups")
        if _, err := os.Stat(backupdir); os.IsNotExist(err) {
                os.Mkdir(backupdir, os.ModePerm)
-               log.Println("Creating backup dir")
        }
 
-       name := backupdir + util.EscapePath(b.AbsPath)
-
-       log.Println("Backing up to", name)
+       name := filepath.Join(backupdir, util.EscapePath(b.AbsPath))
 
        err := overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
                if len(b.lines) == 0 {
@@ -72,21 +72,45 @@ func (b *Buffer) Backup(checkTime bool) error {
                        }
                }
                return
-       })
+       }, false)
 
        return err
 }
 
 // RemoveBackup removes any backup file associated with this buffer
 func (b *Buffer) RemoveBackup() {
-       if !b.Settings["backup"].(bool) || b.Path == "" {
+       if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
                return
        }
-       f := config.ConfigDir + "/backups/" + util.EscapePath(b.AbsPath)
+       f := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
        os.Remove(f)
 }
 
 // ApplyBackup applies the corresponding backup file to this buffer (if one exists)
-func (b *Buffer) ApplyBackup() error {
-       return nil
+// Returns true if a backup was applied
+func (b *Buffer) ApplyBackup(fsize int64) bool {
+       if b.Settings["backup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
+               backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
+               if info, err := os.Stat(backupfile); err == nil {
+                       backup, err := os.Open(backupfile)
+                       if err == nil {
+                               defer backup.Close()
+                               t := info.ModTime()
+                               msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), util.EscapePath(b.AbsPath))
+                               choice := screen.TermPrompt(msg, []string{"r", "i", "recover", "ignore"}, true)
+
+                               if choice%2 == 0 {
+                                       // recover
+                                       b.LineArray = NewLineArray(uint64(fsize), FFAuto, backup)
+                                       b.isModified = true
+                                       return true
+                               } else if choice%2 == 1 {
+                                       // delete
+                                       os.Remove(backupfile)
+                               }
+                       }
+               }
+       }
+
+       return false
 }