changed cli tool

- create new note if arg is 'new'
- can edit several notes
- type q to quit
- refactor
This commit is contained in:
quenousimporte 2023-09-01 12:58:34 +02:00
parent 3be1d05859
commit f1b7420a50
1 changed files with 104 additions and 76 deletions

View File

@ -11,9 +11,18 @@ var rl = readline.createInterface({
var settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" })); var settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" }));
var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" }); var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" });
var filter = process.argv.length > 2 ? process.argv[2] : ""; var filter = "";
var intervalid = null; var intervalid = null;
var notes = null; var notes = null;
var currentnote = null;
function timestamp()
{
var utc = new Date();
var loc = new Date(utc - utc.getTimezoneOffset() * 60 * 1000);
return loc.toISOString().replace("T", " ").replace(/\..*/, "").replace(/:/g, ".");
}
function simplifystring(str) function simplifystring(str)
{ {
@ -49,6 +58,61 @@ function filteredlist()
.filter(n => simplifystring(n.title).includes(simplifystring(filter))); .filter(n => simplifystring(n.title).includes(simplifystring(filter)));
} }
async function saveifneeded()
{
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
if (currentnote.content != newcontent)
{
currentnote.content = newcontent;
notes.splice(notes.indexOf(currentnote), 1);
notes.unshift(currentnote);
console.log("sending data file to server...");
var encrypted = await encrypt(JSON.stringify(notes));
axios.post(`${settings.url}/handler.php`,
{
action: "push",
password: settings.password,
data: encrypted
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
}).then(res => {
console.log("...done.");
});
}
else
{
console.log("no change.");
}
}
function editnote(index)
{
currentnote = filteredlist()[index];
if (currentnote)
{
// todo: use title instead? To put in data folder?
fs.writeFileSync("note.md", currentnote.content);
cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr)
{
clearInterval(intervalid);
saveifneeded();
});
intervalid = setInterval(saveifneeded, 10000);
}
else
{
console.log("No note found.");
}
}
// Run part
axios.post(`${settings.url}/handler.php`, axios.post(`${settings.url}/handler.php`,
{ {
action: "fetch", action: "fetch",
@ -64,86 +128,50 @@ axios.post(`${settings.url}/handler.php`,
{ {
notes = JSON.parse(await decrypt(res.data)); notes = JSON.parse(await decrypt(res.data));
filteredlist() if (process.argv.length > 2 && process.argv[2] === "new")
.every( (note, i) =>
{ {
console.log(`[${i}] ${note.title}`) var title = timestamp();
return Boolean(filter) || i < settings.maxcountifnofilter; currentnote = {
}); title: title,
content: ""
// todo: open if only one match. quit if no match }
rl.prompt(); notes.unshift(currentnote);
rl.on("line", async function (line) console.log("Creating new note: " + title);
editnote(0);
}
else
{ {
var note = filteredlist()[line]; if (process.argv.length > 2)
// todo: use title instead? To put in data folder?
fs.writeFileSync("note.md", note.content);
cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr)
{ {
clearInterval(intervalid); filter = process.argv[2];
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); }
if (note.content != newcontent)
var matchcount = filteredlist().length;
if (matchcount == 1)
{
editnote(0);
}
else if (matchcount > 1)
{
console.log("Select a note or type 'q' to quit:");
filteredlist()
.every( (note, i) =>
{ {
note.content = newcontent; console.log(`[${i}] ${note.title}`)
return Boolean(filter) || i < settings.maxcountifnofilter;
notes.splice(notes.indexOf(note), 1); });
notes.unshift(note); rl.prompt();
rl.on("line", async function (line)
console.log("sending data file to server...");
var encrypted = await encrypt(JSON.stringify(notes));
axios.post(`${settings.url}/handler.php`,
{
action: "push",
password: settings.password,
data: encrypted
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
}).then(res => {
console.log("done.");
});
}
else
{ {
console.log("no change."); if (line == "q")
}
})
intervalid = setInterval(async function()
{
//todo: refactor "save"
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
if (note.content != newcontent)
{ {
note.content = newcontent; rl.close();
notes.splice(notes.indexOf(note), 1);
notes.unshift(note);
console.log("sending data file to server...");
var encrypted = await encrypt(JSON.stringify(notes));
axios.post(`${settings.url}/handler.php`,
{
action: "push",
password: settings.password,
data: encrypted
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
}).then(res => {
console.log("done.");
});
} }
}, 10000); else
{
rl.close(); editnote(line);
}); }
});
}
}
}); });