diff --git a/extension.js b/extension.js index eca878c..f4b5c4a 100644 --- a/extension.js +++ b/extension.js @@ -1,6 +1,8 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below -var vscode = require('vscode'); +var vscode = require('vscode'); +var fs = require('fs'); +var dateFormat = require('dateformat'); // this method is called when your extension is activated // your extension is activated the very first time the command is executed @@ -14,7 +16,7 @@ function activate(context) { context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteInsertDate', function () { var editor = vscode.window.activeTextEditor; if (!editor) { - console.error("no active editor"); + console.error("next-note: no active editor"); return; }; //editor.document.getWordRangeAtPosition @@ -22,14 +24,70 @@ function activate(context) { if (editor.document.lineCount > cursorEnd.line+1) { cursorEnd.line += 1; } - var d = new Date(); editor.edit(editBuilder => { - editBuilder.insert(cursorEnd, "## "+ d.toString() +"\n"); + editBuilder.insert(cursorEnd, dateLine()); + }); + })); + + + context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenCurrentWeek', function () { + var basedir = process.env["NOTEDIR"]; + + if (!basedir) { + basedir = process.env["HOME"]+"/Notes"; + console.log("next-note: no basedir. Using "+basedir); + console.log("next-note: set NOTEDIR to use a diferent basedir"); + } + try { + fs.statSync(basedir); + } catch(e) { + try { + fs.mkdirSync(basedir); + } catch(e) { + vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); + return + } + } + + var notepath = basedir +"/"+ currentWeekFilename(); + fs.access(notepath, fs.constants.R_OK | fs.constants.W_OK, (err) => { + if (err) { + try { + fs.writeFileSync(notepath, dateLine()); + } catch (e) { + vscode.window.showErrorMessage("next-note: failed to create "+ notepath); + console.error(e); + return; + } + } + }); + vscode.workspace.openTextDocument(notepath).then(doc => { + vscode.window.showTextDocument(doc); + vscode.window.activeTextEditor.selection.active.line += 1; }); })); } exports.activate = activate; +function dateLine() { + var d = new Date(); + return "## "+ dateFormat(d,"ddd mmm dd HH:MM:ss Z yyyy") +"\n"; +} + +function currentWeekFilename() { + return "Tasks-"+currentWeek()+".md"; +} + +function currentWeek() { + var d = new Date(); + var beginDate = new Date(); + beginDate.setDate(d.getDate()-d.getDay()); // This will be sunday of current current week + d.setDate(beginDate.getDate()+6); // this is a week from that sunday + + return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); + //return String(beginDate.getFullYear())+String(beginDate.getMonth())+String(beginDate.getDate()) +"-"+ String(d.getFullYear())+String(d.getMonth())+String(d.getDate()); +} + // this method is called when your extension is deactivated function deactivate() { console.log("next-note: deactivated") diff --git a/package.json b/package.json index 9d614b4..3a1698b 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,15 @@ "Other" ], "activationEvents": [ - "onCommand:extension.sayHello", - "onCommand:extension.nextNoteInsertDate" - + "onCommand:extension.nextNoteInsertDate", + "onCommand:extension.nextNoteOpenCurrentWeek" ], "main": "./extension", "contributes": { "commands": [ { - "command": "extension.sayHello", - "title": "Hello World" + "command": "extension.nextNoteOpenCurrentWeek", + "title": "NextNote: Open Current Week" }, { "command": "extension.nextNoteInsertDate", @@ -36,6 +35,9 @@ "postinstall": "node ./node_modules/vscode/bin/install", "test": "node ./node_modules/vscode/bin/test" }, + "dependencies": { + "dateformat": "^2.0.0" + }, "devDependencies": { "typescript": "^2.0.3", "vscode": "^1.0.0",