|
787 | 787 | hide : ()=>term.write('\x1b[?25l'), |
788 | 788 | show : ()=>term.write('\x1b[?25h'), |
789 | 789 | xy : ()=>{ |
790 | | - // Current Column |
| 790 | + // Note: These are 0-indexed, so the top-left is 0,0. |
| 791 | + // ANSI sequences are usually 1-indexed (1,1). |
| 792 | + // Current Column |
791 | 793 | var x = term.buffer.active.cursorX; |
792 | | - // Current Row |
| 794 | + // Current Row |
793 | 795 | var y = term.buffer.active.cursorY; |
794 | | - console.log(`Cursor is at: ${x}, ${y}`); |
| 796 | + console.log(`cursor is at: ${x}, ${y}`); |
795 | 797 | return {x,y}; |
796 | 798 |
|
797 | 799 | },//xy |
|
1168 | 1170 |
|
1169 | 1171 | function spinner({x,y}={}){ |
1170 | 1172 |
|
1171 | | - var frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; |
1172 | | - var i = 0; |
| 1173 | + var spinner = {}; |
| 1174 | + spinner.timer = null; |
| 1175 | + spinner.stop = null; |
| 1176 | + spinner.x = null; |
| 1177 | + spinner.y = null; |
1173 | 1178 |
|
| 1179 | + var frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; |
| 1180 | + var i = 0; |
1174 | 1181 | // Hide the cursor so it doesn't flicker |
1175 | 1182 | //term.write(cursor.hide); |
| 1183 | + setTimeout(setup,50); |
1176 | 1184 |
|
1177 | | - if(x===undefined){ |
1178 | | - ({x,y} = cursor.xy()); |
1179 | | - } |
1180 | | - |
1181 | | - var spinner = {}; |
| 1185 | + return spinner; |
1182 | 1186 |
|
1183 | | - spinner.timer = setInterval(()=>{ |
1184 | | - // \r moves to start of line, colors.cyan makes it pop |
1185 | | - cursor.save(); |
1186 | | - cursor.to(x,y); |
1187 | | - term.write(`\r${colors.cyan}${frames[i]}${colors.reset}`); |
1188 | | - cursor.restore(); |
1189 | | - i = (i+1)%frames.length; |
1190 | | - |
1191 | | - },80); |
1192 | 1187 |
|
1193 | | - spinner.stop = function(){ |
| 1188 | + function setup(){ |
1194 | 1189 |
|
1195 | | - if(!spinner.timer){ |
1196 | | - return; |
| 1190 | + if(x===undefined){ |
| 1191 | + ({x,y} = cursor.xy()); |
| 1192 | + spinner.x = x; |
| 1193 | + spinner.y = y; |
1197 | 1194 | } |
1198 | 1195 |
|
1199 | | - clearInterval(spinner.timer); |
1200 | | - spinner.timer = null; |
| 1196 | + |
| 1197 | + spinner.timer = setInterval(()=>{ |
| 1198 | + // \r moves to start of line, colors.cyan makes it pop |
| 1199 | + cursor.save(); |
| 1200 | + cursor.to(x+1,y); |
| 1201 | + term.write(`${colors.cyan}${frames[i]}${colors.reset}`); |
| 1202 | + cursor.restore(); |
| 1203 | + i = (i+1)%frames.length; |
| 1204 | + |
| 1205 | + },80); |
| 1206 | + |
| 1207 | + spinner.stop = function(){ |
| 1208 | + |
| 1209 | + if(!spinner.timer){ |
| 1210 | + return; |
| 1211 | + } |
| 1212 | + |
| 1213 | + clearInterval(spinner.timer); |
| 1214 | + spinner.timer = null; |
1201 | 1215 | // Clear the line, show cursor, and print final message |
1202 | 1216 | // \x1b[K clears from cursor to end of line |
1203 | | - cursor.save(); |
1204 | | - cursor.to(x,y); |
1205 | | - term.write(' '); |
1206 | | - cursor.restore(); |
1207 | | - // Show cursor |
1208 | | - //term.write(cursor.show); |
| 1217 | + cursor.save(); |
| 1218 | + cursor.to(x+1,y); |
| 1219 | + term.write(' '); |
| 1220 | + cursor.restore(); |
| 1221 | + // Show cursor |
| 1222 | + //term.write(cursor.show); |
| 1223 | + |
| 1224 | + }//stop |
1209 | 1225 |
|
1210 | | - }//stop |
1211 | | - |
1212 | | - return spinner; |
| 1226 | + }//setup |
1213 | 1227 |
|
1214 | 1228 | }//spinner |
1215 | 1229 |
|
|
0 commit comments