Add output for lunch break duration and rework config lunch option
This commit is contained in:
parent
29ded9426b
commit
71e8352ecf
7 changed files with 31 additions and 30 deletions
|
@ -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<WtcConfig, 'defaults'> {
|
||||
interface RawConfig extends Omit<WtcConfig, 'lunchBreakDuration' | 'defaults'> {
|
||||
lunchBreakDuration: string;
|
||||
defaults: {
|
||||
workDayDuration: string;
|
||||
lunchBreakDuration: string;
|
||||
startTime: string;
|
||||
stopTime: string;
|
||||
};
|
||||
|
@ -18,9 +18,9 @@ interface RawConfig extends Omit<WtcConfig, 'defaults'> {
|
|||
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,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
@ -7,6 +7,7 @@ export enum MessageKey {
|
|||
parseTimeFailed,
|
||||
startTimeBeforeStopTimeError,
|
||||
promptLunchBreak,
|
||||
unpaidLunch,
|
||||
promptLogged,
|
||||
none,
|
||||
startedWorking,
|
||||
|
@ -46,6 +47,10 @@ const messages: Record<MessageKey, Record<Language, string>> = {
|
|||
[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] ',
|
||||
|
|
12
src/input.ts
12
src/input.ts
|
@ -16,7 +16,7 @@ const { error } = console;
|
|||
const input = async (config: WtcConfig): Promise<WtcPromptResult> => {
|
||||
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<WtcPromptResult> => {
|
|||
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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue