load console pipe content to buffer (async version for progressive rendering)
(stdin io.Reader, b *Buffer, updateChan chan<- bool, doneChan chan<- error)
| 441 | |
| 442 | // load console pipe content to buffer (async version for progressive rendering) |
| 443 | func loadPipeToBufferAsync(stdin io.Reader, b *Buffer, updateChan chan<- bool, doneChan chan<- error) { |
| 444 | totalAddedLN := 0 //the number of lines has been added into buffer |
| 445 | var err error |
| 446 | |
| 447 | // For pipes, we don't know the total size |
| 448 | loadProgress.TotalBytes = 0 |
| 449 | loadProgress.LoadedBytes = 0 |
| 450 | loadProgress.IsComplete = false |
| 451 | |
| 452 | // Create progress tracker (disabled for async loading) |
| 453 | progress := newProgressTracker(0, false) |
| 454 | |
| 455 | scanner := bufio.NewScanner(stdin) |
| 456 | //increase buffer size for large files and long lines |
| 457 | const maxScanTokenSize = 1024 * 1024 |
| 458 | buf := make([]byte, maxScanTokenSize) |
| 459 | scanner.Buffer(buf, maxScanTokenSize) |
| 460 | //read 10 lines to detect separator |
| 461 | lineNumber := 10 |
| 462 | var detectLines []string //lines as detect separator data |
| 463 | if b.sep == 0 { |
| 464 | for scanner.Scan() { |
| 465 | line := scanner.Text() |
| 466 | //skip empty line |
| 467 | if line == "\n" { |
| 468 | continue |
| 469 | } |
| 470 | //ignore first n lines |
| 471 | if args.SkipNum > 0 { |
| 472 | args.SkipNum-- |
| 473 | continue |
| 474 | } |
| 475 | //ignore line with specified prefix |
| 476 | if skipLine(line, args.SkipSymbol) { |
| 477 | continue |
| 478 | } |
| 479 | detectLines = append(detectLines, line) |
| 480 | if len(detectLines) >= lineNumber { |
| 481 | break |
| 482 | } |
| 483 | } |
| 484 | sd := sepDetecor{} |
| 485 | b.sep = sd.sepDetect(detectLines) |
| 486 | } |
| 487 | //check final separator |
| 488 | if b.sep == 0 { |
| 489 | doneChan <- errors.New("tv can't identify separator, you need to set it manual") |
| 490 | return |
| 491 | } |
| 492 | |
| 493 | //add detectLines to buffer |
| 494 | for _, line := range detectLines { |
| 495 | //parse and add line to buffer |
| 496 | err = addDRToBuffer(b, line, args.ShowNum, args.HideNum) |
| 497 | if err != nil { |
| 498 | progress.finish() |
| 499 | doneChan <- err |
| 500 | return |