Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (142 loc) · 4.17 KB
/
Copy pathindex.js
File metadata and controls
181 lines (142 loc) · 4.17 KB
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
const ansiRegex = require('ansi-regex')
const superSplit = require('super-split')
const arrayUniq = require('array-uniq')
const stripAnsi = require('strip-ansi')
const realignOutOfBoundsCoords = (text, opts) => {
const plain = stripAnsi(text)
const lines = plain.split('\n')
const totalLines = lines.length
// 'End.line marker out of bounds (max).'
if (opts.end.line > totalLines) {
opts.end.line = totalLines
}
// 'Start.line marker out of bounds (min).'
if (opts.start.line < 1) {
opts.start.line = 1
}
// 'Start.column marker out of bounds (min).'
if (opts.start.column < 1) {
opts.start.column = 1
}
// 'End.column marker out of bounds (max).'
if (opts.end.column > lines[opts.end.line - 1].length) {
opts.end.column = lines[opts.end.line - 1].length
}
if (opts.start.line > opts.end.line) {
throw new Error('Your start line is after your end line.')
}
if (opts.start.line === opts.end.line &&
opts.end.column < opts.start.column) {
throw new Error('Your end column is after your start column.')
}
return false
}
// Returns arys:
// 1 - ANSI Escape sequences from section
// 2 - Glyphs in section (ansi escape seq - or - ascii character)
const atomize = section => {
const ansies = arrayUniq(section.match(ansiRegex()))
const words = superSplit(section, ansies)
let glyphs = []
words.forEach(word => {
if (ansies.includes(word) === false) {
glyphs = glyphs.concat(word.split(''))
return
}
glyphs.push(word)
})
return {ansies, glyphs}
}
const markSection = (section, opts, linear) => {
const {ansies, glyphs} = atomize(section)
let x = 0
let y = 0
let inPoint
let outPoint
let output = ''
const height = opts.end.line - opts.start.line
const markNotBegun = () => {
return typeof inPoint !== 'number' &&
typeof outPoint !== 'number'
}
const markHasEnded = () => {
return typeof inPoint === 'number' &&
typeof outPoint === 'number'
}
const outsideOfMark = () => {
return markNotBegun() || markHasEnded()
}
glyphs.forEach(glyph => {
if (ansies.includes(glyph) === false) {
if (glyph === '\n' && !linear) {
y += 1
x = -1
}
x += 1
if (x === opts.start.column && y === 0) {
inPoint = output.length
}
output += glyph
if (x === opts.end.column && y === height) {
outPoint = output.length
}
return
}
if (outsideOfMark()) {
output += glyph
} else if (!outsideOfMark() && !opts.resetColor) {
output += glyph
}
})
const pre = output.substr(0, inPoint)
const mark = opts.color(output.substr(inPoint, outPoint - inPoint))
const post = output.substr(outPoint)
const sectionMarked = pre + mark + post
return sectionMarked
}
const mark2d = (text, opts) => {
realignOutOfBoundsCoords(text, opts)
const lines = text.split('\n')
// Minus 1: because line and column numbers start at 1
const startLine = opts.start.line - 1
const endLine = opts.end.line - 1
// Plus 1: because slice does not include the end indice
const unmarkedSection = lines.slice(startLine, endLine + 1).join('\n')
const preSection = lines.slice(0, startLine)
const markedSection = markSection(unmarkedSection, opts)
const postSection = lines.slice(endLine + 1)
const result = preSection.concat([markedSection]).concat(postSection).join('\n')
return result
}
const mark1d = (text, opts, linear) => {
const markedSection = markSection(text, opts, linear)
return markedSection
}
const mark = (text, opts, linear) => {
return linear ? mark1d(text, opts, linear) : mark2d(text, opts)
}
const validMarkersNumbers = opts => {
return typeof opts.start === 'number' &&
typeof opts.end === 'number'
}
const validMarkersObject = opts => {
return typeof opts.start === 'object' &&
typeof opts.end === 'object' &&
typeof opts.start.line === 'number' &&
typeof opts.start.column === 'number' &&
typeof opts.end.line === 'number' &&
typeof opts.end.column === 'number'
}
const ansiMark = (text, opts) => {
if (validMarkersObject(opts)) {
return mark(text, opts)
}
if (validMarkersNumbers(opts)) {
opts.start = {line: 1, column: opts.start}
opts.end = {line: 1, column: opts.end}
const linear = true
return mark(text, opts, linear)
}
throw new Error('Invalid marker definition.')
}
module.exports = ansiMark