1
1
import { readFileSync } from "node:fs" ;
2
2
import fs from "fs" ;
3
+ import "missing-native-js-functions" ;
3
4
4
5
var util = require ( "util" ) ;
5
6
@@ -25,7 +26,17 @@ var util = require("util");
25
26
// KittyLogo.printWithIcon(message + " " + optionalParams.join(" "));
26
27
// };
27
28
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
+
29
40
public static printLogo ( ) : void {
30
41
const data = readFileSync ( __dirname + "/../../../assets/logo.png" , {
31
42
encoding : "base64" ,
@@ -48,48 +59,54 @@ export class KittyLogo {
48
59
}
49
60
50
61
private static writeIcon ( ) : void {
51
- const data = readFileSync ( __dirname + "/../../../assets/icon.png" , {
52
- encoding : "base64" ,
53
- } ) ;
54
62
KittyLogo . writeImage ( {
55
- base64Data : data ,
63
+ base64Data : this . iconCache ,
56
64
width : 2 ,
57
65
addNewline : false ,
58
66
} ) ;
59
67
}
60
68
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
+ } ) ( ) ;
70
101
} ) ;
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 ;
81
102
}
82
103
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 ;
89
106
let pngData = request . base64Data ;
90
107
91
108
// Ga=T,q=2,o=z,s=1022,v=181,X=5;
92
- const chunkSize = 1024 ;
109
+ const chunkSize = 4096 ;
93
110
94
111
//#region Header
95
112
let header = `\x1b_G` ; // enable graphics
@@ -120,16 +137,25 @@ export class KittyLogo {
120
137
}
121
138
}
122
139
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 ;
130
147
}
131
148
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
+ } ) ( ) ;
133
159
134
160
//
135
161
// for (let i = 0; i < 10; i++) {
0 commit comments