Skip to content

Commit

Permalink
Kitty stuff works now
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Oct 29, 2024
1 parent b551c02 commit d11a608
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 62 deletions.
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@
runHook postInstall
'';
};
devShell = pkgs.mkShell {
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
nodejs
nodePackages.typescript
nodePackages.ts-node
];
};
}
Expand Down
36 changes: 16 additions & 20 deletions src/bundle/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ moduleAlias(__dirname + "../../../package.json");
import "reflect-metadata";
import cluster, { Worker } from "cluster";
import os from "os";
import { red, bold, yellow, cyan } from "picocolors";
import { red, bold, yellow, cyan, blueBright, redBright } from "picocolors";
import { initStats } from "./stats";
import { config } from "dotenv";

Expand All @@ -44,28 +44,24 @@ function getCommitOrFail() {

if (cluster.isPrimary) {
const commit = getCommitOrFail();
Logo.printLogo();
// Logo.printLogo();
const unformatted = `spacebar-server | ! Pre-release build !`;
const formatted = `${blueBright("spacebar-server")} | ${redBright("⚠️ Pre-release build ⚠️")}`;
console.log(
bold(`
${centerString(
`spacebar-server | ${yellow(
`Pre-release (${
commit !== null
? commit.slice(0, 7)
: "Unknown (Git cannot be found)"
})`,
)}`,
64,
)}
bold(centerString(unformatted, 64).replace(unformatted, formatted)),
);

Commit Hash: ${
commit !== null
? `${cyan(commit)} (${yellow(commit.slice(0, 7))})`
: "Unknown (Git cannot be found)"
}
Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)
`),
const unformattedGitHeader = `Commit Hash: ${commit !== null ? commit : "Unknown (Git cannot be found)"}`;
const formattedGitHeader = `Commit Hash: ${commit !== null ? `${cyan(commit)} (${yellow(commit.slice(0, 7))})` : "Unknown (Git cannot be found)"}`;
console.log(
bold(
centerString(unformattedGitHeader, 64).replace(
unformattedGitHeader,
formattedGitHeader,
),
),
);
console.log(`Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)`);

if (commit == null) {
console.log(yellow(`Warning: Git is not installed or not in PATH.`));
Expand Down
104 changes: 65 additions & 39 deletions src/util/util/KittyLogo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync } from "node:fs";
import fs from "fs";
import "missing-native-js-functions";

var util = require("util");

Expand All @@ -25,7 +26,17 @@ var util = require("util");
// KittyLogo.printWithIcon(message + " " + optionalParams.join(" "));
// };

export class KittyLogo {
export static class KittyLogo {

Check failure on line 29 in src/util/util/KittyLogo.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'static' modifier cannot appear on a module or namespace element.
private static isSupported = false;
private static iconCache: string;

public static async initialise() {
this.isSupported = await this.checkSupport();
this.iconCache = readFileSync(__dirname + "/../../../assets/icon.png", {
encoding: "base64",
});
}

public static printLogo(): void {
const data = readFileSync(__dirname + "/../../../assets/logo.png", {
encoding: "base64",
Expand All @@ -48,48 +59,54 @@ export class KittyLogo {
}

private static writeIcon(): void {
const data = readFileSync(__dirname + "/../../../assets/icon.png", {
encoding: "base64",
});
KittyLogo.writeImage({
base64Data: data,
base64Data: this.iconCache,
width: 2,
addNewline: false,
});
}

private static checkSupport(cb: void): boolean {
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);
let resp = "";
process.stdin.once("data", function (key) {
console.log(util.inspect(key));
process.stdin.setRawMode(false);
process.stdin.pause();
resp = key.toString();
private static checkSupport(): Promise<boolean> {
if (process.env.FORCE_KITTY) return Promise.resolve(true);
// Check if we are running in a terminal
if (!process.stdin.isTTY) return Promise.resolve(false);
if (!process.stdout.isTTY) return Promise.resolve(false);

// Check if we are running in a Kitty terminal
if (process.env.TERM == "xterm-kitty") return Promise.resolve(true);

// Check if we are running in a common unsupported terminal
if (process.env.TERM == "xterm") return Promise.resolve(false);
if (process.env.TERM == "xterm-256color") return Promise.resolve(false);

return new Promise<boolean>((resolve) => {
(async () => {
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);
let resp = "";
process.stdin.once("data", function (key) {
process.stdin.setRawMode(false);
process.stdin.pause();
resp = key.toString();
if (resp == "\x1B_Gi=31;OK\x1B\\\x1B[?62;c") resolve(true);
else resolve(false);
});
process.stdout.write(
"\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c",
);

await sleep(5000);
resolve(false);
})();
});
process.stdout.write(
"\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c",
);

while(resp == "") {
console.log("waiting");
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
}

return false;
}

// private static sleep(ms: number): void {
// Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
// }

private static writeImage(request: KittyImageMetadata): void {
if (this.checkSupport()) return;
private static writeImage(request: KittyImageMetadata) {
if (!this.isSupported) return;
let pngData = request.base64Data;

// Ga=T,q=2,o=z,s=1022,v=181,X=5;
const chunkSize = 1024;
const chunkSize = 4096;

//#region Header
let header = `\x1b_G`; // enable graphics
Expand Down Expand Up @@ -120,16 +137,25 @@ export class KittyLogo {
}
}

export class KittyImageMetadata {
public base64Data: string;
public width?: number;
public height?: number;
public widthPixels?: number;
public heightPixels?: number;
public addNewline?: boolean;
export interface KittyImageMetadata {
base64Data: string;
width?: number;
height?: number;
widthPixels?: number;
heightPixels?: number;
addNewline?: boolean;
}

KittyLogo.printLogo();
(async () => {
await KittyLogo.initialise();
KittyLogo.printLogo();

for (let i = 0; i < 1000; i++) {
console.time("meow");
KittyLogo.printWithIcon("meow");
console.timeEnd("meow");
}
})();

//
// for (let i = 0; i < 10; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/util/util/Logo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { KittyLogo } from "./KittyLogo";


export class Logo {
public static printLogo() {
KittyLogo.printLogo();
public static async printLogo() {
await KittyLogo.printLogo();
// const chafaPath = findOnPath("chafa");
// console.log("Chafa path: " + chafaPath);
// const info = terminfo.parse({debug: true});
Expand Down

0 comments on commit d11a608

Please sign in to comment.