Skip to content

Commit

Permalink
feat: scale cli table width (#14)
Browse files Browse the repository at this point in the history
* feat: scale day status table to terminal width

* feat: scale timer table to terminal width
  • Loading branch information
simonrauch authored Mar 18, 2022
1 parent e3b3065 commit 1495d23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/cli/cli-output/day.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import Table from 'cli-table';
import { formatHours } from '.';
import { formatHours, getTableWidth } from '.';
import { HarvestTimeEntry } from '../../business/harvest';

export function printTimeEntryTable(timeEntries: HarvestTimeEntry[]): void {
const totalWidth = getTableWidth();
let totalTime = 0;
const table = new Table({
head: ['ID', 'Task', 'Notes', 'Time'],
colWidths: [4, 49, 20, 7],
colWidths: [4, Math.floor((totalWidth - 11) * 0.65), Math.floor((totalWidth - 11) * 0.35), 7],
});

timeEntries.forEach((timeEntry, index) => {
totalTime += timeEntry.hours;
table.push([index, timeEntry.task?.name ?? '', timeEntry.notes ?? '', formatHours(timeEntry.hours)]);
});
process.stdout.write(table.toString() + '\n');
process.stdout.write(
' Sum: ' + formatHours(totalTime) + '\n\n',
);
process.stdout.write(' Sum:' + new Array(totalWidth - 7).join(' ') + formatHours(totalTime) + '\n\n');
}
12 changes: 12 additions & 0 deletions src/cli/cli-output/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import yargs from 'yargs';

export function printMessage(message: string): void {
process.stdout.write(message + '\n');
}
Expand All @@ -8,3 +10,13 @@ export function formatHours(hours: number): string {

return `${fullHours}:${String(Math.floor(minutes)).padStart(2, '0')}`;
}

export function getTableWidth(): number {
const terminalWidth = yargs.terminalWidth() - 4;
const maxWidth = 156;
const minWidth = 36;

if (terminalWidth > maxWidth) return maxWidth;
if (terminalWidth < minWidth) return minWidth;
return terminalWidth;
}
5 changes: 3 additions & 2 deletions src/cli/cli-output/timer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Table from 'cli-table';
import { formatHours } from '.';
import { formatHours, getTableWidth } from '.';
import { HarveyTimer, HarveyTimerStatus } from '../../business/timer';

export function printTimer(timer: HarveyTimer): void {
const timeEntry = timer.timeEntry;
let tableHead: string[] = ['Status'];
let colWidth: number[] = [9];
let tableRow: string[] = [getStatusString(timer.status)];
const totalWidth = getTableWidth();

if (timeEntry && timeEntry.task) {
tableHead = tableHead.concat(['Task', 'Notes', 'Timer']);
colWidth = colWidth.concat([44, 20, 7]);
colWidth = colWidth.concat([Math.floor((totalWidth - 17) * 0.65), Math.floor((totalWidth - 17) * 0.35), 7]);
tableRow = tableRow.concat([timeEntry.task.name, (timeEntry.notes ??= ''), formatHours(timeEntry.hours)]);
}

Expand Down

0 comments on commit 1495d23

Please sign in to comment.