-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprefixer.go
164 lines (131 loc) · 4.07 KB
/
prefixer.go
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
package bubblelister
import (
"fmt"
"github.com/muesli/reflow/ansi"
"strings"
)
// Prefixer is used to prefix all visible Lines.
// Init gets called ones on the beginning of the Lines methode
// and then Prefix ones, per line to draw, to generate according prefixes.
type Prefixer interface {
InitPrefixer(value fmt.Stringer, currentItemIndex, cursorIndex, lineOffset, width, height int) int
Prefix(currentLine, allLines int) string
}
// DefaultPrefixer is the default struct used for Prefixing a line
type DefaultPrefixer struct {
PrefixWrap bool
// Make clear where a item begins and where it ends
FirstSep string
Seperator string
SeperatorWrap string
// Mark it so that even without color support all is explicit
CurrentMarker string
// enable Linenumber
Number bool
NumberRelative bool
prefixWidth int
cursorIndex int
markWidth int
numWidth int
unmark string
mark string
sepItem string
sepWrap string
currentIndex int
}
// NewPrefixer returns a DefautPrefixer with default values
func NewPrefixer() *DefaultPrefixer {
return &DefaultPrefixer{
PrefixWrap: true,
// Make clear where a item begins and where it ends
FirstSep: "╭",
Seperator: "├",
SeperatorWrap: "│",
// Mark it so that even without color support all is explicit
CurrentMarker: ">",
// enable Linenumber
Number: true,
NumberRelative: false,
}
}
// InitPrefixer sets up all strings used to prefix a given line later by Prefix()
func (d *DefaultPrefixer) InitPrefixer(value fmt.Stringer, currentItemIndex, cursorIndex, lineOffset, width, height int) int {
// TODO adapt to per item call
d.currentIndex = currentItemIndex
d.cursorIndex = cursorIndex
offset := cursorIndex - lineOffset
if offset < 0 {
offset = 0
}
seperator := d.Seperator
if currentItemIndex == 0 {
seperator = d.FirstSep
}
// Get separators width
widthItem := ansi.PrintableRuneWidth(seperator)
widthWrap := ansi.PrintableRuneWidth(d.SeperatorWrap)
// Find max width
sepWidth := widthItem
if widthWrap > sepWidth {
sepWidth = widthWrap
}
// get widest possible number, for padding
// TODO handle wrap, cause only correct when wrap off:
d.numWidth = len(fmt.Sprintf("%d", offset+height))
// pad all prefixes to the same width for easy exchange
// pad all separators to the same width for easy exchange
d.sepItem = strings.Repeat(" ", sepWidth-widthItem) + seperator
d.sepWrap = strings.Repeat(" ", sepWidth-widthWrap) + d.SeperatorWrap
// pad right of prefix, with length of current pointer
d.mark = d.CurrentMarker
d.markWidth = ansi.PrintableRuneWidth(d.mark)
d.unmark = strings.Repeat(" ", d.markWidth)
// Get the hole prefix width
d.prefixWidth = d.numWidth + sepWidth + d.markWidth
return d.prefixWidth
}
// Prefix prefixes a given line
func (d *DefaultPrefixer) Prefix(lineIndex, allLines int) string {
var (
wrapPad string
lineNum int
)
if d.Number {
lineNum = lineNumber(d.NumberRelative, d.cursorIndex, d.currentIndex)
}
number := fmt.Sprintf("%d", lineNum)
// since digits are only single bytes, len is sufficient:
padTo := d.numWidth - len(number)
if padTo < 0 {
// TODO log error
padTo = 0
}
// if a number is set, prepend first line with number and both with enough spaces
firstPad := strings.Repeat(" ", padTo) + number
// pad wrapped lines
wrapPad = strings.Repeat(" ", d.numWidth)
// Current: handle highlighting of current item/first-line
curPad := d.unmark
if d.currentIndex == d.cursorIndex {
curPad = d.mark
}
// join all prefixes
linePrefix := strings.Join([]string{firstPad, d.sepItem, curPad}, "")
if lineIndex > 0 {
linePrefix = strings.Join([]string{wrapPad, d.sepWrap, d.unmark}, "") // don't prefix wrap lines with CurrentMarker (unmark)
}
return linePrefix
}
// lineNumber returns line number of the given index
// and if relative is true the absolute difference to the cursor
// or if on the cursor the absolute line number
func lineNumber(relativ bool, curser, current int) int {
if !relativ || curser == current {
return current + 1
}
diff := curser - current
if diff < 0 {
diff *= -1
}
return diff
}