Skip to content

Commit

Permalink
added a setting to make duration displays less fine-grained
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
Ellpeck committed May 23, 2023
1 parent 175ef1e commit b5e768d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 71 deletions.
11 changes: 11 additions & 0 deletions src/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {
});
});

new Setting(this.containerEl)
.setName("Fine-Grained Durations")
.setDesc("Whether durations should include days, months and years. If this is disabled, additional time units will be displayed as part of the hours.")
.addToggle(t => {
t.setValue(this.plugin.settings.fineGrainedDurations);
t.onChange(async v => {
this.plugin.settings.fineGrainedDurations = v;
await this.plugin.saveSettings();
});
});

this.containerEl.createEl("hr");
this.containerEl.createEl("p", {text: "If you like this plugin and want to support its development, you can do so through my website by clicking this fancy image!"});
this.containerEl.createEl("a", {href: "https://ellpeck.de/support"})
Expand Down
4 changes: 3 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export const defaultSettings: SimpleTimeTrackerSettings = {
timestampFormat: "YY-MM-DD hh:mm:ss",
csvDelimiter: ","
csvDelimiter: ",",
fineGrainedDurations: true
};

export interface SimpleTimeTrackerSettings {

timestampFormat: string;
csvDelimiter: string;
fineGrainedDurations: boolean;

}
42 changes: 24 additions & 18 deletions src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export function displayTracker(tracker: Tracker, element: HTMLElement, file: str
}


setCountdownValues(tracker, current, total, currentDiv);
setCountdownValues(tracker, current, total, currentDiv, settings);
let intervalId = window.setInterval(() => {
// we delete the interval timer when the element is removed
if (!element.isConnected) {
window.clearInterval(intervalId);
return;
}
setCountdownValues(tracker, current, total, currentDiv);
setCountdownValues(tracker, current, total, currentDiv, settings);
}, 1000);
}

Expand Down Expand Up @@ -186,32 +186,38 @@ function getTotalDuration(entries: Entry[]): number {
return ret;
}

function setCountdownValues(tracker: Tracker, current: HTMLElement, total: HTMLElement, currentDiv: HTMLDivElement) {
function setCountdownValues(tracker: Tracker, current: HTMLElement, total: HTMLElement, currentDiv: HTMLDivElement, settings: SimpleTimeTrackerSettings) {
let running = getRunningEntry(tracker.entries);
if (running && !running.endTime) {
current.setText(formatDuration(getDuration(running)));
current.setText(formatDuration(getDuration(running), settings));
currentDiv.hidden = false;
} else {
currentDiv.hidden = true;
}
total.setText(formatDuration(getTotalDuration(tracker.entries)));
total.setText(formatDuration(getTotalDuration(tracker.entries), settings));
}

function formatTimestamp(timestamp: number, settings: SimpleTimeTrackerSettings): string {
return moment.unix(timestamp).format(settings.timestampFormat);
}

function formatDuration(totalTime: number): string {
let duration = moment.duration(totalTime);
function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string {
let ret = "";
if (duration.years() > 0)
ret += duration.years() + "y ";
if (duration.months() > 0)
ret += duration.months() + "m ";
if (duration.days() > 0)
ret += duration.days() + "d ";
if (duration.hours() > 0)
ret += duration.hours() + "h ";
let duration = moment.duration(totalTime);
let hours: number;
if (settings.fineGrainedDurations) {
if (duration.years() > 0)
ret += duration.years() + "y ";
if (duration.months() > 0)
ret += duration.months() + "M ";
if (duration.days() > 0)
ret += duration.days() + "d ";
hours = duration.hours();
} else {
hours = Math.floor(duration.asHours());
}
if (hours > 0)
ret += hours + "h ";
if (duration.minutes() > 0)
ret += duration.minutes() + "m ";
ret += duration.seconds() + "s";
Expand All @@ -222,7 +228,7 @@ function createMarkdownTable(tracker: Tracker, settings: SimpleTimeTrackerSettin
let table = [["Segment", "Start time", "End time", "Duration"]];
for (let entry of tracker.entries)
table.push(...createTableSection(entry, settings));
table.push(["**Total**", "", "", `**${formatDuration(getTotalDuration(tracker.entries))}**`]);
table.push(["**Total**", "", "", `**${formatDuration(getTotalDuration(tracker.entries), settings)}**`]);

let ret = "";
// calculate the width every column needs to look neat when monospaced
Expand Down Expand Up @@ -254,7 +260,7 @@ function createTableSection(entry: Entry, settings: SimpleTimeTrackerSettings):
entry.name,
entry.startTime ? formatTimestamp(entry.startTime, settings) : "",
entry.endTime ? formatTimestamp(entry.endTime, settings) : "",
entry.endTime || entry.subEntries ? formatDuration(getDuration(entry)) : ""]];
entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : ""]];
if (entry.subEntries) {
for (let sub of entry.subEntries)
ret.push(...createTableSection(sub, settings));
Expand All @@ -273,7 +279,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle

row.createEl("td", {text: entry.startTime ? formatTimestamp(entry.startTime, settings) : ""});
row.createEl("td", {text: entry.endTime ? formatTimestamp(entry.endTime, settings) : ""});
row.createEl("td", {text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry)) : ""});
row.createEl("td", {text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : ""});

let entryButtons = row.createEl("td");
if (!running) {
Expand Down
2 changes: 1 addition & 1 deletion test-vault/.obsidian/core-plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"outline",
"word-count",
"file-recovery"
]
]
5 changes: 3 additions & 2 deletions test-vault/.obsidian/plugins/simple-time-tracker/data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"timestampFormat": "YY-MM-DD hh:mm:ss",
"csvDelimiter": ","
}
"csvDelimiter": ",",
"fineGrainedDurations": false
}
58 changes: 36 additions & 22 deletions test-vault/.obsidian/plugins/simple-time-tracker/main

Large diffs are not rendered by default.

55 changes: 34 additions & 21 deletions test-vault/.obsidian/plugins/simple-time-tracker/main.js

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions test-vault/duration_accumulation_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@
More notes for my cool project! This note shows that we can correctly display accumulated time that lasts longer than a 24 hour day!

```simple-time-tracker
{"entries":[
{"name":"test","startTime":1596265200,"endTime":1627887600,"subEntries":null},
{"name":"test","startTime":1627801200,"endTime":1633158000,"subEntries":null},
{"name":"test","startTime":1659337200,"endTime":1659423600,"subEntries":null},
{"name":"test","startTime":1664627410,"endTime":1664631605,"subEntries":null}
]}
{"entries":[{"name":"test","startTime":1596265200,"endTime":1627887600,"subEntries":null},{"name":"test","startTime":1627801200,"endTime":1633158000,"subEntries":null},{"name":"test","startTime":1659337200,"endTime":1659423600,"subEntries":null},{"name":"test","startTime":1664627410,"endTime":1664631605,"subEntries":null},{"name":"Segment 5","startTime":1684858616,"endTime":1684858619,"subEntries":null}]}
```

0 comments on commit b5e768d

Please sign in to comment.