🚀 add stl/log.nas and stl/process_bar.nas

This commit is contained in:
ValKmjolnir 2022-06-15 00:36:49 +08:00
parent 6b20e0f167
commit ef3afa14b9
13 changed files with 278 additions and 11 deletions

View File

@ -1,7 +1,5 @@
# lib file.nas
# ValKmjolnir 2022/3/6
import("lib.nas");
var file={
SEEK_SET:io.SEEK_SET,
SEEK_CUR:io.SEEK_CUR,

View File

@ -1,4 +1,4 @@
# lib list.nas
# list.nas
# valkmjolnir 2021/3/31
var list=func()
{

40
stl/log.nas Normal file
View File

@ -0,0 +1,40 @@
# log.nas
# ValKmjolnir 2022/6/14
var log=func(){
var (log_date,log_time,prefix)=(1,1,"");
var os_time="";
var prt_core=func(elem){
os_time=os.time();
print(prefix," ");
if(log_date and log_time)
print(os_time," ");
elsif(log_date or log_time){
var s=split(" ",os_time);
if(log_date)
print(s[0]," ");
if(log_time)
print(s[1]," ");
}
foreach(var i;elem)
print(i);
print("\n");
}
return {
setflags:func(date,time){
log_date=!!date;
log_time=!!time;
},
setprefix:func(s){
if(typeof(s)!="str")
println("[log.nas] must use string as the prefix.");
prefix=s;
},
println:func(elem...){
prt_core(elem);
},
fatalln:func(elem...){
prt_core(elem);
die("log:fatal error");
}
};
}();

View File

@ -1,4 +1,4 @@
# lib module.nas
# module.nas
# ValKmjolnir 2022/3/5
# this provides safe usage of dylib

224
stl/process_bar.nas Normal file
View File

@ -0,0 +1,224 @@
# process_bar.nas
# ValKmjolnir 2022/6/14
# this file is inspired by a Python lib: alive_progress
import("stl/log.nas");
var generate_bar=func(){
var bar={
solid_triangle_right:"▶",
hollow_triangle_right:"▷",
solid_triangle_left:"◀",
hollow_triangle_left:"◁",
solid_circle:"●",
hollow_circle:"○",
tick:"✔",
cross:"✘",
light_shadow:"░",
medium_shadow:"▒",
deep_shadow:"▓",
block:"█",
sharp:"#",
square:"√",
equal:"=",
space:" ",
point:"."
};
var separator={
angle_bracket:["<",">"],
line:["|","|"],
bracket:["[","]"],
space:[" "," "],
curve:["(",")"]
};
return func(front="sharp",back="space",sep="line",length=20){
if(typeof(front)!="str" or !contains(bar,front))
front="sharp";
if(typeof(back)!="str" or !contains(bar,back))
back="space";
if(typeof(sep)!="str" or !contains(separator,sep))
sep="line";
front=bar[front];
back=bar[back];
sep=separator[sep];
return {
bar:func(number){
if(number>1)
number=1;
if(number<0)
number=0;
var finish_length=int(number*length);
var other=length-finish_length;
var s="";
for(var i=0;i<finish_length;i+=1)
s~=front;
for(var i=0;i<other-1;i+=1)
s~=back;
return sep[0]~s~sep[1];
}
};
};
}();
var generate_spinner=func(){
var spinner={
rise:["▁","▂","▃","▄","▅","▆","▇","█","█","▇","▆","▅","▄","▃","▂","▁"],
vertical:["▏","▎","▍","▌","▋","▊","▉","▇","▇","▉","▊","▋","▌","▍","▎","▏"],
dot:["⠁","⠈","⠐","⠠","⢀","⡀","⠄","⠂"],
dots:["⣾","⣷","⣯","⣟","⡿","⢿","⣻","⣽"],
arrow:["↑","↗","→","↘","↓","↙","←","↖"],
classic:["/","-","\\","-"],
balls:["●...",".●..","..●.","...●",],
dots_wave:[
"⠁⠂⠄⡀⢀⠠⠐⠈",
"⠂⠄⡀⢀⠠⠐⠈⠁",
"⠄⡀⢀⠠⠐⠈⠁⠂",
"⡀⢀⠠⠐⠈⠁⠂⠄",
"⢀⠠⠐⠈⠁⠂⠄⡀",
"⠠⠐⠈⠁⠂⠄⡀⢀",
"⠐⠈⠁⠂⠄⡀⢀⠠",
"⠈⠁⠂⠄⡀⢀⠠⠐"
],
pulse:[
"----------------",
"●---------------",
"-●--------------",
"--●-------------",
"---●------------",
"----●-----------",
"-----●----------",
"------●---------",
"-------√--------",
"-------√\\-------",
"-------√\\/------",
"-------√\\/●-----",
"--------\\/-●----",
"---------/--●---",
"-------------●--",
"--------------●-",
"---------------●"
],
wave:[
"▁▂▃▄▅▆▇█",
"▂▃▄▅▆▇█▇",
"▃▄▅▆▇█▇▆",
"▄▅▆▇█▇▆▅",
"▅▆▇█▇▆▅▄",
"▆▇█▇▆▅▄▃",
"▇█▇▆▅▄▃▂",
"█▇▆▅▄▃▂▁",
"▇▆▅▄▃▂▁▂",
"▆▅▄▃▂▁▂▃",
"▅▄▃▂▁▂▃▄",
"▄▃▂▁▂▃▄▅",
"▃▂▁▂▃▄▅▆",
"▂▁▂▃▄▅▆▇"
],
short_wave:[
"▅▄█▇",
"▄▃▇▆",
"▃▂▆▅",
"▂▁▅▄",
"▁▂▄▃",
"▂▃▃▂",
"▃▄▂▁",
"▄▅▁▂",
"▅▆▂▃",
"▆▇▃▄",
"▇█▄▅",
"█▇▅▆",
"▇▆▆▇",
"▆▅▇█"
],
fish:[
" ",
"> ",
"`> ",
")`> ",
"))`> ",
")))`> ",
"))))`> ",
"<))))`> ",
"><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`> ",
" ><))))`>",
" ><))))`",
" ><))))",
" ><)))",
" ><))",
" ><)",
" ><",
" >"
]
};
return func(type="classic",repeat=1){
if(typeof(type)!="str" or !contains(spinner,type))
type="classic";
type=spinner[type];
var counter=0;
return {
next:func(){
var s="";
for(var i=0;i<repeat;i+=1)
s~=type[counter];
counter+=1;
if(counter>=size(type))
counter=0;
return s;
}
}
};
}();
var show=func(){
print("\ec");
var bars={
"classic ":generate_bar(front:"sharp",back:"point",sep:"bracket",length:40),
"classic2 ":generate_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),
"dots ":generate_bar(front:"solid_circle",back:"hollow_circle",sep:"curve",length:40),
"ticks ":generate_bar(front:"tick",back:"space",sep:"line",length:40),
"deep_shadow":generate_bar(front:"deep_shadow",back:"light_shadow",sep:"line",length:40),
"block ":generate_bar(front:"block",back:"light_shadow",sep:"line",length:40)
};
var key=keys(bars);
for(var i=0;i<40;i+=1){
forindex(var j;key){
var k=key[j];
print("\e["~(j+1)~";1H["~k~"] "~bars[k].bar(i/40));
}
unix.sleep(1/15);
}
var spinners={
"rise ":generate_spinner(type:"rise",repeat:16),
"vertical ":generate_spinner(type:"vertical",repeat:16),
"dot ":generate_spinner(type:"dot",repeat:16),
"dots ":generate_spinner(type:"dots",repeat:16),
"arrow ":generate_spinner(type:"arrow",repeat:16),
"classic ":generate_spinner(type:"classic",repeat:16),
"balls ":generate_spinner(type:"balls",repeat:4),
"dots_wave ":generate_spinner(type:"dots_wave",repeat:2),
"pulse ":generate_spinner(type:"pulse",repeat:1),
"wave ":generate_spinner(type:"wave",repeat:2),
"short_wave ":generate_spinner(type:"short_wave",repeat:4),
"fish ":generate_spinner(type:"fish",repeat:1)
};
var key=keys(spinners);
for(var i=0;i<60;i+=1){
forindex(var j;key){
var k=key[j];
var tmp=spinners[k];
print("\e["~(j+1+size(bars))~";1H["~k~"] |"~tmp.next()~"|");
}
unix.sleep(1/15);
}
print("\n");
}
show();

View File

@ -1,4 +1,4 @@
# lib queue.nas
# queue.nas
# valkmjolnir 2021/3/31
var queue=func()
{

View File

@ -1,5 +1,6 @@
# result.nas
# ValKmjolnir 2021
import("lib.nas");
var ResultTrait={
Ok:func(val){
me.ok=val;

View File

@ -1,4 +1,4 @@
# lib sort.nas
# sort.nas
# valkmjolnir 2021/4/2
var sort=func(vec,left,right,cmp=func(a,b){return a<=b;})
{

View File

@ -1,4 +1,4 @@
# lib stack.nas
# stack.nas
# valkmjolnir 2021/3/31
var stack=func()
{

View File

@ -19,7 +19,9 @@ var lib=[
"stl/file.nas ",
"stl/lib.nas ",
"stl/list.nas ",
"stl/log.nas ",
"stl/module.nas ",
"stl/process_bar.nas ",
"stl/queue.nas ",
"stl/result.nas ",
"stl/sort.nas ",

View File

@ -51,12 +51,12 @@ while(1){
var client=socket.accept(sd);
var recieve_data=socket.recv(client.sd,1024);
if(!recieve_data.size){
println("[",client.ip,"] request connection closed");
println("[",os.time(),"] ",client.ip," request connection closed");
continue;
}
var first=split("\n",recieve_data.str)[0];
var (type,path)=split(" ",first)[0,1];
println("[",client.ip,"] request ",type," [",path,"]");
println("[",os.time(),"] ",client.ip," request ",type," [",path,"]");
if(path=="/")
socket.send(client.sd,httpheader~index~httptail);
elsif(path=="/favicon.ico")

View File

@ -53,7 +53,9 @@ var filechecksum=func(){
"./stl/file.nas ",
"./stl/lib.nas ",
"./stl/list.nas ",
"./stl/log.nas ",
"./stl/module.nas ",
"./stl/process_bar.nas ",
"./stl/queue.nas ",
"./stl/result.nas ",
"./stl/sort.nas ",

View File

@ -45,6 +45,6 @@ func(){
println("CPU occupation(%) : ",cpu_occ>90?"\e[91m":"\e[32m",cpu_occ,"\e[0m");
println("Memory total(GB) : \e[36m",mem.MemTotal/1024/1024,"\e[0m");
println("Memory free(GB) : \e[36m",mem.MemFree/1024/1024,"\e[0m");
println("Memory occupation(%): ",mem_occ>30?"\e[91m":"\e[32m",mem_occ,"\e[0m");
println("Memory occupation(%): ",mem_occ>60?"\e[91m":"\e[32m",mem_occ,"\e[0m");
}
}();