Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cmd/slit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions slit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -272,20 +276,21 @@ 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
}
}
break FORLOOP
}
linesOut = append(linesOut, line)
lineLen := len(line.Str.Runes)
if lineLen > 0 {
parsedLineCount += lineLen / w
Expand All @@ -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
}