Skip to content

Commit b82e1b1

Browse files
authored
Merge pull request #683 from noborus/notify-eof
Add EOF notification feature
2 parents 82139ea + 68c8929 commit b82e1b1

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ func init() {
513513
rootCmd.PersistentFlags().BoolP("hide-other-section", "", false, "hide other section")
514514
_ = viper.BindPFlag("general.HideOtherSection", rootCmd.PersistentFlags().Lookup("hide-other-section"))
515515

516+
rootCmd.PersistentFlags().IntP("notify-eof", "", 0, "notify at the end of the file")
517+
_ = viper.BindPFlag("NotifyEOF", rootCmd.PersistentFlags().Lookup("notify-eof"))
518+
rootCmd.PersistentFlags().Lookup("notify-eof").NoOptDefVal = "1"
519+
516520
// Config
517521
rootCmd.PersistentFlags().BoolP("quit-if-one-screen", "F", false, "quit if the output fits on one screen")
518522
_ = viper.BindPFlag("QuitSmall", rootCmd.PersistentFlags().Lookup("quit-if-one-screen"))

oviewer/draw.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"strings"
8+
"time"
89

910
"github.com/gdamore/tcell/v2"
1011
)
@@ -389,3 +390,29 @@ func (root *Root) reverseRange(y int, start int, end int, sel bool) {
389390
}
390391
}
391392
}
393+
394+
// notifyEOFReached notifies that EOF has been reached.
395+
func (root *Root) notifyEOFReached(m *Document) {
396+
root.setMessagef("EOF reached %s", m.FileName)
397+
root.notify(root.Config.NotifyEOF)
398+
}
399+
400+
// notify notifies by beeping and flashing the screen the specified number of times.
401+
func (root *Root) notify(count int) {
402+
for i := 0; i < count; i++ {
403+
root.Screen.Beep()
404+
root.flash()
405+
}
406+
}
407+
408+
// flash flashes the screen.
409+
func (root *Root) flash() {
410+
root.Screen.Fill(' ', tcell.StyleDefault.Background(tcell.ColorWhite))
411+
root.Screen.Sync()
412+
time.Sleep(50 * time.Millisecond)
413+
root.Screen.Fill(' ', tcell.StyleDefault.Background(tcell.ColorBlack))
414+
root.Screen.Sync()
415+
time.Sleep(50 * time.Millisecond)
416+
root.draw(context.Background())
417+
time.Sleep(100 * time.Millisecond)
418+
}

oviewer/event.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func (root *Root) event(ctx context.Context, ev tcell.Event) bool {
9898
root.saveBuffer(ev.value)
9999
case *eventSectionNum:
100100
root.setSectionNum(ev.value)
101+
case *eventReachEOF:
102+
root.notifyEOFReached(ev.m)
101103

102104
// tcell events
103105
case *tcell.EventResize:
@@ -361,3 +363,36 @@ func (root *Root) postEvent(ev tcell.Event) {
361363
root.releaseEventBuffer()
362364
}
363365
}
366+
367+
// eventReachEOF represents an EOF event.
368+
type eventReachEOF struct {
369+
m *Document
370+
tcell.EventTime
371+
}
372+
373+
// sendReachEOF fires the eventReachEOF event.
374+
func (root *Root) sendReachEOF(m *Document) {
375+
ev := &eventReachEOF{}
376+
ev.m = m
377+
ev.SetEventNow()
378+
root.postEvent(ev)
379+
}
380+
381+
// monitorEOF monitors the EOF of the document.
382+
func (root *Root) monitorEOF() {
383+
if root.Config.NotifyEOF == 0 {
384+
return
385+
}
386+
log.Println("monitorEOF")
387+
for _, m := range root.DocList {
388+
go func() {
389+
if !m.BufEOF() {
390+
m.cond.L.Lock()
391+
m.cond.Wait()
392+
m.cond.L.Unlock()
393+
}
394+
root.sendReachEOF(m)
395+
log.Println("EOF reached", m.FileName)
396+
}()
397+
}
398+
}

oviewer/oviewer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ type Config struct {
274274
RegexpSearch bool
275275
// Incsearch is incremental search if true.
276276
Incsearch bool
277+
// NotifyEOF specifies the number of times to notify EOF.
278+
NotifyEOF int
277279

278280
// ShrinkChar is the character to display when the column is shrunk.
279281
ShrinkChar string
@@ -641,6 +643,8 @@ func (root *Root) Run() error {
641643
sigSuspend := registerSIGTSTP()
642644
quitChan := make(chan struct{})
643645

646+
root.monitorEOF()
647+
644648
go func() {
645649
// Undo screen when goroutine panic.
646650
defer func() {

0 commit comments

Comments
 (0)