Skip to content

Commit

Permalink
Fix an issue with "clear on quit" when displaying images
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jan 15, 2025
1 parent efc5410 commit f63fa42
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
10 changes: 4 additions & 6 deletions v2/cmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,14 @@ func (e *Editor) CommandMenu(c *vt100.Canvas, tty *vt100.TTY, status *StatusBar,
if firstWordContainsOneOf(parentCommand(), searchProcessNames) {
actions.Add("Kill parent and exit without saving", func() {
e.stopParentOnQuit = true
e.clearOnQuit = true
e.quit = true // indicate that the user wishes to quit
e.clearOnQuit = true // clear the terminal after quitting
e.quit = true // indicate that the user wishes to quit
e.clearOnQuit.Store(true) // clear the terminal after quitting
})
} else {
actions.Add("Exit without saving", func() {
e.stopParentOnQuit = false
e.clearOnQuit = false
e.quit = true // indicate that the user wishes to quit
e.clearOnQuit = true // clear the terminal after quitting
e.quit = true // indicate that the user wishes to quit
e.clearOnQuit.Store(true) // clear the terminal after quitting
})
}

Expand Down
2 changes: 1 addition & 1 deletion v2/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (e *Editor) CommandToFunction(c *vt100.Canvas, tty *vt100.TTY, status *Stat
savequitclear: func() { // save and quit, then clear the screen
e.UserSave(c, tty, status)
e.quit = true
e.clearOnQuit = true
e.clearOnQuit.Store(true)
},
sortblock: func() { // sort the current block of lines, until the next blank line or EOF
undo.Snapshot(e)
Expand Down
2 changes: 1 addition & 1 deletion v2/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type Editor struct {
expandTags bool // can be used for XML and HTML
syntaxHighlight bool // syntax highlighting
stopParentOnQuit bool // send SIGQUIT to the parent PID when quitting
clearOnQuit bool // clear the terminal when quitting the editor, or not
quit bool // for indicating if the user wants to end the editor session
readOnly bool // is the file read-only when initializing o?
debugHideOutput bool // hide the GDB stdout pane when in debug mode?
Expand All @@ -71,6 +70,7 @@ type Editor struct {
highlightCurrentLine bool // highlight the current line
highlightCurrentText bool // highlight the current text (not the entire line)
// atomic.Bool are used for values that might be read when redrawing text asynchronously
clearOnQuit atomic.Bool // clear the terminal when quitting the editor, or not
changed atomic.Bool // has the contents changed, since last save?
redraw atomic.Bool // if the contents should be redrawn in the next loop
redrawCursor atomic.Bool // if the cursor should be moved to the location it is supposed to be
Expand Down
17 changes: 10 additions & 7 deletions v2/keyloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
e, messageAfterRedraw, displayedImage, err := NewEditor(tty, c, fnord, lineNumber, colNumber, theme, syntaxHighlight, true, monitorAndReadOnly, nanoMode, createDirectoriesIfMissing, displayQuickHelp)
if err != nil {
if e != nil {
return "", false, e.clearOnQuit, err
return "", false, e.clearOnQuit.Load(), err
}
return "", false, false, err
} else if displayedImage {
// A special case for if an image was displayed instead of a file being opened
return "", false, e.clearOnQuit, nil
if e != nil {
return "", false, e.clearOnQuit.Load(), nil
}
return "", false, false, nil
}

// Find the absolute path to this filename
Expand Down Expand Up @@ -181,7 +184,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
} else {
// Lock the current file, if it's not already locked
if err := fileLock.Lock(absFilename); err != nil {
return fmt.Sprintf("Locked by another (possibly dead) instance of this editor.\nTry: o -f %s", filepath.Base(absFilename)), false, e.clearOnQuit, errors.New(absFilename + " is locked")
return fmt.Sprintf("Locked by another (possibly dead) instance of this editor.\nTry: o -f %s", filepath.Base(absFilename)), false, e.clearOnQuit.Load(), errors.New(absFilename + " is locked")
}
// Immediately save the lock file as a signal to other instances of the editor
fileLock.Save()
Expand Down Expand Up @@ -1052,7 +1055,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
e.blockMode = false
// If o is used as a man page viewer, exit at the press of esc
if e.mode == mode.ManPage {
e.clearOnQuit = false
e.clearOnQuit.Store(false)
e.quit = true
break
}
Expand Down Expand Up @@ -2097,7 +2100,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
break
} else if e.mode == mode.ManPage && keyRunes[0] == 'q' {
// If o is used as a man page viewer, exit at the press of "q"
e.clearOnQuit = false
e.clearOnQuit.Store(false)
e.quit = true
break
}
Expand Down Expand Up @@ -2256,7 +2259,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
e.CloseLocksAndLocationHistory(canUseLocks, absFilename, lockTimestamp, forceFlag, &closeLocksWaitGroup)

// Quit everything that has to do with the terminal
if e.clearOnQuit {
if e.clearOnQuit.Load() {
vt100.Clear()
vt100.Close()
} else {
Expand All @@ -2276,5 +2279,5 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
stopBackgroundProcesses()

// All done
return "", e.stopParentOnQuit, e.clearOnQuit, nil
return "", e.stopParentOnQuit, e.clearOnQuit.Load(), nil
}
6 changes: 3 additions & 3 deletions v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ func main() {
NoTitle()

// Clear the current color attribute
if !clearOnQuit {
fmt.Print("\n" + vt100.Stop())
} else {
if clearOnQuit {
fmt.Print(vt100.Stop())
} else {
fmt.Print("\n" + vt100.Stop())
}

traceComplete() // if building with -tags trace
Expand Down
6 changes: 3 additions & 3 deletions v2/neweditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func NewEditor(tty *vt100.TTY, c *vt100.Canvas, fnord FilenameOrData, lineNumber
// Minor adjustments for some file types
switch e.mode {
case mode.Email, mode.Git, mode.ManPage:
e.clearOnQuit = true
e.clearOnQuit.Store(true)
}

// Set the editor filename
Expand Down Expand Up @@ -370,12 +370,12 @@ func NewEditor(tty *vt100.TTY, c *vt100.Canvas, fnord FilenameOrData, lineNumber
// NetBSD, FreeBSD, OpenBSD or Dragonfly
e.SetTheme(NewRedBlackTheme())
DisableQuickHelpScreen(nil)
e.clearOnQuit = true
e.clearOnQuit.Store(true)
} else if shell := env.Str("SHELL"); shell != "/usr/local/bin/fish" && (shell == "/bin/csh" || shell == "/bin/ksh" || strings.HasPrefix(shell, "/usr/local/bin")) && !inVTEGUI && filepath.Base(os.Args[0]) != "default" {
// This is likely to be FreeBSD or OpenBSD (and the executable/link name is not "default")
e.SetTheme(NewRedBlackTheme())
DisableQuickHelpScreen(nil)
e.clearOnQuit = true
e.clearOnQuit.Store(true)
} else if colorString := env.Str("COLORFGBG"); strings.Contains(colorString, ";") {
fields := strings.Split(colorString, ";")
backgroundColor := fields[len(fields)-1]
Expand Down

0 comments on commit f63fa42

Please sign in to comment.