todo4/nice.js

331 lines
8.8 KiB
JavaScript
Raw Normal View History

2024-07-26 20:02:43 -04:00
module.exports=exports;
var todo=require('./todo');
var {pullexposedtasks,randomtasks,edittitle,searchtasks,pullmirrorsuper,pullmirrortask,getjournal,decayranks,completetask,pulltasks,addtask,removetask,ranktasks,removetasks,addmany}=todo;
Object.keys(todo).forEach(k=>exports[k]=todo[k]);
var prompt=require('prompt-sync')();
var fs=require('fs');
var async=require('async');
function noop() {}
function exports(user) {
var retr={};
retr.exports=exports;
retr.random=random;
retr.rand=random;
function random(count=5) {
randomtasks(user,count,(err,res)=>{
console.log();
res.forEach(i=>console.log(i.id,i.title));
});
}
retr.add=add;
function add() {
if(arguments.length==0) {
var list=[];
function ask() {
var line=prompt(':');
if(line) {
list.push(line);
ask();
}
else { //Add list
addmany(user,list,console.log);
}
//if(line) addtask(user,line,ask);
}
return ask();
}
Array.prototype.forEach.bind(arguments)(item=>{
if(typeof(item)==='string') addtask(user,item,noop);
else { //Asuming list
item.forEach(item=>addtask(user,item,noop));
}
});
}
retr.remove=remove;
function remove() {
Array.prototype.forEach.bind(arguments)(item=>{
if(typeof(item)==='number') {
if(item<=4) {
var verify = prompt('Verify: Y/n');
if(verify[0]=='n'||verify[0]=='N') return;
}
removetask(user,item,noop);
}
else { //Asuming list
item.forEach(item=>removetask(user,item,noop));
}
});
}
function printtasks(tasks,stream) {
pastpull=tasks;
try {
tasks.sort(()=>Math.random()-0.5);
stream.write('\n');
//console.log({tasks});
tasks.forEach((t,i)=>{
//console.log(t.id+':',i,t.title);
stream.write(t.id+': '+i+' '+t.title+'\n');
});
}
catch(e) {
console.log({user,tasks});
console.log(e);
}
}
retr.pullexposed = pullexposed;
function pullexposed(count=4,stream=process.stdout) {
pullexposedtasks(user,count,(err,tasks)=>{
if(err) return console.log(err,tasks);
printtasks(tasks,stream);
});
}
var pastpull=[];
retr.pull=pull;
retr.p = pull;
function pull(count=4,stream=process.stdout) {
pulltasks(user,count,(err,tasks)=>{
if(err) return console.log(err,tasks);
printtasks(tasks,stream);
});
}
retr.r=r;
var pulllog=fs.createWriteStream(__dirname+'/data/logs/pull.log',{flags:'a'});
//function r() {
// if(arguments.length===1 && typeof(arguments[0])==='string') {
// return retr.done(arguments[0]);
// }
// var expanded=[];
// Array.prototype.forEach.bind(arguments)(item=>{
// if(typeof(item)==='number') expanded.push(item);
// else { //Asuming list
// expanded=expanded.concat(item);
// }
// });
// var toptitle = pastpull[expanded[0]].title;
// expanded=expanded.map(i=>pastpull[i].id);
// //console.log(expanded)
// ranktasks(user,expanded,noop);
// pulllog.write(toptitle+'\n');
// pastrank=expanded;
//}
function r() {
if (arguments.length === 1 && typeof(arguments[0]) === 'string') {
return retr.done(arguments[0]);
}
var expanded = [];
Array.prototype.forEach.call(arguments, item => {
if (typeof(item) === 'number') {
expanded.push([item]); // Wrap single numbers in an array
} else if (Array.isArray(item)) {
expanded.push(item); // Use array as is
}
});
var toptitle = pastpull[expanded[0][0]].title; // Use the first element of the first array to get the title
expanded = expanded.map(group => group.map(i => pastpull[i].id)); // Replace temp ids with longhand ids
ranktasks(user, expanded, noop);
pulllog.write(toptitle + '\n');
pastrank = expanded.flat();
}
retr.addfromfile=addfromfile;
function addfromfile(path='insert') {
fs.readFile(path,(err,buff)=>{
if(err) return console.log(err);
addmany(user,buff.toString().split('\n').filter(i=>i.length),console.log);
});
}
retr.done=done;
function done(taskid) {
if(taskid<=4) {
var verify = prompt('Verify: Y/n');
if(verify[0]=='n'||verify[0]=='N') return;
}
completetask(user,taskid,err=>{
if(err) return console.log(err);
decayranks(user,1-5/300,noop);
//if(typeof(taskid)=='number') setTimeout(pm,100);
});
}
retr.d=d;
function d() {
done(pastrank[0]);
}
var pastrank=[];
retr.rank=rank;
//function rank() {
// var expanded=[];
// Array.prototype.forEach.bind(arguments)(item=>{
// if(typeof(item)==='number') expanded.push(item);
// else { //Asuming list
// expanded=expanded.concat(item);
// }
// });
// pastrank=expanded;
// ranktasks(user,expanded,noop);
//}
function rank() {
var expanded = [];
if (arguments.length === 1 && Array.isArray(arguments[0])) {
expanded = arguments[0]; // Treat single array argument as the complete taskGroups
} else {
// Treat each argument as an element to be passed to ranktasks
Array.prototype.forEach.call(arguments, item => {
if (typeof(item) === 'number') {
expanded.push([item]); // Wrap single numbers in an array
} else {
expanded.push(item); // Already an array, use as is
}
});
}
pastrank = expanded.flat();
ranktasks(user, expanded, noop);
}
retr.pullmirror=pullmirror;
retr.pm=pullmirror;
retr.pullmirrormonth=pullmirrormonth;
retr.pullmirrorweek=pullmirrorweek;
retr.pullmirrorday=pullmirrorday;
function pullmirror() {
//async.parallel([pullmirrormonth,pullmirrorweek,pullmirrorday],(err,res)=>{
async.map([doublepullmirrormonth,pullmirrormonth,doublepullmirrorweek,pullmirrorweek,doublepullmirrorday,pullmirrorday],(func,cb)=>{
func((err,res)=>{
if(err == 'Journal missing - skiplimit reached') return cb();
cb(err,res);
});
},(err,res)=>{
if(err) return console.log(err);
res=res.filter(r=>r);
res.forEach(item=>{
if(!item) return console.log('missing');
console.log((new Date(item.time)).toLocaleString(),item.title,item.id||'');
});
});
}
function pullmirrorday(cb) {
pullmirrortask(user,cb);
}
function doublepullmirrorday(cb) {
getjournal(user,0,(err,journal)=>{
if(err) return console.log(err);
if(!journal.list) return cb();
pullmirrorsuper(user,1,journal.list.length*2,5,cb);
});
}
retr.lookback=lookback;
function lookback(days) {
getjournal(user,days,(err,journal)=>{
if(err) return console.log(err);
if(!journal.list) return console.log('Journal missing');
journal.list.forEach(item=>{
console.log((new Date(item.time)).toLocaleString(),item.title,item.id||'');
});
});
}
retr.pullmirrorweek=pullmirrorweek;
function pullmirrorweek(cb) {
var day=(new Date()).getDay()||7;
var pending=day;
var lookcount=0;
for(var i=0;i<day;++i) {
getjournal(user,i,(err,journal)=>{
--pending;
if(journal && journal.list) lookcount+=journal.list.length;
if(!pending) {
pullmirrorsuper(user,day,lookcount,5,cb);
}
});
}
}
retr.doublepullmirrorweek=doublepullmirrorweek;
function doublepullmirrorweek(cb) {
var day=(new Date()).getDay()||7;
var pending=day;
var lookcount=0;
for(var i=0;i<day;++i) {
getjournal(user,i,(err,journal)=>{
--pending;
if(journal && journal.list) lookcount+=journal.list.length;
if(!pending) {
pullmirrorsuper(user,day,lookcount*2,5,cb);
}
});
}
}
retr.pullmirrormonth=pullmirrormonth;
function pullmirrormonth(cb) {
var day=(new Date()).getDate()||30;
var pending=day;
var lookcount=0;
for(var i=0;i<day;++i) {
getjournal(user,i,(err,journal)=>{
--pending;
if(journal && journal.list) lookcount+=journal.list.length;
if(!pending) {
pullmirrorsuper(user,day,lookcount,15,cb);
}
});
}
}
retr.doublepullmirrormonth=doublepullmirrormonth;
function doublepullmirrormonth(cb) {
var day=(new Date()).getDate()||30;
var pending=day;
var lookcount=0;
for(var i=0;i<day;++i) {
getjournal(user,i,(err,journal)=>{
--pending;
if(journal && journal.list) lookcount+=journal.list.length;
if(!pending) {
pullmirrorsuper(user,day,lookcount*2,15,cb);
}
});
}
}
function lookup(ref) {
if(!ref) return;
if(typeof(ref)=='number') return lookupid(ref);
else return lookupname(ref);
}
retr.search=search;
function search(titlefrag) {
var args=[];
for(var i=0;arguments[i];++i) {
args=args.concat(arguments[i]);
}
searchtasks(user,args,console.log);
}
retr.searchres=searchres;
function searchres(titlefrag) {
var args=[];
for(var i=0;arguments[i];++i) {
args=args.concat(arguments[i]);
}
return new Promise((resolve,reject)=>{
searchtasks(user,args,(err,results)=>{
if(err) return reject(err);
resolve(results);
});
});
}
retr.edit = edit;
function edit(id,title) {
edittitle(user,id,title);
}
/*
http.createServer((req,res)=>{
if(req.url=='/pull') {
res.writeHead(200,{'Content-type':'text/plain'});
pulltasks(user,5,(err,tasks)=>{
tasks.forEach((t,i)=>{
//console.log(t.id+':',i,t.title);
res.write(t.id+': '+i+' '+t.title+'\n');
});
res.end();
});
}
}).listen(7000);
*/
return retr;
}