📝 update stl/process_bar.nas and test/md5compare.nas

This commit is contained in:
ValKmjolnir 2022-06-15 20:21:03 +08:00
parent ef3afa14b9
commit c2a23ecba6
2 changed files with 93 additions and 83 deletions

View File

@ -2,9 +2,11 @@
# ValKmjolnir 2022/6/14 # ValKmjolnir 2022/6/14
# this file is inspired by a Python lib: alive_progress # this file is inspired by a Python lib: alive_progress
import("stl/log.nas"); var process_bar={
bar:nil,
var generate_bar=func(){ spinner:nil
};
process_bar.bar=func(){
var bar={ var bar={
solid_triangle_right:"▶", solid_triangle_right:"▶",
hollow_triangle_right:"▷", hollow_triangle_right:"▷",
@ -52,15 +54,36 @@ var generate_bar=func(){
var s=""; var s="";
for(var i=0;i<finish_length;i+=1) for(var i=0;i<finish_length;i+=1)
s~=front; s~=front;
for(var i=0;i<other-1;i+=1) for(var i=0;i<other;i+=1)
s~=back; s~=back;
return sep[0]~s~sep[1]; return sep[0]~s~sep[1];
} }
}; };
}; };
}(); }();
process_bar.spinner=func(){
var generate_spinner=func(){ var generate_scrolling_spinner=func(s){
if(typeof(s)!="str")
s="****";
if(size(s)>16)
s="****";
var vec=split("",s);
var res=[];
var len=size(vec);
var tmp="";
for(var i=0;i<len;i+=1){
tmp=pop(vec)~tmp;
append(res,tmp);
while(size(res[-1])!=16)
res[-1]~=" ";
}
tmp=res[-1];
while(tmp!=" "){
tmp=" "~substr(tmp,0,15);
append(res,tmp);
}
return res;
}
var spinner={ var spinner={
rise:["▁","▂","▃","▄","▅","▆","▇","█","█","▇","▆","▅","▄","▃","▂","▁"], rise:["▁","▂","▃","▄","▅","▆","▇","█","█","▇","▆","▅","▄","▃","▂","▁"],
vertical:["▏","▎","▍","▌","▋","▊","▉","▇","▇","▉","▊","▋","▌","▍","▎","▏"], vertical:["▏","▎","▍","▌","▋","▊","▉","▇","▇","▉","▊","▋","▌","▍","▎","▏"],
@ -70,14 +93,14 @@ var generate_spinner=func(){
classic:["/","-","\\","-"], classic:["/","-","\\","-"],
balls:["●...",".●..","..●.","...●",], balls:["●...",".●..","..●.","...●",],
dots_wave:[ dots_wave:[
"⠁⠂⠄⡀⢀⠠⠐⠈", "⠈⠁⠂⠄⡀⢀⠠⠐",
"⠂⠄⡀⢀⠠⠐⠈⠁",
"⠄⡀⢀⠠⠐⠈⠁⠂",
"⡀⢀⠠⠐⠈⠁⠂⠄",
"⢀⠠⠐⠈⠁⠂⠄⡀",
"⠠⠐⠈⠁⠂⠄⡀⢀",
"⠐⠈⠁⠂⠄⡀⢀⠠", "⠐⠈⠁⠂⠄⡀⢀⠠",
"⠈⠁⠂⠄⡀⢀⠠⠐" "⠠⠐⠈⠁⠂⠄⡀⢀",
"⢀⠠⠐⠈⠁⠂⠄⡀",
"⡀⢀⠠⠐⠈⠁⠂⠄",
"⠄⡀⢀⠠⠐⠈⠁⠂",
"⠂⠄⡀⢀⠠⠐⠈⠁",
"⠁⠂⠄⡀⢀⠠⠐⠈"
], ],
pulse:[ pulse:[
"----------------", "----------------",
@ -130,32 +153,34 @@ var generate_spinner=func(){
"▇▆▆▇", "▇▆▆▇",
"▆▅▇█" "▆▅▇█"
], ],
fish:[ happy:[
" ", " ",
"> ", "ᕗ ",
"`> ", " ᕗ ",
")`> ", ") ᕗ ",
"))`> ", "ᐛ ) ᕗ ",
")))`> ", " ᐛ ) ᕗ ",
"))))`> ", "( ᐛ ) ᕗ ",
"<))))`> ", " ( ᐛ ) ᕗ ",
"><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ ",
" ><))))`> ", " ᕕ ( ᐛ ) ᕗ",
" ><))))`> ", " ᕕ ( ᐛ ) ",
" ><))))`>", " ᕕ ( ᐛ )",
" ><))))`", " ᕕ ( ᐛ ",
" ><))))", " ᕕ ( ᐛ",
" ><)))", " ᕕ ( ",
" ><))", " ᕕ (",
" ><)", " ᕕ ",
" ><", " ᕕ",
" >" ],
] fish:generate_scrolling_spinner("><))))`>"),
wait:generate_scrolling_spinner("please wait"),
stars:generate_scrolling_spinner("********")
}; };
return func(type="classic",repeat=1){ return func(type="classic",repeat=1){
if(typeof(type)!="str" or !contains(spinner,type)) if(typeof(type)!="str" or !contains(spinner,type))
@ -179,46 +204,47 @@ var generate_spinner=func(){
var show=func(){ var show=func(){
print("\ec"); print("\ec");
var bars={ var bars={
"classic ":generate_bar(front:"sharp",back:"point",sep:"bracket",length:40), "classic ":process_bar.bar(front:"sharp",back:"point",sep:"bracket",length:40),
"classic2 ":generate_bar(front:"equal",back:"point",sep:"bracket",length:40), "classic2 ":process_bar.bar(front:"equal",back:"point",sep:"bracket",length:40),
"triangle ":generate_bar(front:"solid_triangle_right",back:"hollow_triangle_right",sep:"angle_bracket",length:40), "triangle ":process_bar.bar(front:"solid_triangle_right",back:"hollow_triangle_right",sep:"angle_bracket",length:40),
"dots ":generate_bar(front:"solid_circle",back:"hollow_circle",sep:"curve",length:40), "dots ":process_bar.bar(front:"solid_circle",back:"hollow_circle",sep:"curve",length:40),
"ticks ":generate_bar(front:"tick",back:"space",sep:"line",length:40), "ticks ":process_bar.bar(front:"tick",back:"space",sep:"line",length:40),
"deep_shadow":generate_bar(front:"deep_shadow",back:"light_shadow",sep:"line",length:40), "deep_shadow":process_bar.bar(front:"deep_shadow",back:"light_shadow",sep:"line",length:40),
"block ":generate_bar(front:"block",back:"light_shadow",sep:"line",length:40) "block ":process_bar.bar(front:"block",back:"light_shadow",sep:"line",length:40)
}; };
var key=keys(bars); var key=keys(bars);
for(var i=0;i<40;i+=1){ for(var i=0;i<40;i+=1){
forindex(var j;key){ forindex(var j;key){
var k=key[j]; var k=key[j];
print("\e["~(j+1)~";1H["~k~"] "~bars[k].bar(i/40)); print("\e["~(j+1)~";1H["~k~"] "~bars[k].bar((i+1)/40));
} }
unix.sleep(1/15); unix.sleep(1/25);
} }
var spinners={ var spinners={
"rise ":generate_spinner(type:"rise",repeat:16), "rise ":process_bar.spinner(type:"rise",repeat:16),
"vertical ":generate_spinner(type:"vertical",repeat:16), "vertical ":process_bar.spinner(type:"vertical",repeat:16),
"dot ":generate_spinner(type:"dot",repeat:16), "dot ":process_bar.spinner(type:"dot",repeat:16),
"dots ":generate_spinner(type:"dots",repeat:16), "dots ":process_bar.spinner(type:"dots",repeat:16),
"arrow ":generate_spinner(type:"arrow",repeat:16), "arrow ":process_bar.spinner(type:"arrow",repeat:16),
"classic ":generate_spinner(type:"classic",repeat:16), "classic ":process_bar.spinner(type:"classic",repeat:16),
"balls ":generate_spinner(type:"balls",repeat:4), "balls ":process_bar.spinner(type:"balls",repeat:4),
"dots_wave ":generate_spinner(type:"dots_wave",repeat:2), "dots_wave ":process_bar.spinner(type:"dots_wave",repeat:2),
"pulse ":generate_spinner(type:"pulse",repeat:1), "pulse ":process_bar.spinner(type:"pulse",repeat:1),
"wave ":generate_spinner(type:"wave",repeat:2), "wave ":process_bar.spinner(type:"wave",repeat:2),
"short_wave ":generate_spinner(type:"short_wave",repeat:4), "short_wave ":process_bar.spinner(type:"short_wave",repeat:4),
"fish ":generate_spinner(type:"fish",repeat:1) "fish ":process_bar.spinner(type:"fish",repeat:1),
"happy ":process_bar.spinner(type:"happy",repeat:1),
"wait ":process_bar.spinner(type:"wait",repeat:1),
"stars ":process_bar.spinner(type:"stars",repeat:1)
}; };
var key=keys(spinners); var key=keys(spinners);
for(var i=0;i<60;i+=1){ for(var i=0;i<80;i+=1){
forindex(var j;key){ forindex(var j;key){
var k=key[j]; var k=key[j];
var tmp=spinners[k]; var tmp=spinners[k];
print("\e["~(j+1+size(bars))~";1H["~k~"] |"~tmp.next()~"|"); print("\e["~(j+1+size(bars))~";1H["~k~"] |"~tmp.next()~"|");
} }
unix.sleep(1/15); unix.sleep(1/20);
} }
print("\n"); print("\n");
} }
show();

View File

@ -1,19 +1,7 @@
import("test/md5.nas"); import("test/md5.nas");
import("stl/process_bar.nas");
srand(); srand();
var progress_bar=func(){
var res=[];
setsize(res,51);
var (tmp,sp)=(" |"," | ");
res[0]=tmp~sp;
for(var i=1;i<=50;i+=1){
tmp~="#";
res[i]=tmp~substr(sp,i,52-i);
}
return res;
}();
var compare=func(){ var compare=func(){
var ch=[ var ch=[
"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","+", "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","+",
@ -21,7 +9,8 @@ var compare=func(){
"^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?" "^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?"
]; ];
return func(begin,end){ return func(begin,end){
var (total,prt_cnt,lastpercent,percent)=(end-begin,0,0,0); var total=end-begin;
var bar=process_bar.bar(front:"block",back:"point",sep:"line",length:50);
for(var i=begin;i<end;i+=1){ for(var i=begin;i<end;i+=1){
var s=""; var s="";
for(var j=0;j<i;j+=1){ for(var j=0;j<i;j+=1){
@ -31,12 +20,7 @@ var compare=func(){
if(cmp(res,_md5(s))){ if(cmp(res,_md5(s))){
die("error: "~str(i)); die("error: "~str(i));
} }
percent=int((i-begin+1)/total*100); print(" ",bar.bar((i-begin+1)/total)," (",i-begin+1,"/",total,")\t",res," max byte: ",end-1," \r");
if(percent-lastpercent>=2){
prt_cnt+=1;
lastpercent=percent;
}
print(progress_bar[prt_cnt],percent,"% (",i-begin+1,"/",total,")\t",res," max byte: ",end-1," \r");
} }
print('\n'); print('\n');
}; };