-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathLogs.js
188 lines (167 loc) Β· 7 KB
/
Logs.js
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
import _ from "../imports/lodash.js"
import { cl } from "../common/ClassTable.js"
import { html, useState, useEffect, useLayoutEffect, useRef, useMemo } from "../imports/Preact.js"
import { SimpleOutputBody } from "./TreeView.js"
import { help_circle_icon } from "./Popup.js"
import AnsiUp from "../imports/AnsiUp.js"
import { open_pluto_popup } from "../common/open_pluto_popup.js"
const LOGS_VISIBLE_START = 60
const LOGS_VISIBLE_END = 20
const PROGRESS_LOG_LEVEL = "LogLevel(-1)"
const STDOUT_LOG_LEVEL = "LogLevel(-555)"
// const RESIZE_THROTTLE = 60
const is_progress_log = (log) => {
return log.level == PROGRESS_LOG_LEVEL && log.kwargs.find((kwarg) => kwarg[0] === "progress") !== undefined
}
const is_stdout_log = (log) => {
return log.level == STDOUT_LOG_LEVEL
}
export const Logs = ({ logs, line_heights, set_cm_highlighted_line, sanitize_html }) => {
const progress_logs = logs.filter(is_progress_log)
const latest_progress_logs = progress_logs.reduce((progress_logs, log) => ({ ...progress_logs, [log.id]: log }), {})
const stdout_log = logs.reduce((stdout_log, log) => {
if (!is_stdout_log(log)) {
return stdout_log
}
if (stdout_log === null) {
return log
}
return {
...stdout_log,
msg: [stdout_log.msg[0] + log.msg[0]], // Append to the previous stdout
}
}, null)
const [_, __, grouped_progress_and_logs] = logs.reduce(
([seen_progress, seen_stdout, final_logs], log) => {
const ipl = is_progress_log(log)
if (ipl && !seen_progress.has(log.id)) {
seen_progress.add(log.id)
return [seen_progress, seen_stdout, [...final_logs, latest_progress_logs[log.id]]]
} else if (!ipl) {
if (is_stdout_log(log) && !seen_stdout) {
return [seen_progress, true, [...final_logs, stdout_log]]
} else if (!is_stdout_log(log)) {
return [seen_progress, seen_stdout, [...final_logs, log]]
}
}
return [seen_progress, seen_stdout, final_logs]
},
[new Set(), false, []]
)
const is_hidden_input = line_heights[0] === 0
if (logs.length === 0) {
return null
}
const dot = (log, i) => html`<${Dot}
set_cm_highlighted_line=${set_cm_highlighted_line}
level=${log.level}
msg=${log.msg}
kwargs=${log.kwargs}
sanitize_html=${sanitize_html}
key=${i}
y=${is_hidden_input ? 0 : log.line - 1}
/> `
return html`
<pluto-logs-container>
<pluto-logs>
${grouped_progress_and_logs.length <= LOGS_VISIBLE_END + LOGS_VISIBLE_START
? grouped_progress_and_logs.map(dot)
: [
...grouped_progress_and_logs.slice(0, LOGS_VISIBLE_START).map(dot),
html`<pluto-log-truncated>
${grouped_progress_and_logs.length - LOGS_VISIBLE_START - LOGS_VISIBLE_END} logs not shown...
</pluto-log-truncated>`,
...grouped_progress_and_logs
.slice(-LOGS_VISIBLE_END)
.map((log, i) => dot(log, i + grouped_progress_and_logs.length - LOGS_VISIBLE_END)),
]}
</pluto-logs>
</pluto-logs-container>
`
}
const Progress = ({ name, progress }) => {
return html`<pluto-progress-name>${name}</pluto-progress-name>
<pluto-progress-bar-container><${ProgressBar} progress=${progress} /></pluto-progress-bar-container>`
}
const ProgressBar = ({ progress }) => {
const bar_ref = useRef(/** @type {HTMLElement?} */ (null))
useLayoutEffect(() => {
if (!bar_ref.current) return
bar_ref.current.style.backgroundSize = `${progress * 100}% 100%`
}, [bar_ref.current, progress])
return html`<pluto-progress-bar ref=${bar_ref}>${Math.ceil(100 * progress)}%</pluto-progress-bar>`
}
const Dot = ({ set_cm_highlighted_line, msg, kwargs, y, level, sanitize_html }) => {
const is_progress = is_progress_log({ level, kwargs })
const is_stdout = level === STDOUT_LOG_LEVEL
let progress = null
if (is_progress) {
progress = kwargs.find((p) => p[0] === "progress")[1][0]
if (progress === "nothing") {
progress = 0
} else if (progress === '"done"') {
progress = 1
} else {
progress = parseFloat(progress)
}
level = "Progress"
}
if (is_stdout) {
level = "Stdout"
}
const mimepair_output = (pair) =>
html`<${SimpleOutputBody} cell_id=${"adsf"} mime=${pair[1]} body=${pair[0]} persist_js_state=${false} sanitize_html=${sanitize_html} />`
useEffect(() => {
return () => set_cm_highlighted_line(null)
}, [])
return html`<pluto-log-dot-positioner
class=${cl({ [level]: true })}
onMouseenter=${() => is_progress || set_cm_highlighted_line(y + 1)}
onMouseleave=${() => {
set_cm_highlighted_line(null)
}}
>
<pluto-log-icon></pluto-log-icon>
<pluto-log-dot class=${level}
>${is_progress
? html`<${Progress} name="${msg[0]}" progress=${progress} />`
: is_stdout
? html`<${MoreInfo}
body=${html`${"This text was written to the "}
<a href="https://en.wikipedia.org/wiki/Standard_streams" target="_blank">terminal stream</a>${" while running the cell. "}<span
style="opacity: .5"
>${"(It is not the "}<em>return value</em>${" of the cell.)"}</span
>`}
/>
<${LogViewAnsiUp} value=${msg[0]} />`
: html`${mimepair_output(msg)}${kwargs.map(
([k, v]) => html`<pluto-log-dot-kwarg><pluto-key>${k}</pluto-key><pluto-value>${mimepair_output(v)}</pluto-value></pluto-log-dot-kwarg>`
)}`}</pluto-log-dot
>
</pluto-log-dot-positioner>`
}
const MoreInfo = (/** @type{{body: import("../imports/Preact.js").ReactElement}} */ { body }) => {
return html`<a
class="stdout-info"
target="_blank"
title="Click for more info"
href="#"
onClick=${(/** @type{Event} */ e) => {
open_pluto_popup({
type: "info",
source_element: /** @type {HTMLElement?} */ (e.currentTarget),
body,
})
e.preventDefault()
}}
><img alt="β" src=${help_circle_icon} width="17"
/></a>`
}
const LogViewAnsiUp = (/** @type {{value: string}} */ { value }) => {
const node_ref = useRef(/** @type {HTMLElement?} */ (null))
useEffect(() => {
if (!node_ref.current) return
node_ref.current.innerHTML = new AnsiUp().ansi_to_html(value)
}, [node_ref.current, value])
return html`<pre ref=${node_ref}></pre>`
}