🚀 add lib function exit() and add test file watchdog.nas to run the nasal file when it is changed.

This commit is contained in:
ValKmjolnir
2022-08-27 18:12:32 +08:00
parent 0e578b3e21
commit 8293f85c5b
6 changed files with 70 additions and 6 deletions
+47
View File
@@ -0,0 +1,47 @@
var os_time=func(){
return "[\e[33m"~os.time()~"\e[0m] ";
}
var err_hd=func(){
return "[\e[91merror\e[0m] ";
}
var watching_hd=func(){
return "[\e[96mwatching\e[0m] ";
}
var modified_hd=func(){
return "[\e[92mmodified\e[0m] ";
}
var usage=func(){
println(os_time(),"[\e[92musage\e[0m] nasal watchdog.nas <filename>");
}
var argv=runtime.argv();
if(size(argv)!=1){
println(os_time(),err_hd(),"need correct file path to watch");
usage();
exit(-1);
}
var filename=argv[0];
if(!io.exists(filename)){
println(os_time(),err_hd(),"file <",filename,"> does not exist");
usage();
exit(-1);
}
var modified_time=fstat(filename).st_mtime;
println(os_time(),watching_hd(),filename," ..");
while(1){
unix.sleep(1);
if(!io.exists(filename)){
println(os_time(),err_hd(),"file <",filename,"> does not exist");
break;
}
var latest_modified_time=fstat(filename).st_mtime;
if(latest_modified_time!=modified_time){
modified_time=latest_modified_time;
println(os_time(),modified_hd(),filename);
var ret=system((os.platform()=="windows"?"":"./")~"nasal "~filename);
if(ret!=0){
println(os_time(),err_hd(),"process returned value ",ret);
}
}
}