From 9c0aead4ea2bc607b2a3b40864011d424f728e39 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Thu, 11 Jul 2024 13:13:06 +0900 Subject: [PATCH] Quit when the filter result fits within the screen Changed when using --quit-if-one-screen and --filter together. Quit when the filter result fits within the screen. Delay the determination that it fits within the screen. --- main.go | 7 +++++++ oviewer/event.go | 7 +++++++ oviewer/oviewer.go | 7 +++---- oviewer/oviewer_test.go | 10 ---------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 0639a49..10fbabc 100644 --- a/main.go +++ b/main.go @@ -174,6 +174,13 @@ func RunOviewer(args []string) error { ov.Filter(nonMatchFilter, true) } + if ov.QuitSmall && (filter != "" || nonMatchFilter != "") { + // UpdateInterval(50 * time.Millisecond) * 10 = 500ms. + // Quit if it fits on the screen within 500ms. + ov.QuitSmallCount = 10 + ov.QuitSmall = false + } + if err := ov.Run(); err != nil { return err } diff --git a/oviewer/event.go b/oviewer/event.go index 03e44dc..803b781 100644 --- a/oviewer/event.go +++ b/oviewer/event.go @@ -157,6 +157,13 @@ func (root *Root) everyUpdate(ctx context.Context) { if atomic.SwapInt32(&root.Doc.watchRestart, 0) == 1 { root.watchControl() } + + if root.QuitSmallCount > 0 { + root.QuitSmallCount-- + if root.Doc.documentType == DocFilter && root.docSmall() { + root.WriteQuit(ctx) + } + } } // keyEvent processes key events. diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index 7d5569d..594b838 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -261,6 +261,8 @@ type Config struct { IsWriteOriginal bool // QuitSmall Quit if the output fits on one screen. QuitSmall bool + // QuitSmallCount is the number of times to check if the output fits on one screen. + QuitSmallCount int // CaseSensitive is case-sensitive if true. CaseSensitive bool // SmartCaseSensitive is lowercase search ignores case, if true. @@ -665,7 +667,7 @@ func (root *Root) Run() error { root.ViewSync(ctx) // Exit if fits on screen - if root.QuitSmall && root.docSmall() { + if root.QuitSmall && root.DocumentLen() == 1 && root.docSmall() { root.IsWriteOriginal = true return nil } @@ -929,9 +931,6 @@ func mergeGeneral(src general, dst general) general { // docSmall returns with bool whether the file to display fits on the screen. func (root *Root) docSmall() bool { - if len(root.DocList) > 1 { - return false - } root.prepareScreen() m := root.Doc if !m.BufEOF() { diff --git a/oviewer/oviewer_test.go b/oviewer/oviewer_test.go index 4b1fecc..8bae397 100644 --- a/oviewer/oviewer_test.go +++ b/oviewer/oviewer_test.go @@ -451,16 +451,6 @@ func TestRoot_docSmall(t *testing.T) { }, want: false, }, - { - name: "test multiple", - args: args{ - fileNames: []string{ - filepath.Join(testdata, "test.txt"), - filepath.Join(testdata, "test2.txt"), - }, - }, - want: false, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {