-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-line.tsx
More file actions
47 lines (39 loc) · 1.55 KB
/
Copy pathbasic-line.tsx
File metadata and controls
47 lines (39 loc) · 1.55 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
import React, { useState, useEffect } from 'react';
import { render, useStdout, useInput } from 'ink';
import { InkUPlot } from '../src/index.js';
const count = 200;
const timestamps = Array.from({ length: count }, (_, i) => i);
const values = timestamps.map(t => Math.sin(t / 5) * 40 + 50);
const opts = {
scales: { x: { time: false } },
series: [
{},
{ stroke: 'cyan', label: 'Value', width: 2 },
],
axes: [
{ stroke: '#999', grid: { stroke: '#333' } },
{ stroke: '#999', grid: { stroke: '#333' } },
],
};
const data: [number[], number[]] = [timestamps, values];
function App({ exit }: { exit: () => void }) {
const { stdout } = useStdout();
const [size, setSize] = useState({ cols: stdout.columns ?? 80, rows: stdout.rows ?? 24 });
useEffect(() => {
// Debounce resize to a single trailing update (~200ms after the last event) so a
// drag doesn't thrash layout — effectively "resize on release".
let resizeTimer: ReturnType<typeof setTimeout> | undefined;
const onResize = () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => setSize({ cols: stdout.columns ?? 80, rows: stdout.rows ?? 24 }), 200);
};
stdout.on('resize', onResize);
return () => { clearTimeout(resizeTimer); stdout.off('resize', onResize); };
}, [stdout]);
useInput((input, key) => {
if (input === 'q' || (key.ctrl && input === 'c')) exit();
});
return <InkUPlot opts={opts} data={data} width={size.cols} height={size.rows} />;
}
const app = render(<App exit={() => app.unmount()} />);
await app.waitUntilExit();