Skip to content

Commit

Permalink
feat: add more granular customization of {{date}} commit message pl…
Browse files Browse the repository at this point in the history
…aceholder
  • Loading branch information
denolehov committed Oct 31, 2020
1 parent 7b37a6e commit 7063f5a
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
import simpleGit, { CheckRepoActions, SimpleGit } from "simple-git";
import { prependListener } from "cluster";
import { platform } from "os";

enum PluginState {
idle,
Expand Down Expand Up @@ -138,9 +140,8 @@ export default class ObsidianGit extends Plugin {

async commit(): Promise<void> {
this.setState(PluginState.commit);
let commitMessage = this.settings.commitMessage.replace(
"{{date}}",
new Date().toISOString()
let commitMessage = this.formatCommitMessage(
this.settings.commitMessage
);
await this.git.commit(commitMessage);
}
Expand Down Expand Up @@ -200,6 +201,14 @@ export default class ObsidianGit extends Plugin {
return false;
}

formatCommitMessage(template: string): string {
let moment = (window as any).moment;
return template.replace(
"{{date}}",
moment().format(this.settings.commitDateFormat)
);
}

maybeNotice(text: string): void {
if (!this.settings.disablePopups) {
new Notice(text);
Expand All @@ -212,6 +221,7 @@ export default class ObsidianGit extends Plugin {

class ObsidianGitSettings {
commitMessage: string = "vault backup: {{date}}";
commitDateFormat: string = "YYYY-MM-DD HH:mm:ss";
autoSaveInterval: number = 0;
autoPullOnBoot: boolean = false;
disablePopups: boolean = false;
Expand Down Expand Up @@ -276,6 +286,30 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("{{date}} placeholder format")
.setDesc('Specify custom date format. E.g. "YYYY-MM-DD HH:mm:ss"')
.addText((text) =>
text
.setPlaceholder(plugin.settings.commitDateFormat)
.setValue(plugin.settings.commitDateFormat)
.onChange(async (value) => {
plugin.settings.commitDateFormat = value;
await plugin.saveData(plugin.settings);
})
);

new Setting(containerEl)
.setName("Preview commit message")
.addButton((button) =>
button.setButtonText("Preview").onClick(() => {
let commitMessagePreview = plugin.formatCommitMessage(
plugin.settings.commitMessage
);
new Notice(`${commitMessagePreview}`);
})
);

new Setting(containerEl)
.setName("Current branch")
.setDesc("Switch to a different branch")
Expand Down

0 comments on commit 7063f5a

Please sign in to comment.