github.com/jmigpin/editor@v1.6.0/util/drawutil/drawer4/indent.go (about) 1 package drawer4 2 3 import ( 4 "unicode" 5 ) 6 7 var NoPaddedIndentedLines bool 8 var useSpaceForMargin bool 9 10 type Indent struct { 11 d *Drawer 12 } 13 14 func (in *Indent) Init() {} 15 16 func (in *Indent) Iter() { 17 if in.d.iters.runeR.isNormal() { 18 // keep track of indentation for wrapped lines 19 penXAdv := in.d.st.runeR.pen.X + in.d.st.runeR.advance 20 if !in.d.st.indent.notStartingSpaces { 21 if in.d.st.lineWrap.wrapping { 22 in.d.st.indent.notStartingSpaces = true 23 in.d.st.indent.indent = 0 // ensure being able to view content 24 } else { 25 if unicode.IsSpace(in.d.st.runeR.ru) { 26 in.d.st.indent.indent = penXAdv 27 } else { 28 in.d.st.indent.notStartingSpaces = true 29 } 30 } 31 } 32 } 33 34 if in.d.st.lineWrap.postLineWrap { 35 in.indent() 36 } 37 38 if !in.d.iterNext() { 39 return 40 } 41 42 if in.d.iters.runeR.isNormal() { 43 if in.d.st.runeR.ru == '\n' { 44 in.d.st.indent.notStartingSpaces = false 45 in.d.st.indent.indent = 0 46 } 47 } 48 } 49 50 func (in *Indent) End() {} 51 52 //---------- 53 54 func (in *Indent) indent() { 55 // set ident 56 pen := &in.d.st.runeR.pen 57 pen.X = in.d.st.indent.indent 58 // left padding 59 pad := in.d.iters.runeR.glyphAdvance('\t') 60 if NoPaddedIndentedLines { 61 pad = 0 62 } 63 startX := in.d.iters.runeR.startingPen().X 64 if pen.X == 0 { 65 pen.X = startX + pad 66 } else { 67 pen.X += pad 68 } 69 // margin on the right 70 marginRune := 'W' 71 if useSpaceForMargin { // for tests 72 marginRune = ' ' 73 } 74 margin := 10 * in.d.iters.runeR.glyphAdvance(marginRune) 75 maxX := in.d.iters.runeR.maxX() - margin 76 if pen.X > maxX { 77 pen.X = maxX 78 } 79 // margin on the left 80 if pen.X < startX { 81 pen.X = startX 82 } 83 }