added timestamp display for durations

closes #20
This commit is contained in:
Ell 2024-02-26 15:21:24 +01:00
parent 7633ac8144
commit 1d90a17e69
5 changed files with 41 additions and 19 deletions

View file

@ -52,6 +52,17 @@ export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {
}); });
}); });
new Setting(this.containerEl)
.setName("Timestamp Durations")
.setDesc("Whether durations should be displayed in a timestamp format (12:15:01) rather than the default duration format (12h 15m 1s).")
.addToggle(t => {
t.setValue(this.plugin.settings.timestampDurations);
t.onChange(async v => {
this.plugin.settings.timestampDurations = v;
await this.plugin.saveSettings();
});
});
new Setting(this.containerEl) new Setting(this.containerEl)
.setName("Display Segments in Reverse Order") .setName("Display Segments in Reverse Order")
.setDesc("Whether older tracker segments should be displayed towards the bottom of the tracker, rather than the top.") .setDesc("Whether older tracker segments should be displayed towards the bottom of the tracker, rather than the top.")

View file

@ -3,7 +3,8 @@ export const defaultSettings: SimpleTimeTrackerSettings = {
editableTimestampFormat: "YYYY-MM-DD HH:mm:ss", editableTimestampFormat: "YYYY-MM-DD HH:mm:ss",
csvDelimiter: ",", csvDelimiter: ",",
fineGrainedDurations: true, fineGrainedDurations: true,
reverseSegmentOrder: false reverseSegmentOrder: false,
timestampDurations: false
}; };
export interface SimpleTimeTrackerSettings { export interface SimpleTimeTrackerSettings {
@ -13,5 +14,6 @@ export interface SimpleTimeTrackerSettings {
csvDelimiter: string; csvDelimiter: string;
fineGrainedDurations: boolean; fineGrainedDurations: boolean;
reverseSegmentOrder: boolean; reverseSegmentOrder: boolean;
timestampDurations: boolean;
} }

View file

@ -215,23 +215,31 @@ function unformatEditableTimestamp(formatted: string, settings: SimpleTimeTracke
function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string { function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string {
let ret = ""; let ret = "";
let duration = moment.duration(totalTime); let duration = moment.duration(totalTime);
let hours: number; let hours = settings.fineGrainedDurations ? duration.hours() : Math.floor(duration.asHours());
if (settings.timestampDurations) {
if (settings.fineGrainedDurations) { if (settings.fineGrainedDurations) {
if (duration.years() > 0) let days = Math.floor(duration.asDays());
ret += duration.years() + "y "; if (days > 0)
ret += days + ".";
}
ret += `${hours.toString().padStart(2, "0")}:${duration.minutes().toString().padStart(2, "0")}:${duration.seconds().toString().padStart(2, "0")}`;
} else {
if (settings.fineGrainedDurations) {
let years = Math.floor(duration.asYears());
if (years > 0)
ret += years + "y ";
if (duration.months() > 0) if (duration.months() > 0)
ret += duration.months() + "M "; ret += duration.months() + "M ";
if (duration.days() > 0) if (duration.days() > 0)
ret += duration.days() + "d "; ret += duration.days() + "d ";
hours = duration.hours();
} else {
hours = Math.floor(duration.asHours());
} }
if (hours > 0) if (hours > 0)
ret += hours + "h "; ret += hours + "h ";
if (duration.minutes() > 0) if (duration.minutes() > 0)
ret += duration.minutes() + "m "; ret += duration.minutes() + "m ";
ret += duration.seconds() + "s"; ret += duration.seconds() + "s";
}
return ret; return ret;
} }

View file

@ -2,6 +2,7 @@
"timestampFormat": "YY-MM-DD hh:mm:ss", "timestampFormat": "YY-MM-DD hh:mm:ss",
"editableTimestampFormat": "YYYY-MM-DD HH:mm:ss", "editableTimestampFormat": "YYYY-MM-DD HH:mm:ss",
"csvDelimiter": ",", "csvDelimiter": ",",
"fineGrainedDurations": false, "fineGrainedDurations": true,
"reverseSegmentOrder": true "reverseSegmentOrder": false,
"timestampDurations": true
} }

View file

@ -6,5 +6,5 @@ More notes for my cool project! This note shows that we can correctly display ac
``` ```
```simple-time-tracker ```simple-time-tracker
{"entries":[{"name":"Segment 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":"2024-02-26T13:37:59.292Z","endTime":"2024-02-26T13:38:01.437Z","subEntries":null},{"name":"Part 2","startTime":"2024-02-26T13:38:16.235Z","endTime":"2024-02-26T13:38:18.895Z","subEntries":null}]}]} {"entries":[{"name":"Segment 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1","startTime":"2024-02-26T13:37:59.292Z","endTime":"2024-02-26T13:38:01.437Z","subEntries":null},{"name":"Part 2","startTime":"2024-02-26T14:04:14.156Z","endTime":"2024-02-26T14:04:30.576Z","subEntries":null}]},{"name":"Part 2","startTime":"2024-02-26T13:38:16.235Z","endTime":"2024-02-26T13:38:18.895Z","subEntries":null}]}]}
``` ```