Skip to content

Commit

Permalink
Merge pull request #434 from noborus/improved-export2
Browse files Browse the repository at this point in the history
Export changed getting line
  • Loading branch information
noborus authored Aug 20, 2023
2 parents dccf4f1 + 7389f65 commit 2b48a38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,18 @@ func (m *Document) CurrentLN() int {
// Export exports the document in the specified range.
func (m *Document) Export(w io.Writer, start int, end int) {
end = min(end, m.BufEndNum()-1)
for n := start; n <= end; n++ {
fmt.Fprintln(w, m.GetLine(n))
startChunk, startCn := chunkLineNum(start)
endChunk, endCn := chunkLineNum(end)

scn := startCn
ecn := ChunkSize
for chunkNum := startChunk; chunkNum <= endChunk; chunkNum++ {
if chunkNum == endChunk {
ecn = endCn + 1
}
chunk := m.store.chunks[chunkNum]
m.store.export(w, chunk, scn, ecn)
scn = 0
}
}

Expand Down
18 changes: 18 additions & 0 deletions oviewer/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,21 @@ func (s *store) appendFormFeed(chunk *chunk) {
s.appendLine(chunk, []byte(feed))
}
}

func (s *store) export(w io.Writer, chunk *chunk, start int, end int) error {
s.mu.RLock()
defer s.mu.RUnlock()

if start < 0 {
start = 0
}
if end > len(chunk.lines) {
end = len(chunk.lines)
}
for i := start; i < end; i++ {
if _, err := w.Write(chunk.lines[i]); err != nil {
return err
}
}
return nil
}

0 comments on commit 2b48a38

Please sign in to comment.