From b60c7f107ce9e8cb80cc915ceedd7ca9dc61cb8d Mon Sep 17 00:00:00 2001 From: Dmitriy Olshevskiy Date: Thu, 15 Nov 2018 14:28:50 +0300 Subject: [PATCH] output filtered lines if can fit display and "filters" specified --- cmd/slit/main.go | 22 ++++++++++++++-------- slit.go | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cmd/slit/main.go b/cmd/slit/main.go index 13b1607..9fd0e99 100644 --- a/cmd/slit/main.go +++ b/cmd/slit/main.go @@ -78,16 +78,16 @@ func main() { defer s.Shutdown() - if !alwaysTermMode && tryDirectOutputIfShort(s, ctx, waitForShortStdin) { - return - } - if filtersOpt != "" { initFilters, err := filters.ParseFiltersOpt(filtersOpt) exitOnErr(err) s.SetFilters(initFilters) } + if !alwaysTermMode && tryDirectOutputIfShort(s, ctx, waitForShortStdin) { + return + } + s.SetOutPath(outPath) // TODO: This is not really used right now, NewFromStdin uses config before it is set here // Probably should pass config to all slit constructors, with sane defaults s.SetFollow(follow) @@ -99,10 +99,16 @@ func main() { func tryDirectOutputIfShort(s *slit.Slit, ctx context.Context, durationMs int) bool { localCtx, cancel := context.WithTimeout(ctx, time.Duration(durationMs)*time.Millisecond) defer cancel() - if s.CanFitDisplay(localCtx) { - file := s.GetFile() - file.Seek(0, io.SeekStart) - outputToStdout(file) + if ok, lines := s.CanFitDisplay(localCtx); ok { + if len(s.GetFilters()) > 0 { + for _, line := range lines { + fmt.Fprintln(os.Stdout, string(line.Str.Runes)) + } + } else { + file := s.GetFile() + file.Seek(0, io.SeekStart) + outputToStdout(file) + } return true } return false diff --git a/slit.go b/slit.go index e80e697..9f25cff 100644 --- a/slit.go +++ b/slit.go @@ -81,6 +81,9 @@ func (s *Slit) SetKeepChars(i int) { config.keepChars = i } // Set initial filters func (s *Slit) SetFilters(f []*filters.Filter) { config.initFilters = f } +// Get filters +func (s *Slit) GetFilters() []*filters.Filter { return config.initFilters } + // Invoke the Slit UI func (s *Slit) Init() { s.fetcher = newFetcher(s.file, s.ctx) @@ -255,13 +258,14 @@ func mkCacheFile() (f *os.File, err error) { return f, err } -func (s *Slit) CanFitDisplay(ctx context.Context) bool { +func (s *Slit) CanFitDisplay(ctx context.Context) (bool, []Line) { s.Init() termbox.Init() w, h := termbox.Size() termbox.Close() localCtx, cancel := context.WithCancel(ctx) parsedLineCount := 0 + linesOut := make([]Line, 0, h) lines := s.fetcher.Get(localCtx, Pos{}) defer func() { for range lines { @@ -272,13 +276,13 @@ FORLOOP: for { select { case <-ctx.Done(): - return false + return false, linesOut case line, isOpen := <-lines: if !isOpen { if config.stdin && !config.isStdinRead() { select { case <-ctx.Done(): - return false + return false, linesOut case <-time.After(10 * time.Millisecond): lines = s.fetcher.Get(localCtx, line.Pos) continue FORLOOP @@ -286,6 +290,7 @@ FORLOOP: } break FORLOOP } + linesOut = append(linesOut, line) lineLen := len(line.Str.Runes) if lineLen > 0 { parsedLineCount += lineLen / w @@ -297,12 +302,9 @@ FORLOOP: parsedLineCount += 1 } if parsedLineCount > h { - return false + return false, linesOut } } } - if parsedLineCount < h { - return true - } - return false + return parsedLineCount < h, linesOut }