load console pipe content to buffer (synchronous version)
(stdin io.Reader, b *Buffer)
| 632 | |
| 633 | // load console pipe content to buffer (synchronous version) |
| 634 | func loadPipeToBuffer(stdin io.Reader, b *Buffer) error { |
| 635 | totalAddedLN := 0 //the number of lines has been added into buffer |
| 636 | var err error |
| 637 | |
| 638 | // Create progress tracker (no file size for pipes) |
| 639 | progress := newProgressTracker(0, true) |
| 640 | |
| 641 | scanner := bufio.NewScanner(stdin) |
| 642 | //increase buffer size for large files and long lines |
| 643 | const maxScanTokenSize = 1024 * 1024 |
| 644 | buf := make([]byte, maxScanTokenSize) |
| 645 | scanner.Buffer(buf, maxScanTokenSize) |
| 646 | //read 10 lines to detect separator |
| 647 | lineNumber := 10 |
| 648 | var detectLines []string //lines as detect separator data |
| 649 | if b.sep == 0 { |
| 650 | for scanner.Scan() { |
| 651 | line := scanner.Text() |
| 652 | //skip empty line |
| 653 | if line == "\n" { |
| 654 | continue |
| 655 | } |
| 656 | //ignore first n lines |
| 657 | if args.SkipNum > 0 { |
| 658 | args.SkipNum-- |
| 659 | continue |
| 660 | } |
| 661 | //ignore line with specified prefix |
| 662 | if skipLine(line, args.SkipSymbol) { |
| 663 | continue |
| 664 | } |
| 665 | detectLines = append(detectLines, line) |
| 666 | if len(detectLines) >= lineNumber { |
| 667 | break |
| 668 | } |
| 669 | } |
| 670 | sd := sepDetecor{} |
| 671 | b.sep = sd.sepDetect(detectLines) |
| 672 | } |
| 673 | //check final separator |
| 674 | if b.sep == 0 { |
| 675 | fatalError(errors.New("tv can't identify separator, you need to set it manual")) |
| 676 | } |
| 677 | |
| 678 | //add detectLines to buffer |
| 679 | for _, line := range detectLines { |
| 680 | //parse and add line to buffer |
| 681 | err = addDRToBuffer(b, line, args.ShowNum, args.HideNum) |
| 682 | if err != nil { |
| 683 | progress.finish() |
| 684 | return err |
| 685 | } |
| 686 | totalAddedLN++ |
| 687 | progress.increment(int64(len(line) + 1)) |
| 688 | if totalAddedLN >= args.NLine && args.NLine > 0 { |
| 689 | break |
| 690 | } |
| 691 | } |