<commit_before>'use strict';

var remove   = require('es5-ext/lib/Array/prototype/remove')
  , d        = require('es5-ext/lib/Object/descriptor')
  , extend   = require('es5-ext/lib/Object/extend')
  , repeat   = require('es5-ext/lib/String/prototype/repeat')
  , memoize  = require('memoizee')
  , deferred = require('deferred')
  , clc      = require('cli-color')
  , Base     = require('./console').Reporter

  , floor = Math.floor, max = Math.max, min = Math.min
  , now = Date.now
  , nextTick = process.nextTick

  , Reporter, distColumns, areReportsFullyExposed;

// Distributes reports per column e.g. [6, 6, 7], [2, 3, 3, 3]
distColumns = function (maxColumns, length, maxReports) {
	var base, rest, result, index;
	result = [];

	base = floor(length / maxColumns);

	if (!base) {
		while (length--) {
			result.push(1);
		}
		return result;
	} else if (base >= maxReports) {
		base = maxReports;
		rest = 0;
	} else {
		rest = length - base * maxColumns;
	}

	while (maxColumns--) {
		result.push(base);
	}
	if (rest) {
		index = result.length - 1;
		while (rest--) {
			result[index] = result[index] + 1;
			--index;
		}
	}
	return result;
};

areReportsFullyExposed = function (cells) {
	return cells.every(function (cell) {
		return cell.report.length === cell.length;
	});
};

Reporter = function () {
	Base.apply(this, arguments);

	this.totalWidth = clc.width;
	this.totalHeight = clc.height;
	this.pending = [];
	this.current = [];
	this.pending.remove = remove;
	this.errored = {};
};
Reporter.prototype = Object.create(Base.prototype, extend({
	init: d(function () {
		var reports = {}, updateMap = {}, waiting = [];

		this.data.on('change', function (event) {
			if (event.type === 'remove') {
				if (reports[event.name]) {
					this.subtract(reports[event.name]);
					delete reports[event.name];
				}
				return;
			} else if (event.type === 'update') {
				this.subtract(reports[event.name]);
			}
			this.add(reports[event.name] = event.report);
		}.bind(this));

		// Reset
		this.log(clc.reset);

		// Bar
		this.log(clc.moveTo(0, 2) +
			this.emap.bar(repeat.call('-', this.totalWidth)));

		// Status
		this.logStatus();

		// Columns
		this.maxColumns = max(floor(this.totalWidth / 90), 1);
		this.columnHeight = this.totalHeight - 3;
		this.maxPerColumn = floor(this.columnHeight / 6);

		this.data.on('change', function (event) {
			var promise, time;
			time = updateMap[event.name] = now();
			if (event.report && event.report.length) {
				promise = this.normalize(event.report)(function (report) {
					if (time !== updateMap[event.name]) {
						// Race condition, abort
						return;
					}
					if (!report) {
						this.subtract(event.report);
						this.logStatus();
						if (this.errored[event.name]) {
							this.pending.remove(event.name);
							delete this.errored[event.name];
							this.logReports();
						}
					} else {
						if (!this.errored[event.name]) {
							this.pending.push(event.name);
						}
						this.errored[event.name] = event.report;
						this.logReports();
					}
				}.bind(this));
				if (!this.data.resolved) {
					waiting.push(promise);
				}
			} else if (this.errored[event.name]) {
				this.pending.remove(event.name);
				delete this.errored[event.name];
				this.logReports();
			}
			this.logStatus();
		}.bind(this));

		return this.data(function (data) {
			if (this.options.stream) {
				return deferred.map(waiting);
			} else {
				return deferred.map(data, function (report, name) {
					this.add(reports[name] = report);
					if (report.length) {
						return this.normalize(report)(function (nreport) {
							if (!nreport) {
								this.subtract(report);
								delete reports[name];
								return;
							}
							this.logReports();
						}.bind(this));
					}
					this.logStatus();
				}.bind(this));
			}
		}.bind(this))(this.data);
	}),
	redraw: d(function () {
		var dist, columns, count, redrawAll, index, top, cls;

		if (!this.pending.length) {
			// No reports
			if (this.current.length) {
				// Clear screen
				top = 0;
				cls = repeat.call(' ', this.totalWidth);
				while (top < this.columnHeight) {
					this.log(clc.moveTo(0, top++ + 3) + cls);
				}<commit_msg>Fix no-stream support in watch console<commit_after>'use strict';

var remove   = require('es5-ext/lib/Array/prototype/remove')
  , d        = require('es5-ext/lib/Object/descriptor')
  , extend   = require('es5-ext/lib/Object/extend')
  , toArray  = require('es5-ext/lib/Object/map-to-array')
  , repeat   = require('es5-ext/lib/String/prototype/repeat')
  , memoize  = require('memoizee')
  , deferred = require('deferred')
  , clc      = require('cli-color')
  , Base     = require('./console').Reporter

  , floor = Math.floor, max = Math.max, min = Math.min
  , now = Date.now
  , nextTick = process.nextTick

  , Reporter, distColumns, areReportsFullyExposed;

// Distributes reports per column e.g. [6, 6, 7], [2, 3, 3, 3]
distColumns = function (maxColumns, length, maxReports) {
	var base, rest, result, index;
	result = [];

	base = floor(length / maxColumns);

	if (!base) {
		while (length--) {
			result.push(1);
		}
		return result;
	} else if (base >= maxReports) {
		base = maxReports;
		rest = 0;
	} else {
		rest = length - base * maxColumns;
	}

	while (maxColumns--) {
		result.push(base);
	}
	if (rest) {
		index = result.length - 1;
		while (rest--) {
			result[index] = result[index] + 1;
			--index;
		}
	}
	return result;
};

areReportsFullyExposed = function (cells) {
	return cells.every(function (cell) {
		return cell.report.length === cell.length;
	});
};

Reporter = function () {
	Base.apply(this, arguments);

	this.totalWidth = clc.width;
	this.totalHeight = clc.height;
	this.pending = [];
	this.current = [];
	this.updateMap = {};
	this.pending.remove = remove;
	this.errored = {};
};
Reporter.prototype = Object.create(Base.prototype, extend({
	init: d(function () {
		var reports = {}, waiting = [];

		this.data.on('change', function (event) {
			if (event.type === 'remove') {
				if (reports[event.name]) {
					this.subtract(reports[event.name]);
					delete reports[event.name];
				}
				return;
			} else if (event.type === 'update') {
				this.subtract(reports[event.name]);
			}
			this.add(reports[event.name] = event.report);
		}.bind(this));

		// Reset
		this.log(clc.reset);

		// Bar
		this.log(clc.moveTo(0, 2) +
			this.emap.bar(repeat.call('-', this.totalWidth)));

		// Status
		this.logStatus();

		// Columns
		this.maxColumns = max(floor(this.totalWidth / 90), 1);
		this.columnHeight = this.totalHeight - 3;
		this.maxPerColumn = floor(this.columnHeight / 6);

		this.data.on('change', function (event) {
			var promise = this._processReport(event.report, event.name, reports);
			if (promise) {
				if (!this.data.resolved) waiting.push(promise);
				else promise.done();
			}
		}.bind(this));

		return this.data(function (data) {
			if (this.options.stream) {
				return deferred.map(waiting);
			} else {
				return deferred.map(toArray(data, function (report, name) {
					this.add(reports[name] = report);
					return this._processReport(report, name, reports);
				}, this));
			}
		}.bind(this))(this.data);
	}),
	_processReport: d(function (report, name, reports) {
		var time;
		time = this.updateMap[name] = now();
		if (report && report.length) {
			return this.normalize(report)(function (resReport) {
				if (time !== this.updateMap[name]) return; // Race condition, abort
				if (!resReport) {
					this.subtract(report);
					delete reports[name];
					this.logStatus();
					if (this.errored[name]) {
						this.pending.remove(name);
						delete this.errored[name];
						this.logReports();
					}
				} else {
					if (!this.errored[name]) this.pending.push(name);
					this.errored[name] = report;
					this.logReports();
				}
			}.bind(this));
		} else if (this.errored[name]) {
			this.pending.remove(name);
			delete this.errored[name];
			this.logReports();
		}
		this.logStatus();
	}),
	redraw: d(function () {
		var dist, columns, count, redrawAll, index, top, cls;

		if (!this.pending.length) {
			// No reports
			if (this.current.length) {
				// Clear screen
				top = 0;
				cls = repeat.call(' ', this.totalWidth);
				while (top < this.columnHeight) {
					this.log(clc.moveTo(0, top++ + 3) + cls);
				}
{'fraction_non_alphanumeric': 0.1725637000362275, 'fraction_numerical': 0.006158676488346818, 'mean_word_length': 2.9551098376313276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3603299', 'n_tokens_mistral': 3455, 'n_tokens_neox': 2870, 'n_words': 568}