load file content to buffer (async version with concurrent parsing)
(fn string, b *Buffer, updateChan chan<- bool, doneChan chan<- error)
| 100 | |
| 101 | // load file content to buffer (async version with concurrent parsing) |
| 102 | func loadFileToBufferAsync(fn string, b *Buffer, updateChan chan<- bool, doneChan chan<- error) { |
| 103 | totalAddedLN := 0 //the number of lines has been added into buffer |
| 104 | |
| 105 | // Get file size for progress tracking |
| 106 | fileInfo, err := os.Stat(fn) |
| 107 | if err != nil { |
| 108 | doneChan <- err |
| 109 | return |
| 110 | } |
| 111 | var fileSize int64 |
| 112 | if !fileInfo.IsDir() { |
| 113 | fileSize = fileInfo.Size() |
| 114 | } |
| 115 | |
| 116 | // Initialize load progress |
| 117 | loadProgress.TotalBytes = fileSize |
| 118 | loadProgress.LoadedBytes = 0 |
| 119 | loadProgress.IsComplete = false |
| 120 | |
| 121 | // Create progress tracker (disabled for async loading since UI will show it) |
| 122 | progress := newProgressTracker(fileSize, false) |
| 123 | |
| 124 | scanner, err := getFileScanner(fn) |
| 125 | if err != nil { |
| 126 | doneChan <- err |
| 127 | return |
| 128 | } |
| 129 | scanner.Split(bufio.ScanLines) |
| 130 | //set separator, if user does not provide it. |
| 131 | var detectLines []string //lines as detect separator data |
| 132 | if b.sep == 0 { |
| 133 | //read 10 lines to detect separator |
| 134 | lineNumber := 10 |
| 135 | for scanner.Scan() { |
| 136 | line := scanner.Text() |
| 137 | //skip empty line |
| 138 | if line == "\n" { |
| 139 | continue |
| 140 | } |
| 141 | //ignore first n lines |
| 142 | if args.SkipNum > 0 { |
| 143 | args.SkipNum-- |
| 144 | continue |
| 145 | } |
| 146 | //ignore line with specified prefix |
| 147 | if skipLine(line, args.SkipSymbol) { |
| 148 | continue |
| 149 | } |
| 150 | detectLines = append(detectLines, line) |
| 151 | if len(detectLines) >= lineNumber { |
| 152 | break |
| 153 | } |
| 154 | } |
| 155 | //if the suffix of file name is ".csv", set separator to ",". |
| 156 | //if the suffix of file name is "tsv", set separator to "\t". |
| 157 | if strings.HasSuffix(fn, ".csv") { |
| 158 | b.sep = ',' |
| 159 | } else if strings.HasSuffix(fn, ".tsv") { |