From 71e8352ecf923f84053f2fe77b9fe598e609b3da Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 23 Nov 2023 19:13:39 +0200 Subject: [PATCH] Add output for lunch break duration and rework config lunch option --- config/config-schema.json | 8 ++++---- config/config.toml | 11 ++++------- src/config.ts | 12 ++++-------- src/i18n.ts | 5 +++++ src/input.ts | 12 ++++++------ src/output.ts | 10 +++++++--- src/types/WtcConfig.ts | 3 +-- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/config/config-schema.json b/config/config-schema.json index e001123..5d9f2aa 100644 --- a/config/config-schema.json +++ b/config/config-schema.json @@ -8,6 +8,10 @@ }, "type": "object", "properties": { + "lunchBreakDuration": { + "type": "string", + "description": "Remove or set as \"00:00\" if you don't have an unpaid lunch break or if you normally log your lunch break hours" + }, "language": { "$ref": "#/definitions/language", "description": "The language of the application. Currently supported languages are 'en', 'fi'" @@ -19,10 +23,6 @@ "defaults": { "type": "object", "properties": { - "lunchBreakDuration": { - "type": "string", - "description": "Set as \"00:00\" if you don't have an unpaid lunch break or if you normally log your lunch break hours" - }, "workDayDuration": { "type": "string", "description": "Your work day duration" }, "startTime": { "type": "string", "description": "The time you start working" }, "stopTime": { diff --git a/config/config.toml b/config/config.toml index 034b261..d2f6235 100644 --- a/config/config.toml +++ b/config/config.toml @@ -8,6 +8,10 @@ # usually ~/.config/wtc/config.toml # For windows, I don't know. +# Remove or set as "00:00" if you don't have an unpaid lunch break +# or if you normally log your lunch break hours +lunchBreakDuration = "00:30" + # The language of the application. # Currently supported languages are "en", "fi" language = "en" @@ -20,10 +24,6 @@ timestampFormat = "YYYY-MM-DD HH:mm" # This section is for default values for inputs [defaults] -# Set as "00:00" if you don't have an unpaid lunch break -# or if you normally log your lunch break hours -lunchBreakDuration = "00:30" - # Your work day duration workDayDuration = "07:30" @@ -41,6 +41,3 @@ workDayDuration = true startTime = true stopTime = true logged = true - -# It is assumed that you didn't have lunch if this is false -hadLunch = true diff --git a/src/config.ts b/src/config.ts index 3623862..1275c06 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,10 +6,10 @@ import { parseDuration, parseTimestamp } from './parse.js'; import WtcConfig from './types/WtcConfig.js'; import Language from './types/Language.js'; -interface RawConfig extends Omit { +interface RawConfig extends Omit { + lunchBreakDuration: string; defaults: { workDayDuration: string; - lunchBreakDuration: string; startTime: string; stopTime: string; }; @@ -18,9 +18,9 @@ interface RawConfig extends Omit { const defaultConfig: RawConfig = { language: Language.en, timestampFormat: 'YYYY-MM-DD HH:mm', + lunchBreakDuration: '00:30', defaults: { workDayDuration: '07:30', - lunchBreakDuration: '00:30', startTime: '08:00', stopTime: 'now', }, @@ -29,7 +29,6 @@ const defaultConfig: RawConfig = { startTime: true, stopTime: true, logged: true, - hadLunch: true, }, }; @@ -47,13 +46,11 @@ const getConfig = (): WtcConfig => { return { language: configData.language ?? defaultConfig.language, timestampFormat: configData.timestampFormat ?? defaultConfig.timestampFormat, + lunchBreakDuration: parseDuration(configData.lunchBreakDuration), defaults: { workDayDuration: parseDuration( configData.defaults.workDayDuration ?? defaultConfig.defaults.workDayDuration, ), - lunchBreakDuration: parseDuration( - configData.defaults.lunchBreakDuration ?? defaultConfig.defaults.workDayDuration, - ), startTime: parseTimestamp(configData.defaults.startTime ?? defaultConfig.defaults.startTime), stopTime: parseTimestamp(configData.defaults.stopTime ?? defaultConfig.defaults.stopTime), }, @@ -62,7 +59,6 @@ const getConfig = (): WtcConfig => { startTime: configData.askInput.startTime ?? defaultConfig.askInput.startTime, stopTime: configData.askInput.stopTime ?? defaultConfig.askInput.stopTime, logged: configData.askInput.logged ?? defaultConfig.askInput.logged, - hadLunch: configData.askInput.hadLunch ?? defaultConfig.askInput.hadLunch, }, }; }; diff --git a/src/i18n.ts b/src/i18n.ts index 833a729..9339014 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -7,6 +7,7 @@ export enum MessageKey { parseTimeFailed, startTimeBeforeStopTimeError, promptLunchBreak, + unpaidLunch, promptLogged, none, startedWorking, @@ -46,6 +47,10 @@ const messages: Record> = { [Language.en]: 'Did you have a lunch break? [y/N]: ', [Language.fi]: 'Piditkö jo lounastauon? [k/E]: ', }, + [MessageKey.unpaidLunch]: { + [Language.en]: 'Unpaid lunch duration:', + [Language.fi]: 'Palkattoman lounaan pituus:', + }, [MessageKey.promptLogged]: { [Language.en]: 'How many hours did you log already? [00:00] ', [Language.fi]: 'Kuinka monta tuntia kirjasit jo? [00:00] ', diff --git a/src/input.ts b/src/input.ts index 96d2450..b7ffbf6 100644 --- a/src/input.ts +++ b/src/input.ts @@ -16,7 +16,7 @@ const { error } = console; const input = async (config: WtcConfig): Promise => { const msg = message(config.language); const fmtDuration = formatDuration(config.language); - const { defaults, askInput } = config; + const { defaults, askInput, lunchBreakDuration } = config; const rl = readline.createInterface({ input: process.stdin, output: process.stdout, @@ -91,13 +91,13 @@ const input = async (config: WtcConfig): Promise => { let worked = dayjs.duration(stoppedAt.diff(startedAt)); let hadLunch = false; - if (askInput.hadLunch) { + if (lunchBreakDuration) { const lunchAnswer = (await rl.question(msg(MessageKey.promptLunchBreak))).toLowerCase(); - hadLunch = lunchAnswer === 'y' || lunchAnswer === 'k'; - } - if (hadLunch) { - worked = worked.subtract(defaults.lunchBreakDuration); + if (lunchAnswer === 'y' || lunchAnswer === 'k') { + hadLunch = true + worked = worked.subtract(lunchBreakDuration); + } } // Calculate unlogged time diff --git a/src/output.ts b/src/output.ts index a74b944..8329fd4 100644 --- a/src/output.ts +++ b/src/output.ts @@ -7,21 +7,25 @@ import WtcConfig from './types/WtcConfig'; const { log } = console; const output = (result: WtcPromptResult, config: WtcConfig) => { - const {language, timestampFormat} = config; + const { language, timestampFormat } = config; const msg = message(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, hadLunch } = result; log(); log(msg(MessageKey.startedWorking), startedAt.format(timestampFormat)); log( (stoppedWorking ? msg(MessageKey.stoppedWorking) : msg(MessageKey.hoursCalculated)) + ` ${msg(MessageKey.klo)}:`, - stoppedAt.format(timestampFormat) + stoppedAt.format(timestampFormat), ); log(msg(MessageKey.workedToday), chalk.green(fmtDuration(worked)), chalk.yellow(hoursRounded(worked))); + if (hadLunch) { + log(msg(MessageKey.unpaidLunch), chalk.green(fmtDuration(config.defaults.lunchBreakDuration))); + } + const unLoggedMinutes = unLogged.asMinutes(); if (unLoggedMinutes >= 0) { log( diff --git a/src/types/WtcConfig.ts b/src/types/WtcConfig.ts index a0aa1bc..5add086 100644 --- a/src/types/WtcConfig.ts +++ b/src/types/WtcConfig.ts @@ -5,9 +5,9 @@ import Language from './Language.js'; export default interface WtcConfig { language: Language, timestampFormat: string, + lunchBreakDuration?: Duration; defaults: { workDayDuration: Duration; - lunchBreakDuration: Duration; startTime: Dayjs; stopTime: Dayjs; }; @@ -16,6 +16,5 @@ export default interface WtcConfig { startTime: boolean; stopTime: boolean; logged: boolean; - hadLunch: boolean; }; }