Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-16041-matrix-ghost-terminal-effects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add the Ghost coder (`mx.effect=ghost`) and Terminal (`mx.effect=terminal`) effects to the opt-in Matrix display, plus an `mx.captions=off` toggle; both render only lines already served by `/api/matrix/lines` (#16041, RFC #15862).
18 changes: 18 additions & 0 deletions src/docs/streaming-the-activity-monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ Dashboard authentication alone does not make a code stream safe for public
Twitch broadcasting: the public allowlist, author restriction and scrub/drop
rules are required.

The display's look is chosen with `mx.*` URL parameters; each value is also
remembered in the browser (`hive.matrix.*` in local storage) for later visits.
Every effect renders only lines already delivered by `/api/matrix/lines`.

| `mx.effect` | Display |
|---|---|
| `rain` (default) | Falling columns built from the characters of the latest lines. Unknown values fall back to `rain`. |
| `cascade` | Whole lines drift down the screen, each with its `repo#PR · path` caption. |
| `ghost` | Ghost coder: a fake editor with one tab per PR (`repo#PR`), titled with the file path; at most six tabs, the oldest closes first. Lines are typed character by character behind a blinking cursor, with light keyword/string/comment/number colouring by file extension. |
| `terminal` | An endless `git log -p`-style scroll: each line printed as a green `+ <line>`, with a `repo#PR · path` header whenever the PR or file changes. |

`mx.speed` (0.1–5, default 1) and `mx.fps` (1–60, default 30) set the
animation and typing speed, `mx.palette` is `green` (default), `amber` or
`gold`, and `mx.captions=off` hides the `repo#PR · path` captions and headers
on the canvas. `p` or Space pauses; a hidden tab pauses too. With the
operating system's *reduce motion* setting on, every effect is replaced by
static pages of the latest lines with their captions.

[`overlays/hive-hosted-matrix/`](../deploy/stream/overlays/hive-hosted-matrix/kustomization.yaml)
reuses the streamer image and base as a separate instance. It renders Deployment,
ConfigMap and NetworkPolicy `matrix-hive-stream` in namespace `hive-stream-matrix`,
Expand Down
8 changes: 6 additions & 2 deletions src/pkg/dashboard/matrix_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestMatrixUIWiring(t *testing.T) {
t.Errorf("matrix.js contains forbidden sink/stream %q", forbidden)
}
}
for _, want := range []string{"visibilitychange", "prefers-reduced-motion: reduce", "cancelAnimationFrame", "text.textContent = line.line", "source.addEventListener('code-lines'", "hive.matrix.", "mx."} {
for _, want := range []string{"visibilitychange", "prefers-reduced-motion: reduce", "cancelAnimationFrame", "text.textContent = line.line", "source.addEventListener('code-lines'", "hive.matrix.", "mx.", "'ghost'", "'terminal'", "effect === 'ghost'", "effect === 'terminal'"} {
if !strings.Contains(js, want) {
t.Errorf("matrix.js missing %q", want)
}
Expand Down Expand Up @@ -120,7 +120,9 @@ async function scenario(query, reduced = false, status = 200) {
assert.equal(raf.size, 1);
for (const time of [1, 100]) {const fn = [...raf.values()][0]; raf.clear(); fn(time)}
assert(draws.length > 0);
if (query.includes('cascade')) assert(draws.some(a => a[0] === 'org/repo#2 · main.go'));
if (/cascade|ghost|terminal/.test(query)) assert(draws.some(a => a[0] === 'org/repo#2 · main.go'));
if (query.includes('ghost')) assert(draws.some(a => a[0] === 'main.go'));
if (query.includes('terminal')) assert(draws.some(a => a[0] === '+ <script>alert(1)</script>'));
}
doc.hidden = true; docEvents.visibilitychange.forEach(fn => fn());
assert.equal(raf.size, 0); assert.equal(timers.size, 0);
Expand All @@ -138,6 +140,8 @@ async function scenario(query, reduced = false, status = 200) {
await scenario(''); await scenario('?view=matrix'); await scenario('?kiosk=1');
await scenario('?kiosk=1&view=matrix');
await scenario('?kiosk=1&view=matrix&mx.effect=cascade');
await scenario('?kiosk=1&view=matrix&mx.effect=ghost');
await scenario('?kiosk=1&view=matrix&mx.effect=terminal');
await scenario('?kiosk=1&view=matrix', true);
for (const status of [404, 401, 403]) await scenario('?kiosk=1&view=matrix', false, status);
})().catch(e => {console.error(e); process.exit(1)});
Expand Down
145 changes: 137 additions & 8 deletions src/pkg/dashboard/static-src/js/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
var value = Number(option(name, String(fallback)));
return Number.isFinite(value) ? Math.max(min, Math.min(max, value)) : fallback;
}
var effect = option('effect', 'rain') === 'cascade' ? 'cascade' : 'rain';
var effect = option('effect', 'rain');
if (['rain', 'cascade', 'ghost', 'terminal'].indexOf(effect) < 0) effect = 'rain';
var captions = option('captions', 'on') !== 'off';
var speed = number('speed', 1, 0.1, 5), fps = number('fps', 30, 1, 60);
var palette = option('palette', 'green');
var colors = {green: '#52ed86', amber: '#ffb347', gold: '#ffd700'};
Expand Down Expand Up @@ -56,15 +58,33 @@
var enabled = false, manualPause = false, loading = false, pending = false;
var frame = 0, previous = 0, page = 0, pageTimer = 0, drops = [];
var atlas = document.createElement('canvas'), glyphs = new Map();
var width = 0, height = 0, cell = 20;
var width = 0, height = 0, cell = 20, rowH = 20, charW = 10;
// Ghost coder and Terminal type the delivered lines in order; once the
// queue runs dry they replay the bounded latest-line window.
var queue = [], tabs = [], typing = null, termRows = [], termHead = '', termClock = 0;
var muted = '#6f8f78', syntaxColors = {keyword: '#7fd4ff', string: '#ffd27f', comment: muted, number: '#ff9cf0'};
var languages = {
go: {comment: '//', words: 'package import func return if else for range var const type struct interface map chan go defer select case switch default break continue nil true false'},
js: {comment: '//', words: 'function var let const return if else for while do new class extends import export from default async await this null undefined true false typeof instanceof switch case break continue try catch finally throw of in'},
py: {comment: '#', words: 'def class import from return if elif else for while in not and or is None True False with as lambda yield pass raise try except finally async await'},
sh: {comment: '#', words: 'if then else elif fi for while do done case esac function local export return in'},
rs: {comment: '//', words: 'fn let mut pub use mod struct enum impl trait match if else for while loop return self Self true false const static where async await'},
c: {comment: '//', words: 'if else for while do return break continue switch case default new class public private protected static const struct enum typedef sizeof void int char long double float unsigned true false null this import package'}
};
var extensions = {go: 'go', js: 'js', mjs: 'js', cjs: 'js', ts: 'js', tsx: 'js', jsx: 'js', py: 'py', sh: 'sh', bash: 'sh',
rs: 'rs', c: 'c', h: 'c', cc: 'c', cpp: 'c', hpp: 'c', java: 'c', cs: 'c'};
Object.keys(languages).forEach(function(k){ languages[k].words = new Set(languages[k].words.split(' ')); });
function paused(){ return manualPause || document.hidden; }
function resize(){
width = window.innerWidth; height = window.innerHeight;
canvas.width = width; canvas.height = height;
drops = [];
ctx.font = '16px monospace';
charW = (ctx.measureText && ctx.measureText('M').width) || 10;
}
function resetText(){ queue = []; tabs = []; typing = null; termRows = []; termHead = ''; termClock = 0; }
function clearContent(message){
enabled = false; lines = []; drops = []; glyphs.clear();
enabled = false; lines = []; drops = []; glyphs.clear(); resetText();
lastSeq = 0; lastAt = 0;
list.replaceChildren(); ctx.clearRect(0, 0, width, height);
notice.textContent = message;
Expand All @@ -77,6 +97,110 @@
pausedLabel.textContent = paused() ? 'paused' : 'p / Space to pause';
}
function caption(line){ return line.repo + '#' + line.pr + ' · ' + line.path; }
function prKey(line){ return line.repo + '#' + line.pr; }
function nextLine(key){
if (!queue.length) queue = lines.slice();
var at = 0;
for (var i = 0; key && i < queue.length; i++) if (prKey(queue[i]) === key) { at = i; break; }
return queue.splice(at, 1)[0] || null;
}
// Minimal keyword/string/comment/number colouring keyed on the extension;
// unknown extensions render in the palette colour.
function tokens(path, text){
var ext = (/\.([^.\/]+)$/.exec(path || '') || [])[1];
var lang = languages[extensions[ext ? ext.toLowerCase() : '']];
if (!lang) return [{text: text, color: color}];
var out = [], last = 0, match;
var pattern = /("(?:[^"\\]|\\.)*"?|'(?:[^'\\]|\\.)*'?|`[^`]*`?)|(\b\d[\w.]*)|([A-Za-z_$][\w$]*)|(\/\/|#)/g;
function push(t, c){ if (t) out.push({text: t, color: c}); }
while ((match = pattern.exec(text))) {
push(text.slice(last, match.index), color);
last = pattern.lastIndex;
if (match[4] === lang.comment) { push(text.slice(match.index), syntaxColors.comment); last = text.length; break; }
push(match[0], match[1] ? syntaxColors.string : match[2] ? syntaxColors.number :
match[3] && lang.words.has(match[0]) ? syntaxColors.keyword : color);
}
push(text.slice(last), color);
return out;
}
function openTab(line){
var key = prKey(line), tab = null;
tabs.forEach(function(t){ if (t.key === key) tab = t; });
if (!tab) {
tab = {key: key, rows: [], count: 0}; tabs.push(tab);
if (tabs.length > 6) tabs.shift();
}
tab.path = line.path;
return tab;
}
function startTyping(line){
typing = line ? {tab: openTab(line), line: line, chars: Array.from(line.line), typed: 0} : null;
}
function drawGhost(dt, now){
if (!typing) startTyping(nextLine(''));
if (!typing) return;
typing.typed += dt * 40;
// A short beat at the end of each line before the next one starts.
if (typing.typed >= typing.chars.length + 4) {
var done = typing.tab;
done.count++;
done.rows.push({n: done.count, path: typing.line.path, text: typing.line.line});
if (done.rows.length > 200) done.rows.shift();
startTyping(nextLine(done.key));
if (!typing) return;
}
var tab = typing.tab, top = 34, tabW = Math.min(240, width / Math.max(1, tabs.length));
ctx.font = '13px monospace';
tabs.forEach(function(t, i){
ctx.fillStyle = t === tab ? '#0b2a16' : '#02140a';
ctx.fillRect(i * tabW, 0, tabW - 2, 28);
ctx.fillStyle = t === tab ? color : muted;
ctx.fillText(t.path, i * tabW + 10, 19, tabW - 20);
});
if (captions) {
ctx.fillStyle = '#b8ccbb'; ctx.font = '12px monospace';
ctx.fillText(caption(typing.line), 10, 48, width - 20);
top = 56;
}
ctx.font = '16px monospace';
var visible = Math.max(1, Math.floor((height - top - 100) / rowH));
var current = {n: tab.count + 1, path: typing.line.path, text: typing.chars.slice(0, Math.floor(typing.typed)).join(''), current: true};
tab.rows.slice(-(visible - 1) || tab.rows.length).concat([current]).forEach(function(row, i){
var y = top + (i + 1) * rowH, col = 0;
ctx.fillStyle = muted; ctx.fillText(String(row.n), 10, y);
tokens(row.path, row.text).forEach(function(token){
ctx.fillStyle = token.color; ctx.fillText(token.text, 60 + col * charW, y);
col += Array.from(token.text).length;
});
if (row.current && Math.floor(now / 530) % 2 === 0) { ctx.fillStyle = color; ctx.fillRect(60 + col * charW, y - 14, 2, 18); }
});
}
function emitTerminal(){
var line = nextLine('');
if (!line) return;
if (caption(line) !== termHead) {
termHead = caption(line);
if (captions) {
if (termRows.length) termRows.push({text: '', color: color});
termRows.push({text: termHead, color: '#b8ccbb'});
}
}
termRows.push({text: '+ ' + line.line, color: color});
var keep = Math.ceil(height / rowH) + 4;
if (termRows.length > keep) termRows = termRows.slice(-keep);
}
function drawTerminal(dt){
if (!termRows.length) emitTerminal();
termClock += dt * 4;
while (termClock >= 1) { termClock -= 1; emitTerminal(); }
var bottom = height - 100;
ctx.font = '16px monospace';
termRows.forEach(function(row, i){
var y = bottom - (termRows.length - 1 - i + termClock) * rowH;
if (y < -rowH || !row.text) return;
ctx.fillStyle = row.color; ctx.fillText(row.text, 10, y, width - 20);
});
}
function staticPage(){
list.replaceChildren();
var latest = lines.slice(-60).reverse();
Expand Down Expand Up @@ -119,17 +243,21 @@
ctx.fillStyle = effect === 'rain' ? 'rgba(0, 8, 4, 0.18)' : '#000804';
ctx.fillRect(0, 0, width, height);
ctx.font = '16px monospace';
if (lines.length) {
if (lines.length && effect === 'ghost') drawGhost(dt, now);
else if (lines.length && effect === 'terminal') drawTerminal(dt);
else if (lines.length) {
var count = effect === 'rain' ? Math.ceil(width / cell) : 12;
while (drops.length < count) drops.push(spawn(effect === 'rain' ? drops.length * cell : Math.random() * width * 0.5));
drops.forEach(function(drop, i){
drop.y += drop.speed * dt;
if (drop.y > height + 40) { drops[i] = spawn(drop.x); return; }
if (effect === 'cascade') {
ctx.fillStyle = color; ctx.fillText(drop.line.line, drop.x, drop.y, width - drop.x);
ctx.fillStyle = '#b8ccbb'; ctx.font = '12px monospace';
ctx.fillText(caption(drop.line), drop.x, drop.y + 18, width - drop.x);
ctx.font = '16px monospace';
if (captions) {
ctx.fillStyle = '#b8ccbb'; ctx.font = '12px monospace';
ctx.fillText(caption(drop.line), drop.x, drop.y + 18, width - drop.x);
ctx.font = '16px monospace';
}
} else if (drop.chars.length) {
var g = drop.chars[Math.floor(drop.y / cell + drop.chars.length * 100) % drop.chars.length];
var index = glyphs.get(g);
Expand Down Expand Up @@ -168,13 +296,14 @@
var incoming = (Array.isArray(data.lines) ? data.lines : []).filter(function(line){ return typeof line.line === 'string'; });
var seq = Number(data.seq) || 0;
// A restarted hive can reset its sequence; replace the old backlog.
if (seq < lastSeq) { lines = []; drops = []; }
if (seq < lastSeq) { lines = []; drops = []; resetText(); }
if (seq !== lastSeq && incoming.length) {
var at = Date.parse(incoming[incoming.length - 1].at);
lastAt = Number.isFinite(at) ? at : Date.now();
}
lastSeq = seq;
lines = lines.concat(incoming).slice(-120);
queue = queue.concat(incoming).slice(-120);
enabled = true;
notice.textContent = lines.length ? '' : 'Waiting for pushed lines…';
if (!motion.matches) buildAtlas();
Expand Down
Loading
Loading