aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gizak/termui/textbuilder.go
blob: 06a019beda80d460d14e8f1f1ffb400fcf42a6ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.

package termui

import (
    "regexp"
    "strings"

    "github.com/mitchellh/go-wordwrap"
)

// TextBuilder is a minimal interface to produce text []Cell using specific syntax (markdown).
type TextBuilder interface {
    Build(s string, fg, bg Attribute) []Cell
}

// DefaultTxBuilder is set to be MarkdownTxBuilder.
var DefaultTxBuilder = NewMarkdownTxBuilder()

// MarkdownTxBuilder implements TextBuilder interface, using markdown syntax.
type MarkdownTxBuilder struct {
    baseFg  Attribute
    baseBg  Attribute
    plainTx []rune
    markers []marker
}

type marker struct {
    st int
    ed int
    fg Attribute
    bg Attribute
}

var colorMap = map[string]Attribute{
    "red":     ColorRed,
    "blue":    ColorBlue,
    "black":   ColorBlack,
    "cyan":    ColorCyan,
    "yellow":  ColorYellow,
    "white":   ColorWhite,
    "default": ColorDefault,
    "green":   ColorGreen,
    "magenta": ColorMagenta,
}

var attrMap = map[string]Attribute{
    "bold":      AttrBold,
    "underline": AttrUnderline,
    "reverse":   AttrReverse,
}

func rmSpc(s string) string {
    reg := regexp.MustCompile(`\s+`)
    return reg.ReplaceAllString(s, "")
}

// readAttr translates strings like `fg-red,fg-bold,bg-white` to fg and bg Attribute
func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) {
    fg := mtb.baseFg
    bg := mtb.baseBg

    updateAttr := func(a Attribute, attrs []string) Attribute {
        for _, s := range attrs {
            // replace the color
            if c, ok := colorMap[s]; ok {
                a &= 0xFF00 // erase clr 0 ~ 8 bits
                a |= c      // set clr
            }
            // add attrs
            if c, ok := attrMap[s]; ok {
                a |= c
            }
        }
        return a
    }

    ss := strings.Split(s, ",")
    fgs := []string{}
    bgs := []string{}
    for _, v := range ss {
        subs := strings.Split(v, "-")
        if len(subs) > 1 {
            if subs[0] == "fg" {
                fgs = append(fgs, subs[1])
            }
            if subs[0] == "bg" {
                bgs = append(bgs, subs[1])
            }
        }
    }

    fg = updateAttr(fg, fgs)
    bg = updateAttr(bg, bgs)
    return fg, bg
}

func (mtb *MarkdownTxBuilder) reset() {
    mtb.plainTx = []rune{}
    mtb.markers = []marker{}
}

// parse streams and parses text into normalized text and render sequence.
func (mtb *MarkdownTxBuilder) parse(str string) {
    rs := str2runes(str)
    normTx := []rune{}
    square := []rune{}
    brackt := []rune{}
    accSquare := false
    accBrackt := false
    cntSquare := 0

    reset := func() {
        square = []rune{}
        brackt = []rune{}
        accSquare = false
        accBrackt = false
        cntSquare = 0
    }
    // pipe stacks into normTx and clear
    rollback := func() {
        normTx = append(normTx, square...)
        normTx = append(normTx, brackt...)
        reset()
    }
    // chop first and last
    chop := func(s []rune) []rune {
        return s[1 : len(s)-1]
    }

    for i, r := range rs {
        switch {
        // stacking brackt
        case accBrackt:
            brackt = append(brackt, r)
            if ')' == r {
                fg, bg := mtb.readAttr(string(chop(brackt)))
                st := len(normTx)
                ed := len(normTx) + len(square) - 2
                mtb.markers = append(mtb.markers, marker{st, ed, fg, bg})
                normTx = append(normTx, chop(square)...)
                reset()
            } else if i+1 == len(rs) {
                rollback()
            }
        // stacking square
        case accSquare:
            switch {
            // squares closed and followed by a '('
            case cntSquare == 0 && '(' == r:
                accBrackt = true
                brackt = append(brackt, '(')
            // squares closed but not followed by a '('
            case cntSquare == 0:
                rollback()
                if '[' == r {
                    accSquare = true
                    cntSquare = 1
                    brackt = append(brackt, '[')
                } else {
                    normTx = append(normTx, r)
                }
            // hit the end
            case i+1 == len(rs):
                square = append(square, r)
                rollback()
            case '[' == r:
                cntSquare++
                square = append(square, '[')
            case ']' == r:
                cntSquare--
                square = append(square, ']')
            // normal char
            default:
                square = append(square, r)
            }
        // stacking normTx
        default:
            if '[' == r {
                accSquare = true
                cntSquare = 1
                square = append(square, '[')
            } else {
                normTx = append(normTx, r)
            }
        }
    }

    mtb.plainTx = normTx
}

func wrapTx(cs []Cell, wl int) []Cell {
    tmpCell := make([]Cell, len(cs))
    copy(tmpCell, cs)

    // get the plaintext
    plain := CellsToStr(cs)

    // wrap
    plainWrapped := wordwrap.WrapString(plain, uint(wl))

    // find differences and insert
    finalCell := tmpCell // finalcell will get the inserts and is what is returned

    plainRune := []rune(plain)
    plainWrappedRune := []rune(plainWrapped)
    trigger := "go"
    plainRuneNew := plainRune

    for trigger != "stop" {
        plainRune = plainRuneNew
        for i := range plainRune {
            if plainRune[i] == plainWrappedRune[i] {
                trigger = "stop"
            } else if plainRune[i] != plainWrappedRune[i] && plainWrappedRune[i] == 10 {
                trigger = "go"
                cell := Cell{10, 0, 0}
                j := i - 0

                // insert a cell into the []Cell in correct position
                tmpCell[i] = cell

                // insert the newline into plain so we avoid indexing errors
                plainRuneNew = append(plainRune, 10)
                copy(plainRuneNew[j+1:], plainRuneNew[j:])
                plainRuneNew[j] = plainWrappedRune[j]

                // restart the inner for loop until plain and plain wrapped are
                // the same; yeah, it's inefficient, but the text amounts
                // should be small
                break

            } else if plainRune[i] != plainWrappedRune[i] &&
                plainWrappedRune[i-1] == 10 && // if the prior rune is a newline
                plainRune[i] == 32 { // and this rune is a space
                trigger = "go"
                // need to delete plainRune[i] because it gets rid of an extra
                // space
                plainRuneNew = append(plainRune[:i], plainRune[i+1:]...)
                break

            } else {
                trigger = "stop" // stops the outer for loop
            }
        }
    }

    finalCell = tmpCell

    return finalCell
}

// Build implements TextBuilder interface.
func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell {
    mtb.baseFg = fg
    mtb.baseBg = bg
    mtb.reset()
    mtb.parse(s)
    cs := make([]Cell, len(mtb.plainTx))
    for i := range cs {
        cs[i] = Cell{Ch: mtb.plainTx[i], Fg: fg, Bg: bg}
    }
    for _, mrk := range mtb.markers {
        for i := mrk.st; i < mrk.ed; i++ {
            cs[i].Fg = mrk.fg
            cs[i].Bg = mrk.bg
        }
    }

    return cs
}

// NewMarkdownTxBuilder returns a TextBuilder employing markdown syntax.
func NewMarkdownTxBuilder() TextBuilder {
    return MarkdownTxBuilder{}
}