# All files / src formatters.ts

13.95% Statements 6/43
8.33% Branches 2/24
28.57% Functions 2/7
14.63% Lines 6/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153

    8x
    8x

    8x
    4x

    4x

    4x

     import chalk from 'chalk';
    import Table from 'cli-table3';
    import { Session, SessionStats, FileChange, Commit } from './types';
    import { formatDistanceToNow, format } from 'date-fns';

    export function formatDuration(seconds: number): string {
      const hours = Math.floor(seconds / 3600);
      const minutes = Math.floor((seconds % 3600) / 60);

      if (hours > 0) {
        return `${hours}h ${minutes}m`;
      }
      return `${minutes}m`;
    }

    export function formatCost(cost: number): string {
      return `$${cost.toFixed(2)}`;
    }

    export function displaySession(session: Session): void {
      console.log(chalk.bold.cyan(`\nSession: ${session.name}\n`));

      const table = new Table({
        head: [chalk.cyan.bold('Metric'), chalk.cyan.bold('Value')],
        style: { head: [], border: [] },
      });

      table.push(
        ['Status', session.status === 'active' ? chalk.green('Active') : chalk.gray('Completed')],
        ['Started', format(new Date(session.startTime), 'MMM dd, yyyy HH:mm')],
      );

      if (session.endTime) {
        table.push(['Ended', format(new Date(session.endTime), 'MMM dd, yyyy HH:mm')]);
      }

      if (session.duration) {
        table.push(['Duration', chalk.white(formatDuration(session.duration))]);
      }

      table.push(
        ['Files Changed', chalk.white(session.filesChanged.toString())],
        ['Commits', chalk.white(session.commits.toString())],
        ['AI Tokens', chalk.white(session.aiTokens.toLocaleString())],
        ['AI Cost', chalk.yellow(formatCost(session.aiCost))],
      );

      if (session.notes) {
        table.push(['Notes', chalk.gray(session.notes)]);
      }

      console.log(table.toString());
    }

    export function displaySessions(sessions: Session[]): void {
      if (sessions.length === 0) {
        console.log(chalk.yellow('\nNo sessions found.\n'));
        return;
      }

      const table = new Table({
        head: [
          chalk.cyan.bold('ID'),
          chalk.cyan.bold('Name'),
          chalk.cyan.bold('Started'),
          chalk.cyan.bold('Duration'),
          chalk.cyan.bold('Files'),
          chalk.cyan.bold('Commits'),
          chalk.cyan.bold('AI Cost'),
        ],
        style: { head: [], border: [] },
      });

      for (const session of sessions) {
        table.push([
          chalk.gray(`#${session.id}`),
          session.status === 'active' ? chalk.green(session.name) : chalk.white(session.name),
          formatDistanceToNow(new Date(session.startTime), { addSuffix: true }),
          session.duration ? formatDuration(session.duration) : chalk.gray('ongoing'),
          session.filesChanged.toString(),
          session.commits.toString(),
          chalk.yellow(formatCost(session.aiCost)),
        ]);
      }

      console.log('\n' + table.toString() + '\n');
    }

    export function displayStats(stats: SessionStats): void {
      console.log(chalk.bold.cyan('\nOverall Stats\n'));

      const table = new Table({
        head: [chalk.cyan.bold('Metric'), chalk.cyan.bold('Value')],
        style: { head: [], border: [] },
      });

      table.push(
        ['Total Sessions', chalk.white(stats.totalSessions.toLocaleString())],
        ['Total Time', chalk.white(formatDuration(stats.totalTime))],
        ['Average Session', chalk.white(formatDuration(stats.avgSessionTime))],
        ['Files Changed', chalk.white(stats.totalFiles.toLocaleString())],
        ['Commits', chalk.white(stats.totalCommits.toLocaleString())],
        ['Total AI Cost', chalk.yellow(formatCost(stats.totalAICost))],
      );

      console.log(table.toString() + '\n');
    }

    export function displayFileChanges(changes: FileChange[]): void {
      if (changes.length === 0) return;

      console.log(chalk.bold.cyan('\nFile Changes\n'));

      const table = new Table({
        head: [chalk.cyan.bold('Type'), chalk.cyan.bold('File'), chalk.cyan.bold('Time')],
        style: { head: [], border: [] },
      });

      for (const change of changes) {
        const typeColor = change.changeType === 'created' ? chalk.green :
                          change.changeType === 'modified' ? chalk.yellow : chalk.red;

        table.push([
          typeColor(change.changeType),
          change.filePath,
          formatDistanceToNow(new Date(change.timestamp), { addSuffix: true }),
        ]);
      }

      console.log(table.toString() + '\n');
    }

    export function displayCommits(commits: Commit[]): void {
      if (commits.length === 0) return;

      console.log(chalk.bold.cyan('\nCommits\n'));

      const table = new Table({
        head: [chalk.cyan.bold('Hash'), chalk.cyan.bold('Message'), chalk.cyan.bold('Time')],
        style: { head: [], border: [] },
      });

      for (const commit of commits) {
        table.push([
          chalk.gray(commit.hash),
          commit.message,
          formatDistanceToNow(new Date(commit.timestamp), { addSuffix: true }),
        ]);
      }

      console.log(table.toString() + '\n');
    }