Skip to content

Commit

Permalink
refactor: improve week command output, rename clDateAgo to clDate
Browse files Browse the repository at this point in the history
  • Loading branch information
f3rno64 committed Dec 14, 2023
1 parent 322e164 commit 982e27f
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 48 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"strip-ansi": "^6.0.0",
"time-speak": "^1.3.3",
"tslib": "^2.6.2",
"weekday": "^3.0.1",
"yargs": "^17.7.2"
}
}
5 changes: 5 additions & 0 deletions src/color/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import colors from 'colors'

const clDate = (dateString: string): string => colors.magenta(dateString)

export default clDate
6 changes: 0 additions & 6 deletions src/color/date_ago.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import clID from './id'
import clText from './text'
import clDate from './date'
import clError from './error'
import clSheet from './sheet'
import clDateAgo from './date_ago'
import clDuration from './duration'
import clHighlight from './highlight'
import clHighlightRed from './highlight_red'

export {
clID,
clText,
clDate,
clError,
clSheet,
clDateAgo,
clDuration,
clHighlight,
clHighlightRed
Expand Down
6 changes: 2 additions & 4 deletions src/commands/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ const handler = async (args: EditCommandArguments): Promise<void> => {
log(
`${C.clText('Updated entry')} ${C.clHighlight(
`${finalEntryID}`
)} ${C.clText('start date to')} ${C.clDateAgo(
startDate.toLocaleString()
)}`
)} ${C.clText('start date to')} ${C.clDate(startDate.toLocaleString())}`
)
} else if (!_isEmpty(end)) {
const endDate = parseDate(end)
Expand All @@ -129,7 +127,7 @@ const handler = async (args: EditCommandArguments): Promise<void> => {
log(
`${C.clText('Updated entry')} ${C.clHighlight(
`${finalEntryID}`
)} ${C.clText('end date to')} ${C.clDateAgo(endDate.toLocaleString())}`
)} ${C.clText('end date to')} ${C.clDate(endDate.toLocaleString())}`
)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const handler = async (args: SheetsCommandArgs) => {
log(
`${C.clText('Showing sheets since')} ${C.clHighlight(
sinceDate.toLocaleString()
)} ${C.clDateAgo(`[${ago(sinceDate)}]`)}`
)} ${C.clDate(`[${ago(sinceDate)}]`)}`
)
log('')
}
Expand Down
102 changes: 71 additions & 31 deletions src/commands/week.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import _sum from 'lodash/sum'
import weekday from 'weekday'
import _isEmpty from 'lodash/isEmpty'
import formatDuration from 'format-duration'

import log from '../log'
import * as U from '../utils'
import * as C from '../color'
import { findSheet } from '../db'
import {
type TimeSheetEntry,
type TimeSheet,
type TimeTrackerDB
} from '../types'

const COMMAND_CONFIG = {
command: 'week',
command: 'week [sheets..]',
describe: 'Display a summary of activity for the past week',
aliases: ['w'],
builder: {
Expand All @@ -25,6 +28,7 @@ const COMMAND_CONFIG = {
interface WeekCommandArguments {
db: TimeTrackerDB
total: boolean
sheets?: string[]
}

const DAY_MS = 24 * 60 * 60 * 1000
Expand Down Expand Up @@ -52,45 +56,64 @@ const getSheetsWithEntriesInLastWeek = (sheets: TimeSheet[]) => {
.filter(({ entries }) => entries.length > 0)
}

interface SheetResult {
interface WeekdayResult {
duration: number
entries: number
}

type SheetResults = Record<string, SheetResult>
type WeekBreakdownResult = Record<string, SheetResults>
type TotalResults = Record<string, number>
type SheetResults = Record<string, WeekdayResult>
type WeekdayResults = Record<string, SheetResults>
type TotalResults = Record<string, WeekdayResult>

const handler = (args: WeekCommandArguments) => {
const { total, db } = args
const { sheets } = db
const relevantSheets = getSheetsWithEntriesInLastWeek(sheets)
const results: WeekBreakdownResult = {}
const { sheets: inputSheets, total, db } = args
const { sheets: allSheets } = db

// prettier-ignore
const selectedSheets =
typeof inputSheets === 'undefined' || _isEmpty(inputSheets)
? allSheets
: inputSheets.map((sheetName: string) => {
const sheet = findSheet(db, sheetName)

if (typeof sheet === 'undefined') {
throw new Error(`Sheet ${sheetName} does not exist`)
}

return sheet
})

const relevantSheets = getSheetsWithEntriesInLastWeek(selectedSheets)
const results: WeekdayResults = {}

let totalDuration = 0
let totalEntries = 0

relevantSheets.forEach(({ name, entries }) => {
const sheetResults: Record<string, SheetResult> = {}
const sheetResults: Record<string, WeekdayResult> = {}

entries.forEach((entry: TimeSheetEntry) => {
for (let i = 0; i < 7; i += 1) {
const date = new Date(+LAST_WEEK_DATE + i * DAY_MS)
const dateKey = date.toLocaleDateString()
const duration = U.getEntryDurationInDay(entry, date)

if (duration === 0) {
continue
}

totalDuration += duration
totalEntries += 1

if (typeof sheetResults[dateKey] === 'undefined') {
sheetResults[dateKey] = {
duration,
entries: duration === 0 ? 0 : 1
entries: 1
}
} else {
sheetResults[dateKey] = {
duration: sheetResults[dateKey].duration + duration,
entries:
duration === 0
? sheetResults[dateKey].entries
: sheetResults[dateKey].entries + 1
entries: sheetResults[dateKey].entries + 1
}
}
}
Expand All @@ -102,7 +125,7 @@ const handler = (args: WeekCommandArguments) => {
log(
`${C.clText('* Total duration:')} ${C.clDuration(
formatDuration(totalDuration)
)}`
)} ${C.clHighlight(`[${totalEntries} entries]`)}`
)

log('')
Expand All @@ -115,19 +138,34 @@ const handler = (args: WeekCommandArguments) => {
const result = results[sheetName][dateKey]
const { duration } = result

if (duration === 0) {
return
}

if (typeof totalResults[dateKey] === 'undefined') {
totalResults[dateKey] = duration
totalResults[dateKey] = {
duration,
entries: 1
}
} else {
totalResults[dateKey] += duration
totalResults[dateKey] = {
duration: totalResults[dateKey].duration + duration,
entries: totalResults[dateKey].entries + 1
}
}
})
})

Object.keys(totalResults).forEach((dateKey: string) => {
Object.keys(totalResults).forEach((dateString: string) => {
const date = new Date(dateString)
const dateWeekday = weekday(date.getDay() + 1)
const result = totalResults[dateString]
const { duration, entries } = result

log(
`${C.clDateAgo(`- ${dateKey}`)}: ${C.clDuration(
`[${formatDuration(totalResults[dateKey])}]`
)}`
`${C.clDate(`- ${dateWeekday} ${dateString}`)}: ${C.clHighlight(
`${entries} entries`
)} ${C.clDuration(`[${formatDuration(duration)}]`)}`
)
})
} else {
Expand All @@ -146,19 +184,21 @@ const handler = (args: WeekCommandArguments) => {
)}`
)

sheetResultDates.forEach((date: string) => {
const result = sheetResults[date]
sheetResultDates.forEach((dateString: string) => {
const date = new Date(dateString)
const dateWeekday = weekday(date.getDay() + 1)
const result = sheetResults[dateString]
const { duration, entries } = result

if (duration === 0) {
log(` ${C.clDateAgo(`- ${date}`)}: ${C.clHighlight('no entries')}`)
} else {
log(
` ${C.clDateAgo(`- ${date}`)}: ${C.clHighlight(
`${entries} entries,`
)} ${C.clDuration(`[${formatDuration(duration)}]`)}`
)
return
}

log(
` ${C.clDate(`- ${dateWeekday} ${dateString}`)}: ${C.clHighlight(
`${entries} entries,`
)} ${C.clDuration(`[${formatDuration(duration)}]`)}`
)
})

if (i < sheetNames.length - 1) {
Expand Down
1 change: 1 addition & 0 deletions src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare module 'time-speak'
declare module 'weekday'
declare module 's-ago'
4 changes: 2 additions & 2 deletions src/print/columns/get_sheet_entry_columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getSheetEntryColumns = (
): string[] => {
const { id, start, end, description } = entry
const idUI = C.clID(`${id}`)
const startUI = C.clDateAgo(
const startUI = C.clDate(
printDateAgo ? ago(start) : new Date(start).toLocaleDateString()
)
const finalEnd = end === null ? new Date() : end
Expand All @@ -25,7 +25,7 @@ const getSheetEntryColumns = (
const endUI =
end === null
? ''
: C.clDateAgo(
: C.clDate(
printDateAgo ? ago(start) : new Date(end).toLocaleDateString()
)

Expand Down
4 changes: 2 additions & 2 deletions src/print/sheet_entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const printSheetEntry = (
): void => {
const { id, start, end, description } = entry
const idUI = C.clID(`${id}`)
const startUI = C.clDateAgo(
const startUI = C.clDate(
printDateAgo ? ago(start) : new Date(start).toLocaleDateString()
)
const finalEnd = end === null ? new Date() : end
Expand All @@ -26,7 +26,7 @@ const printSheetEntry = (
const endUI =
end === null
? ''
: C.clDateAgo(
: C.clDate(
printDateAgo ? ago(start) : new Date(end).toLocaleDateString()
)

Expand Down

0 comments on commit 982e27f

Please sign in to comment.