From 99f143115b901d53c83c541d32a50103e493c7c5 Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sun, 10 Apr 2022 17:38:37 +0800 Subject: [PATCH] export sync plans in table format --- src/debugMode.ts | 74 +++++++++++++++++++++++++++++++++++++++++++++--- src/langs | 2 +- src/main.ts | 22 ++++++++++++-- src/settings.ts | 17 +++++++++-- 4 files changed, 105 insertions(+), 10 deletions(-) diff --git a/src/debugMode.ts b/src/debugMode.ts index 38a4f38..a1917b0 100644 --- a/src/debugMode.ts +++ b/src/debugMode.ts @@ -11,14 +11,73 @@ import { DEFAULT_DEBUG_FOLDER, DEFAULT_LOG_HISTORY_FILE_PREFIX, DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX, + FileOrFolderMixedState, } from "./baseTypes"; import { log } from "./moreOnLog"; +const turnSyncPlanToTable = (record: string) => { + const syncPlan: SyncPlanType = JSON.parse(record); + const { ts, tsFmt, remoteType, mixedStates } = syncPlan; + + type allowedHeadersType = keyof FileOrFolderMixedState; + const headers: allowedHeadersType[] = [ + "key", + "remoteEncryptedKey", + "existLocal", + "sizeLocal", + "mtimeLocal", + "deltimeLocal", + "changeLocalMtimeUsingMapping", + "existRemote", + "sizeRemote", + "mtimeRemote", + "deltimeRemote", + "changeRemoteMtimeUsingMapping", + "decision", + "decisionBranch", + ]; + + const lines = [ + `ts: ${ts}${tsFmt !== undefined ? " / " + tsFmt : ""}`, + `remoteType: ${remoteType}`, + `| ${headers.join(" | ")} |`, + `| ${headers.map((x) => "---").join(" | ")} |`, + ]; + for (const [k1, v1] of Object.entries(syncPlan.mixedStates)) { + const k = k1 as string; + const v = v1 as FileOrFolderMixedState; + const singleLine = []; + for (const h of headers) { + const field = v[h]; + if (field === undefined) { + singleLine.push(""); + continue; + } + if ( + h === "mtimeLocal" || + h === "deltimeLocal" || + h === "mtimeRemote" || + h === "deltimeRemote" + ) { + const fmt = v[(h + "Fmt") as allowedHeadersType] as string; + const s = `${field}${fmt !== undefined ? " / " + fmt : ""}`; + singleLine.push(s); + } else { + singleLine.push(field); + } + } + lines.push(`| ${singleLine.join(" | ")} |`); + } + + return lines.join("\n"); +}; + export const exportVaultSyncPlansToFiles = async ( db: InternalDBs, vault: Vault, - vaultRandomID: string + vaultRandomID: string, + toFormat: "table" | "json" = "json" ) => { log.info("exporting"); await mkdirpInVault(DEFAULT_DEBUG_FOLDER, vault); @@ -27,9 +86,16 @@ export const exportVaultSyncPlansToFiles = async ( if (records.length === 0) { md = "No sync plans history found"; } else { - md = - "Sync plans found:\n\n" + - records.map((x) => "```json\n" + x + "\n```\n").join("\n"); + if (toFormat === "json") { + md = + "Sync plans found:\n\n" + + records.map((x) => "```json\n" + x + "\n```\n").join("\n"); + } else if (toFormat === "table") { + md = + "Sync plans found:\n\n" + records.map(turnSyncPlanToTable).join("\n\n"); + } else { + const _: never = toFormat; + } } const ts = Date.now(); const filePath = `${DEFAULT_DEBUG_FOLDER}${DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX}${ts}.md`; diff --git a/src/langs b/src/langs index 6bfbaa3..470fe14 160000 --- a/src/langs +++ b/src/langs @@ -1 +1 @@ -Subproject commit 6bfbaa3c1b5c54885b300d6fb93f9c98d50f54e4 +Subproject commit 470fe141ef48237e81eb84cb466f9bc7f374274c diff --git a/src/main.ts b/src/main.ts index cd1ceef..0adc8f6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -676,14 +676,30 @@ export default class RemotelySavePlugin extends Plugin { }); this.addCommand({ - id: "export-sync-plans", - name: t("command_exportsyncplans"), + id: "export-sync-plans-json", + name: t("command_exportsyncplans_json"), icon: iconNameLogs, callback: async () => { await exportVaultSyncPlansToFiles( this.db, this.app.vault, - this.vaultRandomID + this.vaultRandomID, + "json" + ); + new Notice(t("settings_syncplans_notice")); + }, + }); + + this.addCommand({ + id: "export-sync-plans-table", + name: t("command_exportsyncplans_table"), + icon: iconNameLogs, + callback: async () => { + await exportVaultSyncPlansToFiles( + this.db, + this.app.vault, + this.vaultRandomID, + "table" ); new Notice(t("settings_syncplans_notice")); }, diff --git a/src/settings.ts b/src/settings.ts index 7e1bab4..8a5819d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1687,12 +1687,25 @@ export class RemotelySaveSettingTab extends PluginSettingTab { .setName(t("settings_syncplans")) .setDesc(t("settings_syncplans_desc")) .addButton(async (button) => { - button.setButtonText(t("settings_syncplans_button")); + button.setButtonText(t("settings_syncplans_button_json")); button.onClick(async () => { await exportVaultSyncPlansToFiles( this.plugin.db, this.app.vault, - this.plugin.vaultRandomID + this.plugin.vaultRandomID, + "json" + ); + new Notice(t("settings_syncplans_notice")); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_table")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + "table" ); new Notice(t("settings_syncplans_notice")); });