|
50 | 50 | console.log('nodejs-terminal-v2.0.html'); |
51 | 51 | console.log(); |
52 | 52 | 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(' ')); |
53 | 54 | var version='v2.0'; |
54 | 55 | var df=true; |
55 | 56 |
|
|
778 | 779 |
|
779 | 780 | const cursor = { |
780 | 781 |
|
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', |
789 | 790 | xy : ()=>{ |
790 | 791 | // Note: These are 0-indexed, so the top-left is 0,0. |
791 | 792 | // ANSI sequences are usually 1-indexed (1,1). |
|
799 | 800 | },//xy |
800 | 801 |
|
801 | 802 | }; |
802 | | - |
803 | | - |
| 803 | + // \x1b[K clears from cursor to end of line |
| 804 | + // \x1b[J: Clears the screen from the cursor down. |
| 805 | + |
804 | 806 | var files = {}; |
805 | 807 |
|
806 | 808 | /* |
|
879 | 881 | }//resume |
880 | 882 | term.write.xy = (x,y,txt)=>{ |
881 | 883 |
|
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}`); |
886 | 885 |
|
887 | 886 | }//xy |
888 | 887 |
|
|
944 | 943 |
|
945 | 944 | }); |
946 | 945 |
|
947 | | - |
948 | | - |
| 946 | +/* |
| 947 | +[J~/rt4gcbtwdausp1nng6uyw4vpasf83c-wlvi |
| 948 | +[1G[0J❯ [3G |
| 949 | +[?2004h |
| 950 | +*/ |
| 951 | + |
949 | 952 | var fitAddon = new FitAddon(); |
950 | 953 | term.loadAddon(fitAddon); |
951 | 954 | term.open(terminal); |
|
973 | 976 | var str = '??'; |
974 | 977 | if(result.code==0)str = result.output; |
975 | 978 | term.write.xy(35,0,str) |
976 | | - phase.ct = 2 |
977 | | - phase.p2() |
978 | 979 |
|
979 | 980 | }//p1 |
980 | 981 | phase.p2 = result=>{ |
981 | 982 |
|
982 | | - if(result)phase.p2.result = result; |
983 | | - if(phase.ct!=2)return; |
984 | | - if(!phase.p2.result)return; |
985 | 983 | s2.stop(); |
986 | 984 | 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); |
988 | 988 | term.write.xy(49,0,str); |
989 | | - phase.ct = 3; |
990 | | - phase.p3(); |
991 | 989 |
|
992 | 990 | }//p2 |
993 | 991 | 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; |
998 | 994 | s3.stop(); |
999 | 995 | term.write.xy(14,1,`${colors.green}ok.${colors.reset}`); |
1000 | 996 | shell.output.pipeTo(output); |
|
1008 | 1004 |
|
1009 | 1005 | spawn('node -v',true).then(phase.p1); |
1010 | 1006 | spawn('npm -v',true).then(phase.p2); |
1011 | | - webcontainer.spawn('jsh').then(phase.p3); |
| 1007 | + setTimeout(()=>webcontainer.spawn('jsh').then(phase.p3),50); |
1012 | 1008 |
|
1013 | 1009 |
|
1014 | 1010 |
|
|
1027 | 1023 | //term.writeln.green('ok'); |
1028 | 1024 | output = new WritableStream({ |
1029 | 1025 | write(data){ |
1030 | | - //console.log(data); |
| 1026 | + console.log(data); |
1031 | 1027 | var str = data; |
1032 | 1028 | str = str.replaceAll('\r',''); |
1033 | 1029 | var lines = str.split('\n'); |
|
1177 | 1173 | var spinner = {}; |
1178 | 1174 | spinner.timer = null; |
1179 | 1175 | spinner.stop = null; |
1180 | | - spinner.x = null; |
1181 | | - spinner.y = null; |
| 1176 | + spinner.x = x; |
| 1177 | + spinner.y = y; |
1182 | 1178 |
|
1183 | 1179 | var frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; |
1184 | 1180 | var i = 0; |
1185 | 1181 | // Hide the cursor so it doesn't flicker |
1186 | 1182 | //term.write(cursor.hide); |
1187 | 1183 | setTimeout(setup,50); |
| 1184 | + //setup(); |
1188 | 1185 |
|
1189 | 1186 | return spinner; |
1190 | 1187 |
|
|
1200 | 1197 |
|
1201 | 1198 | spinner.timer = setInterval(()=>{ |
1202 | 1199 | // \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}`); |
1207 | 1201 | i = (i+1)%frames.length; |
1208 | 1202 |
|
1209 | 1203 | },80); |
1210 | 1204 |
|
| 1205 | + |
1211 | 1206 | spinner.stop = function(){ |
1212 | 1207 |
|
1213 | 1208 | if(!spinner.timer){ |
|
1216 | 1211 |
|
1217 | 1212 | clearInterval(spinner.timer); |
1218 | 1213 | 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}`); |
1225 | 1215 | // Show cursor |
1226 | 1216 | //term.write(cursor.show); |
1227 | 1217 |
|
|
0 commit comments