micro: Provide recovery of settings.json & bindings.json

This commit is contained in:
Jöran Karl 2024-09-08 12:42:18 +02:00
parent e15bb88270
commit c4dcef3e66
2 changed files with 44 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
@ -222,6 +223,35 @@ func LoadInput(args []string) []*buffer.Buffer {
return buffers return buffers
} }
func checkBackup(name string) error {
target := filepath.Join(config.ConfigDir, name)
backup := util.AppendBackupSuffix(target)
if info, err := os.Stat(backup); err == nil {
input, err := os.ReadFile(backup)
if err == nil {
t := info.ModTime()
msg := fmt.Sprintf(buffer.BackupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backup)
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
if choice%3 == 0 {
// recover
err := os.WriteFile(target, input, util.FileMode)
if err != nil {
return err
}
return os.Remove(backup)
} else if choice%3 == 1 {
// delete
return os.Remove(backup)
} else if choice%3 == 2 {
// abort
return errors.New("Aborted")
}
}
}
return nil
}
func exit(rc int) { func exit(rc int) {
for _, b := range buffer.OpenBuffers { for _, b := range buffer.OpenBuffers {
if !b.Modified() { if !b.Modified() {
@ -269,6 +299,12 @@ func main() {
config.InitRuntimeFiles(true) config.InitRuntimeFiles(true)
config.InitPlugins() config.InitPlugins()
err = checkBackup("settings.json")
if err != nil {
screen.TermMessage(err)
exit(1)
}
err = config.ReadSettings() err = config.ReadSettings()
if err != nil { if err != nil {
screen.TermMessage(err) screen.TermMessage(err)
@ -329,6 +365,12 @@ func main() {
screen.TermMessage(err) screen.TermMessage(err)
} }
err = checkBackup("bindings.json")
if err != nil {
screen.TermMessage(err)
exit(1)
}
action.InitBindings() action.InitBindings()
action.InitCommands() action.InitCommands()

View File

@ -14,7 +14,7 @@ import (
"github.com/zyedidia/micro/v2/internal/util" "github.com/zyedidia/micro/v2/internal/util"
) )
const backupMsg = `A backup was detected for this file. This likely means that micro const BackupMsg = `A backup was detected for this file. This likely means that micro
crashed while editing this file, or another instance of micro is currently crashed while editing this file, or another instance of micro is currently
editing this file. editing this file.
@ -131,7 +131,7 @@ func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
if err == nil { if err == nil {
defer backup.Close() defer backup.Close()
t := info.ModTime() t := info.ModTime()
msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backupfile) msg := fmt.Sprintf(BackupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backupfile)
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true) choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
if choice%3 == 0 { if choice%3 == 0 {