Skip to content

Commit

Permalink
#57 updates to notes file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
pajoma committed May 17, 2020
1 parent 2bda024 commit 04ecee9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 55 deletions.
119 changes: 74 additions & 45 deletions src/actions/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import { ScopedTemplate, JournalPageType } from '../ext/conf';

export interface FileEntry {
path: string;
lastUpdate: number;
type: JournalPageType;
name: string;
update_at: number;
created_at: number;
type: JournalPageType;
}

/**
Expand All @@ -40,87 +42,114 @@ export class Reader {
constructor(public ctrl: J.Util.Ctrl) {
}

private previousEntries: Array<FileEntry> = [];
private previousEntries: Array<FileEntry> = [];


/**
* Loads previous entries.
* Loads previous entries.
*
* Update: ignore threshold
*
* @returns {Q.Promise<[string]>}
* @memberof Reader
* @deprecated
* @deprecated aargh, why?
*/
public getPreviouslyAccessedFiles(thresholdInMs: number): Q.Promise<Array<FileEntry>> {
public getPreviouslyAccessedFiles(thresholdInMs: number, callback: Function, picker: any, type: JournalPageType) {
this.ctrl.logger.trace("Entering getPreviousJournalFiles() in actions/reader.ts");

const deferred: Q.Deferred<Array<FileEntry>> = Q.defer<FileEntry[]>();


deferred.resolve(this.previousEntries);

/*
deferred.resolve(this.previousEntries.map((f: FileEntry) => {
return f.path;
})); */

// go into base directory, find all files changed within the last 40 days
// for each file, check if it is an entry, a note or an attachement
let base: string = this.ctrl.config.getBasePath();
this.walkDir(base, thresholdInMs, (entry: FileEntry) => {
if(this.previousEntries.findIndex(e => e.path.startsWith(entry.path)) == -1) {
this.inferType(entry);
this.previousEntries.push(entry);
}
});
Q.fcall(() => {
let base: string = this.ctrl.config.getBasePath();
this.walkDir(base, thresholdInMs, (entry: FileEntry) => {
if (this.previousEntries.findIndex(e => e.path.startsWith(entry.path)) == -1) {
this.inferType(entry);
this.previousEntries.push(entry);
}

// this adds the item to the quickpick list of vscode (the addItem Function)
callback(entry, picker, type);

});
});


return deferred.promise;
}

/**
* Tries to infer the file type from the path by matching against the configured patterns
* @param entry
*/
inferType(entry: FileEntry) {
const fileName = entry.path.substring(entry.path.lastIndexOf(Path.sep)+1, entry.path.lastIndexOf('.'));

if(! entry.path.endsWith(this.ctrl.config.getFileExtension())) {
entry.type = JournalPageType.ATTACHEMENT; // any attachement
} else
const fileName = entry.path.substring(entry.path.lastIndexOf(Path.sep) + 1, entry.path.lastIndexOf('.'));

// this is getting out of hand if we need to infer it by scanning the patterns from the settings.
// We keep it simple: if the filename contains only digits and special chars, we assume it
// is a journal entry. Everything else is a journal note.
if(fileName.match(/^[\d|\-|_]+$/gm)) {
entry.type = JournalPageType.ENTRY; // any entry
} else {
entry.type = JournalPageType.NOTE; // anything else is a note
}
if (!entry.path.endsWith(this.ctrl.config.getFileExtension())) {
entry.type = JournalPageType.ATTACHEMENT; // any attachement
} else

// this is getting out of hand if we need to infer it by scanning the patterns from the settings.
// We keep it simple: if the filename contains only digits and special chars, we assume it
// is a journal entry. Everything else is a journal note.
if (fileName.match(/^[\d|\-|_]+$/gm)) {
entry.type = JournalPageType.ENTRY; // any entry
} else {
entry.type = JournalPageType.NOTE; // anything else is a note
}

console.log(fileName, " - ", entry.type);
}



/**
* Scans journal directory and scans for notes
*
* Update: Removed age threshold, take everything
* Update: switched to async with readdir
*
* See https://medium.com/@allenhwkim/nodejs-walk-directory-f30a2d8f038f
* @param dir
* @param callback
*/
private async walkDir(dir: string, thresholdInMs: number, callback: Function) {
fs.readdirSync(dir).forEach(f => {
let dirPath = Path.join(dir, f);
let stats: fs.Stats = fs.statSync(dirPath);

// check last access
if( (stats.atimeMs > thresholdInMs) && (stats.isDirectory())) {
this.walkDir(dirPath, thresholdInMs, callback);
} else if(stats.mtimeMs > thresholdInMs) {
callback({
path: Path.join(dir, f),
lastUpdate: stats.mtimeMs
});
}
});
};
fs.readdir(dir, (err, files) => {
// we ignore errors here

files.forEach(f => {
let dirPath = Path.join(dir, f);
let stats: fs.Stats = fs.statSync(dirPath);

// check last access (deprecated: removed threshold)
/*
if( (stats.atimeMs > thresholdInMs) && (stats.isDirectory())) {
this.walkDir(dirPath, thresholdInMs, callback);
} else if(stats.mtimeMs > thresholdInMs) {
callback({
path: Path.join(dir, f),
lastUpdate: stats.mtimeMs
});
} */

if (stats.isDirectory()) {
this.walkDir(dirPath, thresholdInMs, callback);
} else {
callback({
path: Path.join(dir, f),
name: f,
updated_at: stats.mtimeMs,
created_at: stats.ctime
});
}
});
});
}

public async checkDirectory(d: Date, entries: string[]) {
await this.ctrl.config.getNotesPathPattern(d)
Expand Down
2 changes: 1 addition & 1 deletion src/ext/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export class Configuration {
* Helper Method, threshold (maximal age) of files shown in the quick picker
*/
getInputTimeThreshold(): number {
let offset = 0;
let offset = 60;
let d: Date = new Date();
d.setDate(d.getDate() + --offset);
return d.getTime();
Expand Down
49 changes: 40 additions & 9 deletions src/ext/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ interface DecoratedQuickPickItem extends vscode.QuickPickItem {
* Anything which extends Visual Studio Code goes here
*
*/

export class VSCode {
constructor(public ctrl: J.Util.Ctrl) {

}



/**
*
*/
public getUserInputWithValidation(): Q.Promise<J.Model.Input> {
let deferred: Q.Deferred<J.Model.Input> = Q.defer<J.Model.Input>();

Expand Down Expand Up @@ -113,8 +117,6 @@ export class VSCode {
deferred.resolve(selected.parsedInput as J.Model.Input);

} else if (!isUndefined(selected.pickItem) && selected.pickItem == JournalPageType.ENTRY) {
// deferred.resolve(new J.Model.Input(5));

this.pickItem(JournalPageType.ENTRY).then(selected => {
deferred.resolve(selected);
});
Expand All @@ -140,6 +142,25 @@ export class VSCode {

return deferred.promise;
};


/**
* Callback function for filewalker to add an item to our quickpick list
*
* @param fe
*/
public addItem(fe: FileEntry, input: vscode.QuickPick<DecoratedQuickPickItem>, type: JournalPageType) {
if(fe.type != type) return;
console.log("adding "+fe.name+ "for type"+type.toString());

let item: DecoratedQuickPickItem = {
label: fe.name,
description: " Created: " +moment(fe.created_at).format("LL")+" Updated: " +moment(fe.update_at).format("LL")
};

input.items = [item].concat(input.items);
}

/**
*
* @param type
Expand All @@ -156,23 +177,33 @@ export class VSCode {
let selected: DecoratedQuickPickItem | undefined;

input.show();

this.ctrl.reader.getPreviouslyAccessedFiles(this.ctrl.config.getInputTimeThreshold(), this.addItem, input, type);
/*
Update: populating the list is async now using a callback, which means we lose the option of sorting the list
this.ctrl.reader.getPreviouslyAccessedFiles(this.ctrl.config.getInputTimeThreshold())
this.ctrl.reader.getPreviouslyAccessedFiles(this.ctrl.config.getInputTimeThreshold(), this.addItem)
.then((values: FileEntry[]) => {
values.sort((a, b) => (a.lastUpdate - b.lastUpdate))
values.sort((a, b) => (a.update_at - b.update_at))
.filter((fe: FileEntry) => fe.type == type)
.map<vscode.QuickPickItem>(fe => {
// strip base path
// TODO: denormalize (see #57)
// let label = fe.path.substring(base.length + 1, fe.path.length);
let label = fe.name;
return {
label: fe.path.substring(base.length + 1, fe.path.length),
description: moment(fe.lastUpdate).format("LL")
label: label,
description: " Created: " +moment(fe.created_at).format("LL")+" Updated: " +moment(fe.update_at).format("LL")
}
}).forEach(item => {
input.items = [item].concat(input.items);
});
});

*/
input.onDidChangeSelection(sel => {
selected = sel[0];
}, disposables);
Expand Down

0 comments on commit 04ecee9

Please sign in to comment.