Change default scan buffer size to 256kb

...and make it configurable
This commit is contained in:
Daisuke Maki 2017-03-03 14:21:38 +09:00
parent a78051dcbe
commit 482fa7fc6d
4 changed files with 39 additions and 4 deletions

View file

@ -251,6 +251,18 @@ Default value for StickySelection is false.
OnCancel is equivalent to `--on-cancel` command line option.
### MaxScanBufferSize
```json
{
"MaxScanBufferSize": 256
}
```
Controls the buffer sized used by `bufio.Scanner`, which is responsible for
reading the input lines. If you believe that your input has very long lines
that prohibit peco from reading them, try increasing this number
## Keymaps
Example:

View file

@ -78,6 +78,7 @@ type Peco struct {
keymap Keymap
layoutType string
location Location
maxScanBufferSize int
mutex sync.Mutex
onCancel string
prompt string
@ -167,9 +168,9 @@ type Screen interface {
// Termbox just hands out the processing to the termbox library
type Termbox struct {
mutex sync.Mutex
resumeCh chan(struct{})
suspendCh chan(struct{})
mutex sync.Mutex
resumeCh chan (struct{})
suspendCh chan (struct{})
}
// View handles the drawing/updating the screen
@ -299,6 +300,7 @@ type Config struct {
Command []CommandConfig
QueryExecutionDelay int
StickySelection bool
MaxScanBufferSize int
// If this is true, then the prefix for single key jump mode
// is displayed by default.

View file

@ -503,6 +503,11 @@ func (p *Peco) ApplyConfig(opts CLIOptions) error {
}
}
p.maxScanBufferSize = 256
if v := p.config.MaxScanBufferSize; v > 0 {
p.maxScanBufferSize = v
}
p.enableSep = opts.OptEnableNullSep
if i := opts.OptInitialIndex; i >= 0 {

View file

@ -77,7 +77,12 @@ func (s *Source) Setup(ctx context.Context, state *Peco) {
// Note: this will be a no-op if notify.Do has been called before
defer notify.Do(notifycb)
if pdebug.Enabled {
pdebug.Printf("Source: using buffer size of %dkb", state.maxScanBufferSize)
}
scanbuf := make([]byte, state.maxScanBufferSize*1024)
scanner := bufio.NewScanner(s.in)
scanner.Buffer(scanbuf, 0)
defer func() {
if util.IsTty(s.in) {
return
@ -95,7 +100,18 @@ func (s *Source) Setup(ctx context.Context, state *Peco) {
}
defer close(lines)
for scanner.Scan() {
for loop := true; loop; {
if !scanner.Scan() {
switch err := scanner.Err(); err {
case nil: // if error was io.EOF, returns nil
loop = false
default:
if pdebug.Enabled {
pdebug.Printf("err: %s", err)
}
}
continue
}
lines <- scanner.Text()
scanned++
}