mirror of
https://github.com/zyedidia/micro.git
synced 2025-06-18 23:05:40 -04:00
Allow aborting while opening a file with backup
Also fixes an issue where the abort prompt consumes interrupt signals. Fixes #2151
This commit is contained in:
parent
33e064b3b9
commit
c315a91fc6
@ -274,12 +274,6 @@ func main() {
|
|||||||
fmt.Println("Fatal: Micro could not initialize a Screen.")
|
fmt.Println("Fatal: Micro could not initialize a Screen.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
sigterm = make(chan os.Signal, 1)
|
|
||||||
sighup = make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
|
|
||||||
signal.Notify(sighup, syscall.SIGHUP)
|
|
||||||
|
|
||||||
m := clipboard.SetMethod(config.GetGlobalOption("clipboard").(string))
|
m := clipboard.SetMethod(config.GetGlobalOption("clipboard").(string))
|
||||||
clipErr := clipboard.Initialize(m)
|
clipErr := clipboard.Initialize(m)
|
||||||
|
|
||||||
@ -353,6 +347,11 @@ func main() {
|
|||||||
|
|
||||||
screen.Events = make(chan tcell.Event)
|
screen.Events = make(chan tcell.Event)
|
||||||
|
|
||||||
|
sigterm = make(chan os.Signal, 1)
|
||||||
|
sighup = make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
|
||||||
|
signal.Notify(sighup, syscall.SIGHUP)
|
||||||
|
|
||||||
// Here is the event loop which runs in a separate thread
|
// Here is the event loop which runs in a separate thread
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -26,8 +26,9 @@ The backup was created on %s, and the file is
|
|||||||
When the buffer is closed, the backup will be removed.
|
When the buffer is closed, the backup will be removed.
|
||||||
* 'ignore' will ignore the backup, discarding its changes. The backup file
|
* 'ignore' will ignore the backup, discarding its changes. The backup file
|
||||||
will be removed.
|
will be removed.
|
||||||
|
* 'abort' will abort the open operation, and instead open an empty buffer.
|
||||||
|
|
||||||
Options: [r]ecover, [i]gnore: `
|
Options: [r]ecover, [i]gnore, [a]bort: `
|
||||||
|
|
||||||
var backupRequestChan chan *Buffer
|
var backupRequestChan chan *Buffer
|
||||||
|
|
||||||
@ -118,7 +119,7 @@ func (b *Buffer) RemoveBackup() {
|
|||||||
|
|
||||||
// ApplyBackup applies the corresponding backup file to this buffer (if one exists)
|
// ApplyBackup applies the corresponding backup file to this buffer (if one exists)
|
||||||
// Returns true if a backup was applied
|
// Returns true if a backup was applied
|
||||||
func (b *Buffer) ApplyBackup(fsize int64) bool {
|
func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
|
||||||
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
|
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
|
||||||
backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
|
backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
|
||||||
if info, err := os.Stat(backupfile); err == nil {
|
if info, err := os.Stat(backupfile); err == nil {
|
||||||
@ -127,20 +128,22 @@ func (b *Buffer) ApplyBackup(fsize int64) bool {
|
|||||||
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"), util.EscapePath(b.AbsPath))
|
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)
|
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
|
||||||
|
|
||||||
if choice%2 == 0 {
|
if choice%3 == 0 {
|
||||||
// recover
|
// recover
|
||||||
b.LineArray = NewLineArray(uint64(fsize), FFAuto, backup)
|
b.LineArray = NewLineArray(uint64(fsize), FFAuto, backup)
|
||||||
b.isModified = true
|
b.isModified = true
|
||||||
return true
|
return true, true
|
||||||
} else if choice%2 == 1 {
|
} else if choice%3 == 1 {
|
||||||
// delete
|
// delete
|
||||||
os.Remove(backupfile)
|
os.Remove(backupfile)
|
||||||
|
} else if choice%3 == 2 {
|
||||||
|
return false, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false, true
|
||||||
}
|
}
|
||||||
|
@ -250,6 +250,9 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
|
|||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
buf = NewBuffer(file, util.FSize(file), filename, cursorLoc, btype)
|
buf = NewBuffer(file, util.FSize(file), filename, cursorLoc, btype)
|
||||||
|
if buf == nil {
|
||||||
|
return nil, errors.New("could not open file")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if readonly && prompt != nil {
|
if readonly && prompt != nil {
|
||||||
@ -333,8 +336,12 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
|
|||||||
b.Settings["encoding"] = "utf-8"
|
b.Settings["encoding"] = "utf-8"
|
||||||
}
|
}
|
||||||
|
|
||||||
hasBackup = b.ApplyBackup(size)
|
var ok bool
|
||||||
|
hasBackup, ok = b.ApplyBackup(size)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return NewBufferFromString("", "", btype)
|
||||||
|
}
|
||||||
if !hasBackup {
|
if !hasBackup {
|
||||||
reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
|
reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user