From 5bebbe8f817d012788cff7fbb589d232ebf34643 Mon Sep 17 00:00:00 2001 From: Marcel Meyer Date: Thu, 13 Jun 2024 21:25:03 +0200 Subject: [PATCH] Extract function that creates internal line struct --- annot.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/annot.go b/annot.go index 72bd250..147d8be 100644 --- a/annot.go +++ b/annot.go @@ -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 @@ -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. @@ -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 holding internal state. +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