Skip to content

Commit 0054c1f

Browse files
save file
1 parent bb32a6b commit 0054c1f

1 file changed

Lines changed: 32 additions & 42 deletions

File tree

utils/misc/nodejs-terminal/v2.0/nodejs-terminal-v2.0.html

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
console.log('nodejs-terminal-v2.0.html');
5151
console.log();
5252
console.json=v=>console.log(JSON.stringify(v,null,4));
53+
console.hex=v=>console.log(v,'Hex:',[...v].map(c=>c.charCodeAt(0).toString(16).padStart(2,'0')).join(' '));
5354
var version='v2.0';
5455
var df=true;
5556

@@ -778,14 +779,14 @@
778779

779780
const cursor = {
780781

781-
to : (x,y)=>term.write(`\x1b[${y+1};${x+1}H`),
782-
up : (n=1)=>term.write(`\x1b[${n}A`),
783-
down : (n=1)=>term.write(`\x1b[${n}B`),
784-
home : ()=>term.write('\x1b[H'),
785-
save : ()=>term.write('\x1b[s'),
786-
restore : ()=>term.write('\x1b[u'),
787-
hide : ()=>term.write('\x1b[?25l'),
788-
show : ()=>term.write('\x1b[?25h'),
782+
to : (x,y)=>`\x1b[${y+1};${x+1}H`,
783+
up : (n=1)=>`\x1b[${n}A`,
784+
down : (n=1)=>`\x1b[${n}B`,
785+
home : '\x1b[H',
786+
save : '\x1b[s',
787+
restore : '\x1b[u',
788+
hide : '\x1b[?25l',
789+
show : '\x1b[?25h',
789790
xy : ()=>{
790791
// Note: These are 0-indexed, so the top-left is 0,0.
791792
// ANSI sequences are usually 1-indexed (1,1).
@@ -799,8 +800,9 @@
799800
},//xy
800801

801802
};
802-
803-
803+
// \x1b[K clears from cursor to end of line
804+
// \x1b[J: Clears the screen from the cursor down.
805+
804806
var files = {};
805807

806808
/*
@@ -879,10 +881,7 @@
879881
}//resume
880882
term.write.xy = (x,y,txt)=>{
881883

882-
cursor.save();
883-
cursor.to(x,y);
884-
term.write(txt);
885-
cursor.restore();
884+
term.write(`${cursor.save}${cursor.to(x,y)}${txt}${cursor.restore}`);
886885

887886
}//xy
888887

@@ -944,8 +943,12 @@
944943

945944
});
946945

947-
948-
946+
/*
947+
~/rt4gcbtwdausp1nng6uyw4vpasf83c-wlvi
948+
❯ 
949+
[?2004h
950+
*/
951+
949952
var fitAddon = new FitAddon();
950953
term.loadAddon(fitAddon);
951954
term.open(terminal);
@@ -973,28 +976,21 @@
973976
var str = '??';
974977
if(result.code==0)str = result.output;
975978
term.write.xy(35,0,str)
976-
phase.ct = 2
977-
phase.p2()
978979

979980
}//p1
980981
phase.p2 = result=>{
981982

982-
if(result)phase.p2.result = result;
983-
if(phase.ct!=2)return;
984-
if(!phase.p2.result)return;
985983
s2.stop();
986984
var str = '??';
987-
if(phase.p2.result.code==0)str = phase.p2.result.output;
985+
if(result.code==0)str = result.output;
986+
str = str.replace(/\x1b\[1G/g, '').replace(/\x1b\[0K/g, '');
987+
//console.hex(str);
988988
term.write.xy(49,0,str);
989-
phase.ct = 3;
990-
phase.p3();
991989

992990
}//p2
993991
phase.p3 = result=>{
994-
//console.log('phase.p3',!!result);
995-
if(result)shell = result;
996-
if(phase.ct!=3)return;
997-
if(!shell)return;
992+
console.log('phase.p3',!!result);
993+
shell = result;
998994
s3.stop();
999995
term.write.xy(14,1,`${colors.green}ok.${colors.reset}`);
1000996
shell.output.pipeTo(output);
@@ -1008,7 +1004,7 @@
10081004

10091005
spawn('node -v',true).then(phase.p1);
10101006
spawn('npm -v',true).then(phase.p2);
1011-
webcontainer.spawn('jsh').then(phase.p3);
1007+
setTimeout(()=>webcontainer.spawn('jsh').then(phase.p3),50);
10121008

10131009

10141010

@@ -1027,7 +1023,7 @@
10271023
//term.writeln.green('ok');
10281024
output = new WritableStream({
10291025
write(data){
1030-
//console.log(data);
1026+
console.log(data);
10311027
var str = data;
10321028
str = str.replaceAll('\r','');
10331029
var lines = str.split('\n');
@@ -1177,14 +1173,15 @@
11771173
var spinner = {};
11781174
spinner.timer = null;
11791175
spinner.stop = null;
1180-
spinner.x = null;
1181-
spinner.y = null;
1176+
spinner.x = x;
1177+
spinner.y = y;
11821178

11831179
var frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
11841180
var i = 0;
11851181
// Hide the cursor so it doesn't flicker
11861182
//term.write(cursor.hide);
11871183
setTimeout(setup,50);
1184+
//setup();
11881185

11891186
return spinner;
11901187

@@ -1200,14 +1197,12 @@
12001197

12011198
spinner.timer = setInterval(()=>{
12021199
// \r moves to start of line, colors.cyan makes it pop
1203-
cursor.save();
1204-
cursor.to(x,y);
1205-
term.write(`${colors.cyan}${frames[i]}${colors.reset}`);
1206-
cursor.restore();
1200+
term.write(`${cursor.save}${cursor.to(x,y)}${colors.cyan}${frames[i]}${colors.reset}${cursor.restore}`);
12071201
i = (i+1)%frames.length;
12081202

12091203
},80);
12101204

1205+
12111206
spinner.stop = function(){
12121207

12131208
if(!spinner.timer){
@@ -1216,12 +1211,7 @@
12161211

12171212
clearInterval(spinner.timer);
12181213
spinner.timer = null;
1219-
// Clear the line, show cursor, and print final message
1220-
// \x1b[K clears from cursor to end of line
1221-
cursor.save();
1222-
cursor.to(x,y);
1223-
term.write(' ');
1224-
cursor.restore();
1214+
term.write(`${cursor.save}${cursor.to(x,y)} ${cursor.restore}`);
12251215
// Show cursor
12261216
//term.write(cursor.show);
12271217

0 commit comments

Comments
 (0)