Skip to content

Commit

Permalink
improved notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlon154 committed Jul 20, 2024
1 parent 400c06d commit 3109c80
Showing 1 changed file with 83 additions and 31 deletions.
114 changes: 83 additions & 31 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ObsidianFlashcard extends Plugin {

this.addCommand({
id: 'generate-flashcards-for-tag',
name: 'Generate for files with tag',
name: 'Generate flashcards for files with tag',
checkCallback: (checking: boolean) => {
if (!checking) {
this.generateCardsForTag();
Expand Down Expand Up @@ -81,34 +81,86 @@ export default class ObsidianFlashcard extends Plugin {
})
}

private async generateCardsForTag() {
if (this.syncInProgress) {
return;
}
this.syncInProgress = true;
new Notice("Start complete Anki sync", noticeTimeout)
const flashcardsTag = "#" + (this.settings.flashcardsTag as string);
const filesWithTag = this.app.vault.getFiles().filter(file => {
const fileTags = this.app.metadataCache.getFileCache(file)?.tags || [];
const tagStrings = fileTags.map(tag => tag.tag);

private padMessage(message: string): string {
const maxLength = 100;
const lines = message.split('\n');
return lines.map(line => line.padEnd(maxLength)).join('\n');
}

private async generateCardsForTag() {
if (this.syncInProgress) {
return;
}
this.syncInProgress = true;

const flashcardsTag = "#" + (this.settings.flashcardsTag as string);
const filesWithTag = this.app.vault.getFiles().filter(file => {
const fileTags = this.app.metadataCache.getFileCache(file)?.tags || [];
const tagStrings = fileTags.map(tag => tag.tag);
return tagStrings.includes(flashcardsTag);
});

let noteNumber = 1;

for (const file of filesWithTag) {
try {
const res = await this.cardsService.execute(file);
new Notice(`Note ${noteNumber++} of ${filesWithTag.length}: \n${res.join('\n')}`, noticeTimeout);
console.log(res);
} catch (err) {
Error(err);
}
}

this.syncInProgress = false;
new Notice("Finished complete Anki sync", noticeTimeout)
}

}
});

let noteNumber = 1;
const totalNotes = filesWithTag.length;
let syncNotice = new Notice(this.padMessage(`Syncing Anki cards: 0/${totalNotes}`), 0);

let totalUpdated = 0;
let totalAdded = 0;
let totalRemoved = 0;
let errors: string[] = [];

for (const file of filesWithTag) {
try {
const res = await this.cardsService.execute(file);
const progressMessage = `Syncing Anki cards: ${noteNumber}/${totalNotes}\nLast synced: ${file.name}`;
syncNotice.setMessage(this.padMessage(progressMessage));
console.log(res);

// Count updates, additions, and removals based on res content
res.forEach(message => {
if (message.includes("Updated successfully")) {
const match = message.match(/Updated successfully (\d+)\/(\d+) cards/);
if (match) {
totalUpdated += parseInt(match[1]);
}
} else if (message.includes("Inserted successfully")) {
const match = message.match(/Inserted successfully (\d+)\/(\d+) cards/);
if (match) {
totalAdded += parseInt(match[1]);
}
} else if (message.includes("Deleted successfully")) {
const match = message.match(/Deleted successfully (\d+)\/(\d+) cards/);
if (match) {
totalRemoved += parseInt(match[1]);
}
} else if (message.startsWith("Error:")) {
errors.push(`${file.name}: ${message}`);
}
});

noteNumber++;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : String(err);
errors.push(`${file.name}: ${errorMessage}`);
}
}

this.syncInProgress = false;
let finalMessage = `Finished Anki sync: ${totalNotes} notes processed\n` +
`Updated: ${totalUpdated}\n` +
`Added: ${totalAdded}\n` +
`Removed: ${totalRemoved}`;

if (errors.length > 0) {
finalMessage += `\n\nErrors occurred in ${errors.length} files:`;
errors.forEach((error, index) => {
finalMessage += `\n${index + 1}. ${error}`;
});
}

syncNotice.setMessage(finalMessage);
console.log(finalMessage);
setTimeout(() => syncNotice.hide(), 12000);
}

}

0 comments on commit 3109c80

Please sign in to comment.