<commit_before>/*
 * This file is part of yoob.js version 0.11-PRE
 * Available from https://github.com/catseye/yoob.js/
 * This file is in the public domain.  See http://unlicense.org/ for details.
 */
if (window.yoob === undefined) yoob = {};

/*
 * A text-based-console simulation in Javascript.  It is, in actuality, a
 * facade for a yoob.Playfield and a yoob.Cursor.
 *
 * The yoob.TextTerminal has no concern for display; in MVC terms it is a
 * "model" and you will need to use a "view" such as yoob.PlayfieldCanvasView
 * or yoob.PlayfieldHTMLView to display it.  (There is a convenience method on
 * to create such an object.)
 *
 * Create a new yoob.TextTerminal object t, then call t.init({ columns: 80, rows: 25}),
 * then call t.write() to write text to the console.
 *
 * You can also call t.setColors() to set the text and background colors of
 * the next text to be written, between calls to t.write().  You can call
 * t.reset() to clear the simulated screen (to the selected backgroundColor.)
 *
 * Because the TextTerminal is backed by a Playfield, you can read characters and
 * colors at any position on the console.
 *
 * Note that this is not a completely "dumb" or "raw" console.  Some methods do
 * understand terminal control codes.  But you don't have to use them.
 */
yoob.TextTerminal = function() {
    
    // Inner Class
    var upper = this;
    var ConsoleCell = function() {        
        this.init = function(c, tc, bc) {
            this.character = c;
            this.textColor = tc;
            this.backgroundColor = bc;
            return this;
        };
        
        this.draw = function(ctx, pfX, pfY, canvasX, canvasY,
                             cellWidth, cellHeight) {
            // only draw the background if the cursor is not here
            if (upper.cursor.x !== pfX || upper.cursor.y !== pfY) {
                ctx.fillStyle = this.backgroundColor;
                ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
            }
            ctx.fillStyle = this.textColor;
            ctx.fillText(this.character, canvasX, canvasY);
        };
    };

    this.init = function(cfg) {
        cfg = cfg || {};
        this.rows = cfg.rows || 25;
        this.cols = cfg.columns || 80;
        this.textColor = cfg.textColor || "green";
        this.backgroundColor = cfg.backgroundColor || "black";
        this.cursor = new yoob.Cursor().init({ dx: 1 });
        this.defaultCell = new ConsoleCell().init(' ', 'green', 'black');
        this.pf = new yoob.Playfield(),init({
            defaultValue: this.defaultCell,
            cursors: [this.cursor]
        });
        this.reset();
        return this;
    };

    // convenience function
    this.createPlayfieldCanvasView = function(canvas, cellWidth, cellHeight) {
        var view = new yoob.PlayfieldCanvasView().init({
            playfield: this.pf,
            canvas: canvas,
            cellWidth: cellWidth,
            cellHeight: cellHeight,
            fixedPosition: true
        });
        var $this = this;
        view.getLowerX = function() { return 0; };
        view.getLowerY = function() { return 0; };
        view.getUpperX = function() { return $this.cols - 1; };
        view.getUpperY = function() { return $this.rows - 1; };
        return view;
    };

    this.setColors = function(textColor, backgroundColor) {
        if (textColor !== undefined) {
            this.textColor = textColor;
        }
        if (backgroundColor !== undefined) {
            this.backgroundColor = backgroundColor;
        }<commit_msg>Fix eg/text-terminal.html, but (a) why slow? (b) why expanding?<commit_after>/*
 * This file is part of yoob.js version 0.11-PRE
 * Available from https://github.com/catseye/yoob.js/
 * This file is in the public domain.  See http://unlicense.org/ for details.
 */
if (window.yoob === undefined) yoob = {};

/*
 * A text-based-console simulation in Javascript.  It is, in actuality, a
 * facade for a yoob.Playfield and a yoob.Cursor.
 *
 * The yoob.TextTerminal has no concern for display; in MVC terms it is a
 * "model" and you will need to use a "view" such as yoob.PlayfieldCanvasView
 * or yoob.PlayfieldHTMLView to display it.  (There is a convenience method on
 * to create such an object.)
 *
 * Create a new yoob.TextTerminal object t, then call t.init({ columns: 80, rows: 25}),
 * then call t.write() to write text to the console.
 *
 * You can also call t.setColors() to set the text and background colors of
 * the next text to be written, between calls to t.write().  You can call
 * t.reset() to clear the simulated screen (to the selected backgroundColor.)
 *
 * Because the TextTerminal is backed by a Playfield, you can read characters and
 * colors at any position on the console.
 *
 * Note that this is not a completely "dumb" or "raw" console.  Some methods do
 * understand terminal control codes.  But you don't have to use them.
 */
yoob.TextTerminal = function() {
    
    // Inner Class
    var upper = this;
    var ConsoleCell = function() {        
        this.init = function(c, tc, bc) {
            this.character = c;
            this.textColor = tc;
            this.backgroundColor = bc;
            return this;
        };
        
        this.draw = function(ctx, pfX, pfY, canvasX, canvasY,
                             cellWidth, cellHeight) {
            // only draw the background if the cursor is not here
            if (upper.cursor.x !== pfX || upper.cursor.y !== pfY) {
                ctx.fillStyle = this.backgroundColor;
                ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
            }
            ctx.fillStyle = this.textColor;
            ctx.fillText(this.character, canvasX, canvasY);
        };
    };

    this.init = function(cfg) {
        cfg = cfg || {};
        this.rows = cfg.rows || 25;
        this.cols = cfg.columns || 80;
        this.textColor = cfg.textColor || "green";
        this.backgroundColor = cfg.backgroundColor || "black";
        this.cursor = new yoob.Cursor().init({ dx: 1 });
        this.defaultCell = new ConsoleCell().init(' ', 'green', 'black');
        this.pf = new yoob.Playfield().init({
            defaultValue: this.defaultCell,
            cursors: [this.cursor]
        });
        this.reset();
        return this;
    };

    // convenience function
    this.createPlayfieldCanvasView = function(canvas, cellWidth, cellHeight) {
        var view = new yoob.PlayfieldCanvasView().init({
            playfield: this.pf,
            canvas: canvas,
            cellWidth: cellWidth,
            cellHeight: cellHeight,
            fixedPosition: true
        });
        var $this = this;
        view.getLowerX = function() { return 0; };
        view.getLowerY = function() { return 0; };
        view.getUpperX = function() { return $this.cols - 1; };
        view.getUpperY = function() { return $this.rows - 1; };
        return view;
    };

    this.setColors = function(textColor, backgroundColor) {
        if (textColor !== undefined) {
            this.textColor = textColor;
        }
        if (backgroundColor !== undefined) {
            this.backgroundColor = backgroundColor;
        }
{'fraction_non_alphanumeric': 0.1121599111604664, 'fraction_numerical': 0.004441976679622432, 'mean_word_length': 2.167032967032967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5493250', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2026, 'n_words': 680}