Last active
August 16, 2019 22:18
-
-
Save anon5r/bf31cb5f5f9656770319c660d44ca9e8 to your computer and use it in GitHub Desktop.
コマンドラインでの進捗状況(プログレスバー)を表示するやつ(Node.JS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Pad string to right | |
| String.prototype.pad=function(len,str){ | |
| if(typeof str=="undefined") str=" "; | |
| if(this.length>len) return this.toString(); | |
| str=str.repeat(len-this.length); | |
| return (this+str).substr(0,len); | |
| } | |
| // Pad string to left | |
| String.prototype.lpad=function(len,str){ | |
| if(typeof str=="undefined") str=" "; | |
| if(this.length>len) return this; | |
| str=str.repeat(len); | |
| return (str+this).slice(len*-1); | |
| } | |
| // 回転するやつ /-\| | |
| //let c=['|','/','-','\\']; | |
| let c=['⣤','⣆','⡇','⠏','⠛','⠹','⢸','⣰']; | |
| let ci=c.length,i=0; | |
| setInterval(function(){ | |
| process.stdout.write("\r"); // 描画カーソルを行頭に戻す | |
| var padlen=20; | |
| var bar="#".repeat(parseInt(i/5)).pad(padlen," "); | |
| var perc=i.toString().lpad(3," "); | |
| process.stdout.write("Processing... ["+bar+"] "+i+"% "+c[i%ci],"utf8"); | |
| i++; if (i > 100) process.exit(); | |
| },50); | |
| // 出力行直後の改行 | |
| process.stdout.write("\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment