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)
.setName("Display Segments in Reverse Order")
.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",
csvDelimiter: ",",
fineGrainedDurations: true,
reverseSegmentOrder: false
reverseSegmentOrder: false,
timestampDurations: false
};
export interface SimpleTimeTrackerSettings {
@ -13,5 +14,6 @@ export interface SimpleTimeTrackerSettings {
csvDelimiter: string;
fineGrainedDurations: boolean;
reverseSegmentOrder: boolean;
timestampDurations: boolean;
}

View file

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

View file

@ -2,6 +2,7 @@
"timestampFormat": "YY-MM-DD hh:mm:ss",
"editableTimestampFormat": "YYYY-MM-DD HH:mm:ss",
"csvDelimiter": ",",
"fineGrainedDurations": false,
"reverseSegmentOrder": true
"fineGrainedDurations": 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
{"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}]}]}
```