Add support for configurable timestamp format
This commit is contained in:
parent
aee473b93b
commit
b76700923d
5 changed files with 16 additions and 8 deletions
|
@ -9,6 +9,11 @@
|
||||||
# Currently supported languages are "en", "fi"
|
# Currently supported languages are "en", "fi"
|
||||||
language = "en"
|
language = "en"
|
||||||
|
|
||||||
|
# Time format used to display timestamps (started, stopped etc.)
|
||||||
|
# Refer to the dayjs documentation on how to set this https://day.js.org/docs/en/display/format
|
||||||
|
# For example, the finnish format would be "MM.DD.YYYY [kello] HH.mm"
|
||||||
|
timestampFormat = "YYYY-MM-DD HH:mm"
|
||||||
|
|
||||||
# This section is for default values for inputs
|
# This section is for default values for inputs
|
||||||
[defaults]
|
[defaults]
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ interface RawConfig extends Omit<WtcConfig, 'defaults'> {
|
||||||
|
|
||||||
const defaultConfig: RawConfig = {
|
const defaultConfig: RawConfig = {
|
||||||
language: Language.en,
|
language: Language.en,
|
||||||
|
timestampFormat: 'YYYY-MM-DD HH:mm',
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: '07:30',
|
workDayDuration: '07:30',
|
||||||
lunchBreakDuration: '00:30',
|
lunchBreakDuration: '00:30',
|
||||||
|
@ -45,6 +46,7 @@ const getConfig = (): WtcConfig => {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
language: configData.language ?? defaultConfig.language,
|
language: configData.language ?? defaultConfig.language,
|
||||||
|
timestampFormat: configData.timestampFormat ?? defaultConfig.timestampFormat,
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: parseDuration(
|
workDayDuration: parseDuration(
|
||||||
configData.defaults.workDayDuration ?? defaultConfig.defaults.workDayDuration,
|
configData.defaults.workDayDuration ?? defaultConfig.defaults.workDayDuration,
|
||||||
|
|
|
@ -3,8 +3,6 @@ import { Duration } from 'dayjs/plugin/duration.js';
|
||||||
import Language from './types/Language';
|
import Language from './types/Language';
|
||||||
import { MessageKey, message } from './i18n';
|
import { MessageKey, message } from './i18n';
|
||||||
|
|
||||||
export const formatTimestamp = (timestamp: Dayjs): string => timestamp.format('YYYY-MM-DD HH:mm');
|
|
||||||
|
|
||||||
export const formatTime = (time: Dayjs): string => time.format('HH:mm');
|
export const formatTime = (time: Dayjs): string => time.format('HH:mm');
|
||||||
|
|
||||||
export const formatDuration =
|
export const formatDuration =
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import { formatDuration, formatTimestamp, getHoursRoundedStr } from './format';
|
import { formatDuration, getHoursRoundedStr } from './format';
|
||||||
import { WtcPromptResult } from './types/WtcPromptResult';
|
import { WtcPromptResult } from './types/WtcPromptResult';
|
||||||
import { MessageKey, message } from './i18n.js';
|
import { MessageKey, message } from './i18n.js';
|
||||||
import WtcConfig from './types/WtcConfig';
|
import WtcConfig from './types/WtcConfig';
|
||||||
|
@ -7,16 +7,18 @@ import WtcConfig from './types/WtcConfig';
|
||||||
const { log } = console;
|
const { log } = console;
|
||||||
|
|
||||||
const output = (result: WtcPromptResult, config: WtcConfig) => {
|
const output = (result: WtcPromptResult, config: WtcConfig) => {
|
||||||
const msg = message(config.language);
|
const {language, timestampFormat} = config;
|
||||||
const fmtDuration = formatDuration(config.language);
|
const msg = message(language);
|
||||||
const hoursRounded = getHoursRoundedStr(config.language);
|
const fmtDuration = formatDuration(language);
|
||||||
|
const hoursRounded = getHoursRoundedStr(language);
|
||||||
const { startedAt, stoppedAt, stoppedWorking, worked, unLogged, workLeft, workedOvertime } = result;
|
const { startedAt, stoppedAt, stoppedWorking, worked, unLogged, workLeft, workedOvertime } = result;
|
||||||
|
|
||||||
log();
|
log();
|
||||||
log(msg(MessageKey.startedWorking), formatTimestamp(startedAt));
|
log(msg(MessageKey.startedWorking), startedAt.format(timestampFormat));
|
||||||
log(
|
log(
|
||||||
(stoppedWorking ? msg(MessageKey.stoppedWorking) : msg(MessageKey.hoursCalculated)) +
|
(stoppedWorking ? msg(MessageKey.stoppedWorking) : msg(MessageKey.hoursCalculated)) +
|
||||||
` ${msg(MessageKey.klo)}:`,
|
` ${msg(MessageKey.klo)}:`,
|
||||||
formatTimestamp(stoppedAt),
|
stoppedAt.format(timestampFormat)
|
||||||
);
|
);
|
||||||
log(msg(MessageKey.workedToday), chalk.green(fmtDuration(worked)), chalk.yellow(hoursRounded(worked)));
|
log(msg(MessageKey.workedToday), chalk.green(fmtDuration(worked)), chalk.yellow(hoursRounded(worked)));
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import Language from './Language.js';
|
||||||
|
|
||||||
export default interface WtcConfig {
|
export default interface WtcConfig {
|
||||||
language: Language,
|
language: Language,
|
||||||
|
timestampFormat: string,
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: Duration;
|
workDayDuration: Duration;
|
||||||
lunchBreakDuration: Duration;
|
lunchBreakDuration: Duration;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue