<commit_before>'use strict';

const Grid = require('term-grid');

module.exports = tools => {
  /**
   * Format a number for nice table outputs.
   *
   * @param num {String|Number}
   *   Convert a value to its number representation with 2 decimal places.
   *
   * @returns {String}
   *   The formatted number.
   */
  const format = num => Number(num).toFixed(2);

  /**
   * Wrap the given number in a tools.chalk color
   *
   * @param num {Number}
   *   The Number to wrap, based upon is +/- value.
   *
   * @returns {String}
   *   The color formatted number.
   */
  const color = num =>
    num < 0 ? tools.chalk.black.bgRed(num) : tools.chalk.black.bgGreen(num);

  tools.getPortfolio().then(portfolio => {
    Promise.all(Object.keys(portfolio.stocks).map(tools.getSymbol))
      .then(data => {
        // Build the data grid for all the stocks in the portfolio.
        const output = [
          ['Symbol', 'Name', 'Price', 'Shares', 'Cost', 'Total $', 'G/L $'].map(
            label => tools.chalk.underline.bold(label)
          )
        ];
        let grandTotal = 0;
        let grandCost = 0;

        data.forEach(({ symbol, name, currentPrice }) => {
          let total = 0;
          let cost = 0;
          let count = 0;
          const orders = portfolio.stocks[symbol].orders;
          orders.forEach(({ shares, price }) => {
            total += shares * currentPrice;
            cost += shares * price;
            count += shares;
          });

          // Update the global amounts.
          grandTotal += total;
          grandCost += cost;

          // Build the data grid row.
          output.push([
            symbol,
            name,
            format(currentPrice),
            count,
            format(cost),
            format(total),
            color(format(total - cost))
          ]);
        });

        // Add the Money/running total to the data grid.
        const cash =
          portfolio.hasOwnProperty('money') &&
          portfolio.money.hasOwnProperty('usd') &&
          portfolio.money.usd
            ? Number(portfolio.money.usd)
            : 0;
        output.push(['', '', '', '', '', '', '']);
        output.push(['Cash', '', '', '', '', '', format(cash)]);
        output.push([
          'Total',
          '',
          '',
          '',
          format(grandCost),
          format(grandTotal),
          color(format(grandTotal - grandCost))
        ]);

        // Build and render the data grid before exiting.
        let grid = new Grid(output);
        grid.setAlign([
          'left',
          'left',
          'right',
          'right',
          'right',
          'right',
          'right'
        ]);
        grid.draw();
      })
      .catch(err => {<commit_msg>Remove the cash output from the simplified display.<commit_after>'use strict';

const Grid = require('term-grid');

module.exports = tools => {
  /**
   * Format a number for nice table outputs.
   *
   * @param num {String|Number}
   *   Convert a value to its number representation with 2 decimal places.
   *
   * @returns {String}
   *   The formatted number.
   */
  const format = num => Number(num).toFixed(2);

  /**
   * Wrap the given number in a tools.chalk color
   *
   * @param num {Number}
   *   The Number to wrap, based upon is +/- value.
   *
   * @returns {String}
   *   The color formatted number.
   */
  const color = num =>
    num < 0 ? tools.chalk.black.bgRed(num) : tools.chalk.black.bgGreen(num);

  tools.getPortfolio().then(portfolio => {
    Promise.all(Object.keys(portfolio.stocks).map(tools.getSymbol))
      .then(data => {
        // Build the data grid for all the stocks in the portfolio.
        const output = [
          ['Symbol', 'Name', 'Price', 'Shares', 'Cost', 'Total $', 'G/L $'].map(
            label => tools.chalk.underline.bold(label)
          )
        ];
        let grandTotal = 0;
        let grandCost = 0;

        data.forEach(({ symbol, name, currentPrice }) => {
          let total = 0;
          let cost = 0;
          let count = 0;
          const orders = portfolio.stocks[symbol].orders;
          orders.forEach(({ shares, price }) => {
            total += shares * currentPrice;
            cost += shares * price;
            count += shares;
          });

          // Update the global amounts.
          grandTotal += total;
          grandCost += cost;

          // Build the data grid row.
          output.push([
            symbol,
            name,
            format(currentPrice),
            count,
            format(cost),
            format(total),
            color(format(total - cost))
          ]);
        });

        output.push(
          ['', '', '', '', '', '', ''],
          [
            'Total',
            '',
            '',
            '',
            format(grandCost),
            format(grandTotal),
            color(format(grandTotal - grandCost))
          ]
        );

        // Build and render the data grid before exiting.
        let grid = new Grid(output);
        grid.setAlign([
          'left',
          'left',
          'right',
          'right',
          'right',
          'right',
          'right'
        ]);
        grid.draw();
      })
      .catch(err => {
{'fraction_non_alphanumeric': 0.1411742352270568, 'fraction_numerical': 0.003230096902907087, 'mean_word_length': 1.5454545454545454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '454359', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1454, 'n_words': 381}