Skip to content

Commit 6720faa

Browse files
Kitty stuff works now
1 parent 70cbd7d commit 6720faa

File tree

4 files changed

+85
-63
lines changed

4 files changed

+85
-63
lines changed

flake.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
runHook postInstall
8585
'';
8686
};
87-
devShell = pkgs.mkShell {
87+
devShells.default = pkgs.mkShell {
8888
buildInputs = with pkgs; [
8989
nodejs
9090
nodePackages.typescript
@@ -95,4 +95,4 @@
9595
};
9696
}
9797
);
98-
}
98+
}

src/bundle/start.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ moduleAlias(__dirname + "../../../package.json");
2424
import "reflect-metadata";
2525
import cluster, { Worker } from "cluster";
2626
import os from "os";
27-
import { red, bold, yellow, cyan } from "picocolors";
27+
import { red, bold, yellow, cyan, blueBright, redBright } from "picocolors";
2828
import { initStats } from "./stats";
2929
import { config } from "dotenv";
3030

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

4545
if (cluster.isPrimary) {
4646
const commit = getCommitOrFail();
47-
Logo.printLogo();
47+
// Logo.printLogo();
48+
const unformatted = `spacebar-server | ! Pre-release build !`;
49+
const formatted = `${blueBright("spacebar-server")} | ${redBright("⚠️ Pre-release build ⚠️")}`;
4850
console.log(
49-
bold(`
50-
${centerString(
51-
`spacebar-server | ${yellow(
52-
`Pre-release (${
53-
commit !== null
54-
? commit.slice(0, 7)
55-
: "Unknown (Git cannot be found)"
56-
})`,
57-
)}`,
58-
64,
59-
)}
51+
bold(centerString(unformatted, 64).replace(unformatted, formatted)),
52+
);
6053

61-
Commit Hash: ${
62-
commit !== null
63-
? `${cyan(commit)} (${yellow(commit.slice(0, 7))})`
64-
: "Unknown (Git cannot be found)"
65-
}
66-
Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)
67-
`),
54+
const unformattedGitHeader = `Commit Hash: ${commit !== null ? commit : "Unknown (Git cannot be found)"}`;
55+
const formattedGitHeader = `Commit Hash: ${commit !== null ? `${cyan(commit)} (${yellow(commit.slice(0, 7))})` : "Unknown (Git cannot be found)"}`;
56+
console.log(
57+
bold(
58+
centerString(unformattedGitHeader, 64).replace(
59+
unformattedGitHeader,
60+
formattedGitHeader,
61+
),
62+
),
6863
);
64+
console.log(`Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)`);
6965

7066
if (commit == null) {
7167
console.log(yellow(`Warning: Git is not installed or not in PATH.`));

src/util/util/KittyLogo.ts

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFileSync } from "node:fs";
22
import fs from "fs";
3+
import "missing-native-js-functions";
34

45
var util = require("util");
56

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

28-
export class KittyLogo {
29+
export static class KittyLogo {
30+
private static isSupported = false;
31+
private static iconCache: string;
32+
33+
public static async initialise() {
34+
this.isSupported = await this.checkSupport();
35+
this.iconCache = readFileSync(__dirname + "/../../../assets/icon.png", {
36+
encoding: "base64",
37+
});
38+
}
39+
2940
public static printLogo(): void {
3041
const data = readFileSync(__dirname + "/../../../assets/logo.png", {
3142
encoding: "base64",
@@ -48,48 +59,54 @@ export class KittyLogo {
4859
}
4960

5061
private static writeIcon(): void {
51-
const data = readFileSync(__dirname + "/../../../assets/icon.png", {
52-
encoding: "base64",
53-
});
5462
KittyLogo.writeImage({
55-
base64Data: data,
63+
base64Data: this.iconCache,
5664
width: 2,
5765
addNewline: false,
5866
});
5967
}
6068

61-
private static checkSupport(cb: void): boolean {
62-
process.stdin.setEncoding("utf8");
63-
process.stdin.setRawMode(true);
64-
let resp = "";
65-
process.stdin.once("data", function (key) {
66-
console.log(util.inspect(key));
67-
process.stdin.setRawMode(false);
68-
process.stdin.pause();
69-
resp = key.toString();
69+
private static checkSupport(): Promise<boolean> {
70+
if (process.env.FORCE_KITTY) return Promise.resolve(true);
71+
// Check if we are running in a terminal
72+
if (!process.stdin.isTTY) return Promise.resolve(false);
73+
if (!process.stdout.isTTY) return Promise.resolve(false);
74+
75+
// Check if we are running in a Kitty terminal
76+
if (process.env.TERM == "xterm-kitty") return Promise.resolve(true);
77+
78+
// Check if we are running in a common unsupported terminal
79+
if (process.env.TERM == "xterm") return Promise.resolve(false);
80+
if (process.env.TERM == "xterm-256color") return Promise.resolve(false);
81+
82+
return new Promise<boolean>((resolve) => {
83+
(async () => {
84+
process.stdin.setEncoding("utf8");
85+
process.stdin.setRawMode(true);
86+
let resp = "";
87+
process.stdin.once("data", function (key) {
88+
process.stdin.setRawMode(false);
89+
process.stdin.pause();
90+
resp = key.toString();
91+
if (resp == "\x1B_Gi=31;OK\x1B\\\x1B[?62;c") resolve(true);
92+
else resolve(false);
93+
});
94+
process.stdout.write(
95+
"\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c",
96+
);
97+
98+
await sleep(5000);
99+
resolve(false);
100+
})();
70101
});
71-
process.stdout.write(
72-
"\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c",
73-
);
74-
75-
while(resp == "") {
76-
console.log("waiting");
77-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
78-
}
79-
80-
return false;
81102
}
82103

83-
// private static sleep(ms: number): void {
84-
// Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
85-
// }
86-
87-
private static writeImage(request: KittyImageMetadata): void {
88-
if (this.checkSupport()) return;
104+
private static writeImage(request: KittyImageMetadata) {
105+
if (!this.isSupported) return;
89106
let pngData = request.base64Data;
90107

91108
// Ga=T,q=2,o=z,s=1022,v=181,X=5;
92-
const chunkSize = 1024;
109+
const chunkSize = 4096;
93110

94111
//#region Header
95112
let header = `\x1b_G`; // enable graphics
@@ -120,16 +137,25 @@ export class KittyLogo {
120137
}
121138
}
122139

123-
export class KittyImageMetadata {
124-
public base64Data: string;
125-
public width?: number;
126-
public height?: number;
127-
public widthPixels?: number;
128-
public heightPixels?: number;
129-
public addNewline?: boolean;
140+
export interface KittyImageMetadata {
141+
base64Data: string;
142+
width?: number;
143+
height?: number;
144+
widthPixels?: number;
145+
heightPixels?: number;
146+
addNewline?: boolean;
130147
}
131148

132-
KittyLogo.printLogo();
149+
(async () => {
150+
await KittyLogo.initialise();
151+
KittyLogo.printLogo();
152+
153+
for (let i = 0; i < 1000; i++) {
154+
console.time("meow");
155+
KittyLogo.printWithIcon("meow");
156+
console.timeEnd("meow");
157+
}
158+
})();
133159

134160
//
135161
// for (let i = 0; i < 10; i++) {

src/util/util/Logo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { KittyLogo } from "./KittyLogo";
66

77

88
export class Logo {
9-
public static printLogo() {
10-
KittyLogo.printLogo();
9+
public static async printLogo() {
10+
await KittyLogo.printLogo();
1111
// const chafaPath = findOnPath("chafa");
1212
// console.log("Chafa path: " + chafaPath);
1313
// const info = terminfo.parse({debug: true});

0 commit comments

Comments
 (0)