refactor(rendering): changed my mind, don't use components

This commit is contained in:
Pihkaal
2024-01-26 12:23:52 +01:00
parent 0a9ce46e02
commit ef41377558
3 changed files with 12 additions and 34 deletions

View File

@@ -1,8 +1,6 @@
import { useTerminal } from "~/context/TerminalContext"; import { useTerminal } from "~/context/TerminalContext";
import { TerminalRenderer } from "~/utils/terminal/renderer"; import { TerminalRenderer } from "~/utils/terminal/renderer";
import { TerminalBoxElement } from "~/utils/terminal/elements/box"; import { TerminalBoxElement } from "~/utils/terminal/elements/box";
import { TerminalCanvas } from "./terminal/TerminalCanvas";
import { TerminalText } from "./terminal/TerminalText";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
const theme = { const theme = {
@@ -24,44 +22,24 @@ const formatDurationMSS = (duration: number) => {
return `${minutes}:${seconds.toString().padStart(2, "0")}`; return `${minutes}:${seconds.toString().padStart(2, "0")}`;
}; };
export const MusicPlayer = (props: { export const MusicPlayer = (props: {
title: string; title: string;
artist: string; artist: string;
album: string; album: string;
duration: number; duration: number;
played: number;
}) => { }) => {
const { cols } = useTerminal(); const { cols } = useTerminal();
const [count, setCount] = useState(0); const canvas = new TerminalRenderer(cols, 5);
const [played, setPlayed] = useState(0);
useEffect(() => { useEffect(() => {
const interval = setInterval(() => { const interval = setInterval(() => {
setCount(c => c + 1); setPlayed(x => Math.min(props.duration, x + 1));
}, 1000); }, 1000);
return () => clearInterval(interval); return () => clearInterval(interval);
}); }, [setPlayed]);
return (
<TerminalCanvas width={cols} height={5}>
<TerminalText x={1} y={0}>
Playback
</TerminalText>
</TerminalCanvas>
);
};
export const MusicPlayer2 = (props: {
title: string;
artist: string;
album: string;
duration: number;
played: number;
}) => {
props;
formatDurationMSS;
const { cols } = useTerminal();
const canvas = new TerminalRenderer(cols, 5);
canvas.writeElement( canvas.writeElement(
new TerminalBoxElement(canvas.width, canvas.height), new TerminalBoxElement(canvas.width, canvas.height),
@@ -75,7 +53,7 @@ export const MusicPlayer2 = (props: {
const inner = new TerminalRenderer(canvas.width - 2, canvas.height - 2); const inner = new TerminalRenderer(canvas.width - 2, canvas.height - 2);
// Title and Artist // Title and Artist
inner.write(2, 0, "Last Tango in Kyoto · Floating Bits", { inner.write(2, 0, `${props.title} · ${props.artist}`, {
foreground: theme.cyan, foreground: theme.cyan,
fontWeight: 800, fontWeight: 800,
}); });
@@ -86,19 +64,20 @@ export const MusicPlayer2 = (props: {
}); });
// Album // Album
inner.write(0, 1, "Last Tango in Kyoto", { foreground: theme.yellow }); inner.write(0, 1, props.album, { foreground: theme.yellow });
// Bar // Bar
const percentage = 45;
inner.write(0, 2, " ".repeat(inner.width), { inner.write(0, 2, " ".repeat(inner.width), {
foreground: theme.green, foreground: theme.green,
background: theme.black, background: theme.black,
}); });
inner.write(0, 2, " ".repeat((inner.width / 100) * percentage), { inner.write(0, 2, " ".repeat((inner.width * played) / props.duration), {
foreground: theme.black, foreground: theme.black,
background: theme.green, background: theme.green,
}); });
const time = "1:10/1:51"; const time = `${formatDurationMSS(played)}/${formatDurationMSS(
props.duration,
)}`;
inner.write(inner.width / 2 - time.length / 2, 2, time); inner.write(inner.width / 2 - time.length / 2, 2, time);
canvas.writeElement(inner, 1, 1); canvas.writeElement(inner, 1, 1);

View File

@@ -24,7 +24,6 @@ export default function Home() {
title="Last Tango in Kyoto" title="Last Tango in Kyoto"
artist="Floating Bits" artist="Floating Bits"
album="Last Tango in Kyoto" album="Last Tango in Kyoto"
played={50}
duration={93} duration={93}
/> />
</Terminal> </Terminal>

View File

@@ -89,7 +89,7 @@ export class TerminalRenderer implements TerminalElement {
render(): Array<ReactNode> { render(): Array<ReactNode> {
const nodes: Array<ReactNode> = []; const nodes: Array<ReactNode> = [];
console.log("here2");
for (let y = 0; y < this.height; y++) { for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) { for (let x = 0; x < this.width; x++) {
const cell = this.data[y][x]; const cell = this.data[y][x];