-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
big-file-viewer.html
267 lines (258 loc) · 8.63 KB
/
big-file-viewer.html
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
<!doctype html>
<html>
<head>
<title>big file viewer</title>
<style>
/*#results {
font-fami
}*/
.context {
color: grey;
}
.res {
color: inherit;
text-decoration: none;
}
.res:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<form onsubmit="window.onSub(this, event)">
<label>
load from url
<input type="url" id="url" value="https://sheeptester.github.io/scratch-gui/lib.min.js" autofocus>
</label>
or
<label>
upload file
<input type="file" id="file">
</label>
<input type="submit" value="ok">
</form>
<textarea cols="50" id="log" readonly style="color:red" placeholder="errors go here"></textarea>
<!-- rows seems to be inexact?? nvm it is because the entr key symbo -->
<p><textarea id="out" readonly cols="100" rows="20" placeholder="text goes here"></textarea></p>
<input id="pos" placeholder="line:col" readonly>
<p>press arrow keys to navigate; "g" to go to line/column; "G" (shift + g) to go to end</p>
<form onsubmit="window.searchh(event)" >
<fieldset id="weee" disabled>
<legend>search</legend>
<label>
search:
<input type="search" id="searchy">
</label>
<input type="submit" value="search">
<label>
<input type=checkbox id=regex>
use regex?
</label></fieldset>
</form>
<pre id="results"></pre>
<script>
async function onSub (form, event) {
event.preventDefault()
try {
log.value = "loading..."
if (file.files.length > 0) {
loadtext(await file.files[0].text())
} else if (url.value) {
loadtext(await fetch(url.value).then(r => r.text()))
} else {
throw new Error('you neeed... url or file.')
}
log.value = ""
form.remove()
} catch (err) {
log.value = err.stack
log.focus()
throw err
}
}
function loadtext (text) {
//console.log(text)
text = text.replace(/\r/g, '')
const rows = []
const rowtoIndex = {}
const linetoIndex = {}
const rowIndextolineCol = {}
const rowOffset = {}
let linenum = 0
let chars = 0
for (const line of text.split('\n')) {
linenum++
linetoIndex[linenum] = chars
const header = `${linenum}. `
const offset = header.length
rowOffset[rows.length] = offset
let col = 1
let str = `${header}${line}¬` // ↵
let isfirstrow = true
while (str) {
rowtoIndex[rows.length] = chars
rowIndextolineCol[rows.length] = [linenum, col]
chars += str.slice(0, 100).length // not necessarily 100
col += str.slice(0, 100).length
if (isfirstrow) { chars -= offset; col -= offset }
rows.push(str.slice(0, 100))
str = str.slice(100)
isfirstrow = false
}
//chars--
}
//console.log(rows, rowtoIndex, linetoIndex)
let ROWindex
const entries = Object.entries(rowtoIndex)
function showtext (index, length = null) {
let rowIndex
//entries.forEach(([, charindex], i) => {
for (let i = 0; i < entries.length; i++) {
// [rowindex, charindex]
if (entries[i][1] > index) {
rowIndex = +entries[i - 1][0]
break
}
}
//const filtered = Object.entries(rowtoIndex).filter(([rowindex, charindex]) => charindex <= index)
//const rowIndex = +filtered[filtered.length - 1][0]
let str = ''
for (let i = rowIndex - 9; i < rowIndex; i++){
str += (rows[i]||'') + '\n'
}
const offset = str.length + (rowOffset[rowIndex] || 0)
str+=(rows[rowIndex])+'\n'
for (let i = rowIndex + 1; i <= rowIndex + 10; i++) {
str+=(rows[i ] || '')+'\n'
}
const rowCharIndex = index - rowtoIndex[rowIndex]
//console.log(outRows, rowIndex)
out.value = str.slice(0, -1)
out.focus()
// console.log(offset, rowCharIndex, rowIndex)
out.selectionStart = offset + rowCharIndex
out.selectionEnd = offset + rowCharIndex + (length /* pretty naive but am lazy */ || 1)
//for (const )
const [line, col] = rowIndextolineCol[rowIndex]
pos.value = `${line}:${col + rowCharIndex}`
ROWindex = rowIndex
}
let indexx = 0
out.addEventListener('keydown', e => {
const oldin = indexx
const col = indexx - rowtoIndex[ROWindex]
switch (e.key) {
case 'ArrowLeft': indexx--; break;
case 'ArrowRight': indexx++; break
case 'ArrowDown': indexx = rowtoIndex[ROWindex + 1] + (col) || text.length - 1; break
case 'ArrowUp': indexx = rowtoIndex[ROWindex - 1] + col || 0; break
case 'g': {
const wow = prompt('line:col', pos.value)
if (wow === null) return
const [line, col = 1] = wow.split(':')
indexx = linetoIndex[line] + +col - 1
break
}
case 'G': {
indexx = text.length - 1
break
}
default: return
}
e.preventDefault()
weeeIndex()
})
function weeeIndex (length) {
if (isNaN(indexx)) {indexx = oldin; return}
if (indexx < 0) indexx = 0
if (indexx >= text.length) indexx = text.length - 1
showtext(indexx, length)
}
showtext(0)
window.text = text
window.showtext = showtext
function ecape (str) {
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
function htmlFrom ([index, length]) {
return `<li><a href="#" class="res" data-index="${index}" data-length="${length}"><span class="context">${
ecape(text.slice(Math.max(0,index - 50), index))
}</span>${text.slice(index, index + length)}<span class="context">${
ecape(text.slice(index + length, index + length + 50))
}</span></a></li>`
}
window.searchh = e => {
e.preventDefault()
try {
const matcher = regex.checked ? getRegexMatches(text, new RegExp(searchy.value, 'g')) : gettextMatches(text, searchy.value)
//let i = 0
//innerHTML
results.textContent = ''
let w = '<ol>'
let done = false
//for (const [index, length] of matcher) {
for (let i = 0; i < 20 ; i++) {
//console.log(text.slice(index, index + length))
// i++
const { value, done: d } = matcher.next()
if (d) {
done = i === 0 ? 'first' : 'not first'
break
}
w+= htmlFrom(value)
//if (i >= 20) break
}
if (done === 'first') {
results.textContent = 'no matches'
} else {
let wow = w
if (!done) {
window.more = () => {
while (true) {
const { value, done } = matcher.next()
if (done) break
w += htmlFrom(value)
}
results.innerHTML = w+'</ol>'
}
wow += '<li><button onclick="window.more()">load rest</button></li>'
}
results.innerHTML = wow+'</ol>'
}
//console.log()
} catch (err) {
log.value = err.stack
log.focus()
throw err
}
}
weee.disabled = false
results.onclick = e => {
const link = e.target.closest('a')
if (!link) return
e.preventDefault()
indexx = +link.dataset.index
weeeIndex(+link.dataset.length)
}
}
function * gettextMatches (source, search) {
let lastIndex = 0
while (true) {
const index = source.indexOf(search, lastIndex)
if (index === -1) return
yield [index, search.length]
lastIndex = search.length + index
}
}
function * getRegexMatches (source, pattern) {
let match
while ((match = pattern.exec(source))) {
yield [match.index, match[0].length]
}
}
</script>
</body>
</html>