Skip to content

Commit

Permalink
feat: improvements to the ls command (#52)
Browse files Browse the repository at this point in the history
* feat: switch ls output to cli-table

* fix: sorts time entries oldest to newest

Sorts the time entries so that the last ones displayed in the table are
the most recent

* feat(ls): adds option to change number of days

New `-d` option to display that number of days in the report

* chore: adds debug to ls command

* feat(ls): ability to search for time entries

Any strings after the ls command will be used to search and filter the
returned results

* test: adds node 18.x
  • Loading branch information
beauraines authored Mar 19, 2023
1 parent 1720c03 commit 5c9effc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [16.x,18.x]

steps:
- uses: actions/checkout@v3
Expand Down
34 changes: 27 additions & 7 deletions cmds/ls.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import Client from '../client.js'
import { convertUtcTime, formatDuration } from '../utils.js'
import dayjs from 'dayjs'
import debugClient from 'debug'
import Table from 'cli-table3'

export const command = 'ls'
export const desc = 'Lists time entries'
const debug = debugClient('toggl-cli-ls')

export const builder = {
export const command = 'ls [searchStrings...]'
export const desc = 'Lists recent time entries. Defaults to the last 14 days.'

export const builder = {
d: { alias: ['days'], describe: 'The number of days to return.', type: 'number', demandOption: false, default: 14 }
}

export const handler = async function (argv) {
debug(argv)
const client = Client()
// TODO update these dates
const timeEntries = await client.timeEntries.list({start_date:dayjs().subtract(14,'days').toISOString(),end_date:dayjs().toISOString()});

const days = argv.days
let timeEntries = await client.timeEntries.list({
start_date: dayjs().subtract(days, 'days').startOf('day').toISOString(),
end_date: dayjs().toISOString()
})
timeEntries.sort((a, b) => (a.start > b.start) ? 1 : -1)
if (argv.searchStrings) {
const searchString = argv.searchStrings.join(' ')
debug(searchString)
timeEntries = timeEntries.filter(x => x.description.includes(searchString))
}
const report = []
timeEntries.forEach(element => {
report.push(
Expand All @@ -26,5 +39,12 @@ export const handler = async function (argv) {
)
})

console.table(report, ['description', 'start', 'stop', 'duration'])
const table = new Table({
head: ['description', 'start', 'stop', 'duration']
})
for (const entry of report) {
table.push([entry.description, entry.start, entry.stop, entry.duration])
}
console.log(table.toString())
// console.table(report, ['description', 'start', 'stop', 'duration'])
}
12 changes: 6 additions & 6 deletions cmds/ls.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('ls command',() => {
it.todo('should get tasks for the last 14 days')
// get time entries
// sort them
// check that the earliest is within 14 days
})
describe('ls command', () => {
it.todo('should get tasks for the last 14 days')
// get time entries
// sort them
// check that the earliest is within 14 days
})

0 comments on commit 5c9effc

Please sign in to comment.