Skip to content

Commit

Permalink
Extract function that creates internal line struct
Browse files Browse the repository at this point in the history
  • Loading branch information
meyermarcel committed Jun 13, 2024
1 parent 8c8d16e commit e909629
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions annot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Annot struct {
pipeLeadingSpaces []int
}

// line is an internal parallel to a string in Lines.
type line struct {
length int
leadingSpaces int
Expand Down Expand Up @@ -109,23 +110,7 @@ func Write(w io.Writer, annots ...*Annot) error {
})

for _, a := range annots {
if len(a.Lines) == 0 {
a.lines = make([]*line, 1)
a.lines[0] = &line{}
continue
}
a.lines = make([]*line, len(a.Lines))
for i := range a.Lines {
leadingSpaces := a.Col
if i > 0 {
leadingSpaces += 3
}

a.lines[i] = &line{
length: uniseg.StringWidth(a.Lines[i]),
leadingSpaces: leadingSpaces,
}
}
a.createLines()
}

// Start with second last annotation index and decrement.
Expand All @@ -138,6 +123,28 @@ func Write(w io.Writer, annots ...*Annot) error {
return write(w, annots)
}

// createLines creates an array of lines parallel to Lines.
func (a *Annot) createLines() {
if len(a.Lines) == 0 {
a.lines = make([]*line, 1)
a.lines[0] = &line{}
return
}

a.lines = make([]*line, len(a.Lines))
for i := range a.Lines {
leadingSpaces := a.Col
if i > 0 {
leadingSpaces += 3
}

a.lines[i] = &line{
length: uniseg.StringWidth(a.Lines[i]),
leadingSpaces: leadingSpaces,
}
}
}

func setRow(a *Annot, rightAnnots []*Annot) {
row := 0

Expand Down

0 comments on commit e909629

Please sign in to comment.