Add config.defaults.hadLunch and rework lunch messages
This commit is contained in:
parent
91a7362495
commit
b08ab097ba
8 changed files with 60 additions and 24 deletions
|
@ -4,7 +4,7 @@
|
||||||
"properties": {
|
"properties": {
|
||||||
"lunchBreakDuration": {
|
"lunchBreakDuration": {
|
||||||
"type": "string",
|
"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"
|
"description": "Comment out or remove if you don't have an unpaid lunch break or if you normally log your lunch break hours"
|
||||||
},
|
},
|
||||||
"language": {
|
"language": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
@ -23,7 +23,8 @@
|
||||||
"stopTime": {
|
"stopTime": {
|
||||||
"type": ["string", "null"],
|
"type": ["string", "null"],
|
||||||
"description": "The time you stop working. Can either be 'now' or a time"
|
"description": "The time you stop working. Can either be 'now' or a time"
|
||||||
}
|
},
|
||||||
|
"hadLunch": { "type": "boolean", "description": "Wether you had lunch already or not" }
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"description": "Default values for inputs"
|
"description": "Default values for inputs"
|
||||||
|
@ -37,8 +38,7 @@
|
||||||
},
|
},
|
||||||
"startTime": { "type": "boolean", "description": "Disable prompt for start time if set to false" },
|
"startTime": { "type": "boolean", "description": "Disable prompt for start time if set to false" },
|
||||||
"stopTime": { "type": "boolean", "description": "Disable prompt for stop time if set to false" },
|
"stopTime": { "type": "boolean", "description": "Disable prompt for stop time if set to false" },
|
||||||
"logged": { "type": "boolean", "description": "Disable prompt for logged time if set to false" },
|
"logged": { "type": "boolean", "description": "Disable prompt for logged time if set to false" }
|
||||||
"hadLunch": { "type": "boolean", "description": "Assumed that you didn't have lunch if this is false" }
|
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"description": "Settings to disable prompts"
|
"description": "Settings to disable prompts"
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
# usually ~/.config/wtc/config.toml
|
# usually ~/.config/wtc/config.toml
|
||||||
# For windows, I don't know.
|
# For windows, I don't know.
|
||||||
|
|
||||||
# Remove or set as "00:00" if you don't have an unpaid lunch break
|
# Comment out or remove if you don't have an unpaid lunch break
|
||||||
# or if you normally log your lunch break hours
|
# or if you normally log your lunch break hours
|
||||||
lunchBreakDuration = "00:30"
|
unpaidLunchBreakDuration = "00:30"
|
||||||
|
|
||||||
# The language of the application.
|
# The language of the application.
|
||||||
# Currently supported languages are "en", "fi"
|
# Currently supported languages are "en", "fi"
|
||||||
|
|
|
@ -6,23 +6,25 @@ import { parseDuration, parseTimestamp } from './parse.js';
|
||||||
import WtcConfig from './types/WtcConfig.js';
|
import WtcConfig from './types/WtcConfig.js';
|
||||||
import Language from './types/Language.js';
|
import Language from './types/Language.js';
|
||||||
|
|
||||||
interface RawConfig extends Omit<WtcConfig, 'lunchBreakDuration' | 'defaults'> {
|
interface RawConfig extends Omit<WtcConfig, 'unpaidLunchBreakDuration' | 'defaults'> {
|
||||||
lunchBreakDuration: string;
|
unpaidLunchBreakDuration: string;
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: string;
|
workDayDuration: string;
|
||||||
startTime: string;
|
startTime: string;
|
||||||
stopTime: string;
|
stopTime: string;
|
||||||
|
hadLunch: boolean;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultConfig: RawConfig = {
|
const defaultConfig: RawConfig = {
|
||||||
language: Language.en,
|
language: Language.en,
|
||||||
timestampFormat: 'YYYY-MM-DD HH:mm',
|
timestampFormat: 'YYYY-MM-DD HH:mm',
|
||||||
lunchBreakDuration: '00:30',
|
unpaidLunchBreakDuration: '00:30',
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: '07:30',
|
workDayDuration: '07:30',
|
||||||
startTime: '08:00',
|
startTime: '08:00',
|
||||||
stopTime: 'now',
|
stopTime: 'now',
|
||||||
|
hadLunch: true,
|
||||||
},
|
},
|
||||||
askInput: {
|
askInput: {
|
||||||
workDayLength: true,
|
workDayLength: true,
|
||||||
|
@ -46,13 +48,14 @@ const getConfig = (): WtcConfig => {
|
||||||
return {
|
return {
|
||||||
language: configData.language ?? defaultConfig.language,
|
language: configData.language ?? defaultConfig.language,
|
||||||
timestampFormat: configData.timestampFormat ?? defaultConfig.timestampFormat,
|
timestampFormat: configData.timestampFormat ?? defaultConfig.timestampFormat,
|
||||||
lunchBreakDuration: parseDuration(configData.lunchBreakDuration),
|
unpaidLunchBreakDuration: !configData.unpaidLunchBreakDuration ? undefined : parseDuration(configData.unpaidLunchBreakDuration),
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: parseDuration(
|
workDayDuration: parseDuration(
|
||||||
configData.defaults.workDayDuration ?? defaultConfig.defaults.workDayDuration,
|
configData.defaults.workDayDuration ?? defaultConfig.defaults.workDayDuration,
|
||||||
),
|
),
|
||||||
startTime: parseTimestamp(configData.defaults.startTime ?? defaultConfig.defaults.startTime),
|
startTime: parseTimestamp(configData.defaults.startTime ?? defaultConfig.defaults.startTime),
|
||||||
stopTime: parseTimestamp(configData.defaults.stopTime ?? defaultConfig.defaults.stopTime),
|
stopTime: parseTimestamp(configData.defaults.stopTime ?? defaultConfig.defaults.stopTime),
|
||||||
|
hadLunch: configData.defaults.hadLunch ?? defaultConfig.defaults.hadLunch,
|
||||||
},
|
},
|
||||||
askInput: {
|
askInput: {
|
||||||
workDayLength: configData.askInput.workDayLength ?? defaultConfig.askInput.workDayLength,
|
workDayLength: configData.askInput.workDayLength ?? defaultConfig.askInput.workDayLength,
|
||||||
|
|
|
@ -7,7 +7,8 @@ export const formatTime = (time: Dayjs): string => time.format('HH:mm');
|
||||||
|
|
||||||
export const formatDuration =
|
export const formatDuration =
|
||||||
(language: Language) =>
|
(language: Language) =>
|
||||||
(duration: Duration, short?: boolean): string => {
|
(duration?: Duration, short?: boolean): string => {
|
||||||
|
duration = duration ?? dayjs.duration(0, 'minutes');
|
||||||
if (duration.hours() === 0 && duration.minutes() === 0) {
|
if (duration.hours() === 0 && duration.minutes() === 0) {
|
||||||
return 'none';
|
return 'none';
|
||||||
}
|
}
|
||||||
|
|
28
src/i18n.ts
28
src/i18n.ts
|
@ -1,12 +1,16 @@
|
||||||
import Language from './types/Language';
|
import Language from './types/Language';
|
||||||
|
|
||||||
|
|
||||||
export enum MessageKey {
|
export enum MessageKey {
|
||||||
promptWorkDayDuration,
|
promptWorkDayDuration,
|
||||||
|
excludingLunch,
|
||||||
promptStartTime,
|
promptStartTime,
|
||||||
promptStopTime,
|
promptStopTime,
|
||||||
parseTimeFailed,
|
parseTimeFailed,
|
||||||
startTimeBeforeStopTimeError,
|
startTimeBeforeStopTimeError,
|
||||||
promptLunchBreak,
|
promptLunchBreak,
|
||||||
|
promptYesNoYes,
|
||||||
|
promptYesNoNo,
|
||||||
unpaidLunch,
|
unpaidLunch,
|
||||||
promptLogged,
|
promptLogged,
|
||||||
none,
|
none,
|
||||||
|
@ -24,8 +28,12 @@ export enum MessageKey {
|
||||||
|
|
||||||
const messages: Record<MessageKey, Record<Language, string>> = {
|
const messages: Record<MessageKey, Record<Language, string>> = {
|
||||||
[MessageKey.promptWorkDayDuration]: {
|
[MessageKey.promptWorkDayDuration]: {
|
||||||
[Language.en]: 'How long is your work day today, excluding the lunch break? [{0}]: ',
|
[Language.en]: 'How long is your work day today{0}? [{1}]: ',
|
||||||
[Language.fi]: 'Kuinka pitkä työpäiväsi on tänään, poisluettuna lounastauko? [{0}]: ',
|
[Language.fi]: 'Kuinka pitkä työpäiväsi on tänään{0}? [{1}]: ',
|
||||||
|
},
|
||||||
|
[MessageKey.excludingLunch]: {
|
||||||
|
[Language.en]: ', excluding the lunch break',
|
||||||
|
[Language.fi]: ', poisluettuna lounastauko',
|
||||||
},
|
},
|
||||||
[MessageKey.promptStartTime]: {
|
[MessageKey.promptStartTime]: {
|
||||||
[Language.en]: 'What time did you start work today? [{0}]: ',
|
[Language.en]: 'What time did you start work today? [{0}]: ',
|
||||||
|
@ -44,12 +52,20 @@ const messages: Record<MessageKey, Record<Language, string>> = {
|
||||||
[Language.fi]: 'Aloitusaika ({0}) pitää olla ennen lopetusaikaa ({1}). Ohjelma sammuu',
|
[Language.fi]: 'Aloitusaika ({0}) pitää olla ennen lopetusaikaa ({1}). Ohjelma sammuu',
|
||||||
},
|
},
|
||||||
[MessageKey.promptLunchBreak]: {
|
[MessageKey.promptLunchBreak]: {
|
||||||
[Language.en]: 'Did you have a lunch break? [y/N]: ',
|
[Language.en]: 'Did you have a lunch break? [{0}]: ',
|
||||||
[Language.fi]: 'Piditkö jo lounastauon? [k/E]: ',
|
[Language.fi]: 'Piditkö jo lounastauon? [{0}]: ',
|
||||||
|
},
|
||||||
|
[MessageKey.promptYesNoYes]: {
|
||||||
|
[Language.en]: 'Y/n',
|
||||||
|
[Language.fi]: 'K/e',
|
||||||
|
},
|
||||||
|
[MessageKey.promptYesNoNo]: {
|
||||||
|
[Language.en]: 'y/N',
|
||||||
|
[Language.fi]: 'k/E',
|
||||||
},
|
},
|
||||||
[MessageKey.unpaidLunch]: {
|
[MessageKey.unpaidLunch]: {
|
||||||
[Language.en]: 'Unpaid lunch duration:',
|
[Language.en]: 'Unpaid lunch:',
|
||||||
[Language.fi]: 'Palkattoman lounaan pituus:',
|
[Language.fi]: 'Palkaton lounas:',
|
||||||
},
|
},
|
||||||
[MessageKey.promptLogged]: {
|
[MessageKey.promptLogged]: {
|
||||||
[Language.en]: 'How many hours did you log already? [00:00] ',
|
[Language.en]: 'How many hours did you log already? [00:00] ',
|
||||||
|
|
25
src/input.ts
25
src/input.ts
|
@ -16,7 +16,7 @@ const { error } = console;
|
||||||
const input = async (config: WtcConfig): Promise<WtcPromptResult> => {
|
const input = async (config: WtcConfig): Promise<WtcPromptResult> => {
|
||||||
const msg = message(config.language);
|
const msg = message(config.language);
|
||||||
const fmtDuration = formatDuration(config.language);
|
const fmtDuration = formatDuration(config.language);
|
||||||
const { defaults, askInput, lunchBreakDuration } = config;
|
const { defaults, askInput, unpaidLunchBreakDuration: lunchBreakDuration } = config;
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout,
|
output: process.stdout,
|
||||||
|
@ -32,7 +32,11 @@ const input = async (config: WtcConfig): Promise<WtcPromptResult> => {
|
||||||
|
|
||||||
if (askInput.workDayLength) {
|
if (askInput.workDayLength) {
|
||||||
const durationAnswer = await rl.question(
|
const durationAnswer = await rl.question(
|
||||||
msg(MessageKey.promptWorkDayDuration, fmtDuration(defaults.workDayDuration, true)),
|
msg(
|
||||||
|
MessageKey.promptWorkDayDuration,
|
||||||
|
config.unpaidLunchBreakDuration ? msg(MessageKey.excludingLunch) : '',
|
||||||
|
fmtDuration(defaults.workDayDuration, true),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
if (durationAnswer !== '') {
|
if (durationAnswer !== '') {
|
||||||
workDayDuration = parseDuration(durationAnswer);
|
workDayDuration = parseDuration(durationAnswer);
|
||||||
|
@ -92,10 +96,21 @@ const input = async (config: WtcConfig): Promise<WtcPromptResult> => {
|
||||||
|
|
||||||
let hadLunch = false;
|
let hadLunch = false;
|
||||||
if (lunchBreakDuration) {
|
if (lunchBreakDuration) {
|
||||||
const lunchAnswer = (await rl.question(msg(MessageKey.promptLunchBreak))).toLowerCase();
|
const lunchAnswer = (
|
||||||
|
await rl.question(
|
||||||
|
msg(
|
||||||
|
MessageKey.promptLunchBreak,
|
||||||
|
msg(config.defaults.hadLunch ? MessageKey.promptYesNoYes : MessageKey.promptYesNoNo),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
).toLowerCase();
|
||||||
|
|
||||||
if (lunchAnswer === 'y' || lunchAnswer === 'k') {
|
if (
|
||||||
hadLunch = true
|
lunchAnswer === 'y' ||
|
||||||
|
lunchAnswer === 'k' ||
|
||||||
|
(config.defaults.hadLunch && lunchAnswer !== 'n' && lunchAnswer !== 'e')
|
||||||
|
) {
|
||||||
|
hadLunch = true;
|
||||||
worked = worked.subtract(lunchBreakDuration);
|
worked = worked.subtract(lunchBreakDuration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ const output = (result: WtcPromptResult, config: WtcConfig) => {
|
||||||
log(msg(MessageKey.workedToday), chalk.green(fmtDuration(worked)), chalk.yellow(hoursRounded(worked)));
|
log(msg(MessageKey.workedToday), chalk.green(fmtDuration(worked)), chalk.yellow(hoursRounded(worked)));
|
||||||
|
|
||||||
if (hadLunch) {
|
if (hadLunch) {
|
||||||
log(msg(MessageKey.unpaidLunch), chalk.green(fmtDuration(config.defaults.lunchBreakDuration)));
|
log(msg(MessageKey.unpaidLunch), chalk.green(fmtDuration(config.unpaidLunchBreakDuration)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const unLoggedMinutes = unLogged.asMinutes();
|
const unLoggedMinutes = unLogged.asMinutes();
|
||||||
|
|
|
@ -5,11 +5,12 @@ import Language from './Language.js';
|
||||||
export default interface WtcConfig {
|
export default interface WtcConfig {
|
||||||
language: Language,
|
language: Language,
|
||||||
timestampFormat: string,
|
timestampFormat: string,
|
||||||
lunchBreakDuration?: Duration;
|
unpaidLunchBreakDuration?: Duration;
|
||||||
defaults: {
|
defaults: {
|
||||||
workDayDuration: Duration;
|
workDayDuration: Duration;
|
||||||
startTime: Dayjs;
|
startTime: Dayjs;
|
||||||
stopTime: Dayjs;
|
stopTime: Dayjs;
|
||||||
|
hadLunch: boolean;
|
||||||
};
|
};
|
||||||
askInput: {
|
askInput: {
|
||||||
workDayLength: boolean;
|
workDayLength: boolean;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue