Skip to content

Commit

Permalink
◐ Spin Pkg terminal spinners with CSS for performance (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Jan 22, 2024
1 parent 46d1030 commit a617193
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion frontend/components/PkgTerminalView.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import AnsiUp from "../imports/AnsiUp.js"
import { html, Component, useState, useEffect, useRef, useLayoutEffect } from "../imports/Preact.js"

const make_spinner_spin = (original_html) => original_html.replaceAll("◐", `<span class="make-me-spin">◐</span>`)

const TerminalViewAnsiUp = ({ value }) => {
const node_ref = useRef(/** @type {HTMLElement?} */ (null))

const start_time = useRef(Date.now())

useEffect(() => {
if (!node_ref.current) return
node_ref.current.innerHTML = new AnsiUp().ansi_to_html(value)
node_ref.current.style.cssText = `--animation-delay: -${(Date.now() - start_time.current) % 1000}ms`
node_ref.current.innerHTML = make_spinner_spin(new AnsiUp().ansi_to_html(value))
const parent = node_ref.current.parentElement
if (parent) parent.scrollTop = 1e5
}, [node_ref.current, value])
Expand Down
7 changes: 7 additions & 0 deletions frontend/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,13 @@ pkg-terminal pre {
margin: 0;
}

pkg-terminal .make-me-spin {
display: inline-block;
animation: identifier-spin 1s linear infinite;
transform-origin: 50% 59%;
animation-delay: var(--animation-delay);
}

pkg-popup pkg-terminal {
display: none;
}
Expand Down
5 changes: 5 additions & 0 deletions src/packages/IOListener.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ function stoplistening(listener::IOListener)
trigger(listener)
end
end

freeze_loading_spinners(s::AbstractString) = _replaceall(s, 'β—‘' => '◐', 'β—’' => '◐', 'β—“' => '◐')

_replaceall(s, p) = replace(s, p)
_replaceall(s, p, ps...) = @static VERSION >= v"1.7" ? replace(s, p, ps...) : _replaceall(replace(s, p), ps...)
4 changes: 2 additions & 2 deletions src/packages/Packages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function sync_nbpkg_core(
iolistener = let
busy_packages = notebook.nbpkg_ctx_instantiated ? added : new_packages
report_to = ["nbpkg_sync", busy_packages...]
IOListener(callback=(s -> on_terminal_output(report_to, s)))
IOListener(callback=(s -> on_terminal_output(report_to, freeze_loading_spinners(s))))
end
cleanup[] = () -> stoplistening(iolistener)

Expand Down Expand Up @@ -553,7 +553,7 @@ function update_nbpkg_core(
iolistener = let
# we don't know which packages will be updated, so we send terminal output to all installed packages
report_to = ["nbpkg_update", old_packages...]
IOListener(callback=(s -> on_terminal_output(report_to, s)))
IOListener(callback=(s -> on_terminal_output(report_to, freeze_loading_spinners(s))))
end
cleanup[] = () -> stoplistening(iolistener)

Expand Down

0 comments on commit a617193

Please sign in to comment.