github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/decent/lib/datapager.go (about)

     1  // See: https://github.com/attic-labs/noms/issues/3808
     2  // +build ignore
     3  
     4  // Copyright 2017 Attic Labs, Inc. All rights reserved.
     5  // Licensed under the Apache License, version 2.0:
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  
     8  package lib
     9  
    10  import (
    11  	"fmt"
    12  	"strings"
    13  
    14  	"github.com/attic-labs/noms/go/datas"
    15  	"github.com/attic-labs/noms/go/marshal"
    16  	"github.com/attic-labs/noms/go/types"
    17  )
    18  
    19  type dataPager struct {
    20  	dataset    datas.Dataset
    21  	msgKeyChan chan types.String
    22  	doneChan   chan struct{}
    23  	msgMap     types.Map
    24  	terms      []string
    25  }
    26  
    27  func NewDataPager(ds datas.Dataset, mkChan chan types.String, doneChan chan struct{}, msgs types.Map, terms []string) *dataPager {
    28  	return &dataPager{
    29  		dataset:    ds,
    30  		msgKeyChan: mkChan,
    31  		doneChan:   doneChan,
    32  		msgMap:     msgs,
    33  		terms:      terms,
    34  	}
    35  }
    36  
    37  func (dp *dataPager) Close() {
    38  	dp.doneChan <- struct{}{}
    39  }
    40  
    41  func (dp *dataPager) Next() (string, bool) {
    42  	msgKey := <-dp.msgKeyChan
    43  	if msgKey == "" {
    44  		return "", false
    45  	}
    46  	nm := dp.msgMap.Get(msgKey)
    47  
    48  	var m Message
    49  	err := marshal.Unmarshal(nm, &m)
    50  	if err != nil {
    51  		return fmt.Sprintf("ERROR: %s", err.Error()), true
    52  	}
    53  
    54  	s1 := fmt.Sprintf("%s: %s", m.Author, m.Body)
    55  	s2 := highlightTerms(s1, dp.terms)
    56  	return s2, true
    57  }
    58  
    59  func (dp *dataPager) Prepend(lines []string, target int) ([]string, bool) {
    60  	new := []string{}
    61  	m, ok := dp.Next()
    62  	if !ok {
    63  		return lines, false
    64  	}
    65  	for ; ok && len(new) < target; m, ok = dp.Next() {
    66  		new1 := strings.Split(m, "\n")
    67  		new = append(new1, new...)
    68  	}
    69  	return append(new, lines...), true
    70  }