<commit_before>var spr_editor = $('#sprite-editor')[0];

// Filter the files opened with the compiler
var loader = new ui.SpriteLoader(spr_editor);

var ide = {
    source: $('#asm')[0],
    files_opened: [],
    init: function(){
        compiler.set_open_file_handle(this.compiler_open_file);
        $(function() {
            ide.codemirror = CodeMirror.fromTextArea($("#asm")[0], {
                lineNumbers: true,
                matchBrackets: true,
                useCPP: true,
                mode: "text/x-asm",
                onChange: scheduleUpdate
                });
        });
    },
    open_file: function(file){
        $.get(file, function(data) {
            var regex = /([a-z\/]+\/)([a-z\d]+\.asm)/;
            var m  = regex.exec(file);
            if (m) {
                files_opened = [];
                compiler.path = m[1];
                ide.codemirror.setValue(data);
                update();
                for (var f in this.files_opened){
                    if (this.files_opened[f].match(/\.chr$/)){
                        loader.load(this.files_opened[f]);
                        break;
                    }
                }
            }
        });
    },
    compiler_open_file: function(file){
        this.files_opened.push(file);
        if (loader.file == file){
            return String.fromCharCode.apply(undefined, loader.sprites);
        } else {
            return compiler.default_open_file(file);
        }
    }
};


ide.init();


$("#source_files").change(function() {
      var value = $(this).val();
      ide.open_file(value);
});

function update(){
    clearTimeout(_idleTimer);
    console.log('compilling');
    var data;
    try {
        data = compiler.nes_compiler(ide.codemirror.getValue());
        _nes.loadRom(data);
        _nes.start();
    } catch (e){
        console.log(e);
    }
}

var _idleTimer = null;

function scheduleUpdate(){
    if (_idleTimer !== null){
        clearTimeout(_idleTimer);
    }
    _idleTimer = setTimeout(update, 3000);
}

var _nes;<commit_msg>added basic file download with chrome<commit_after>var spr_editor = $('#sprite-editor')[0];

// Filter the files opened with the compiler
var loader = new ui.SpriteLoader(spr_editor);

window.requestFileSystem  = window.MozBlobBuilder || window.webkitRequestFileSystem || window.requestFileSystem;
BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder || window.BlobBuilder;

var ide = {
    files_opened: [],
    init: function(){
        compiler.set_open_file_handle(this.compiler_open_file);
        this.init_fs();
        $(function() {
            ide.codemirror = CodeMirror.fromTextArea($("#asm")[0], {
                lineNumbers: true,
                matchBrackets: true,
                useCPP: true,
                mode: "text/x-asm",
                onChange: scheduleUpdate
                });
        });
    },
    errorFileHandler: function (e) {
        var msg = '';
        switch (e.code) {
            case FileError.QUOTA_EXCEEDED_ERR:
              msg = 'QUOTA_EXCEEDED_ERR';
              break;
            case FileError.NOT_FOUND_ERR:
              msg = 'NOT_FOUND_ERR';
              break;
            case FileError.SECURITY_ERR:
              msg = 'SECURITY_ERR';
              break;
            case FileError.INVALID_MODIFICATION_ERR:
              msg = 'INVALID_MODIFICATION_ERR';
              break;
            case FileError.INVALID_STATE_ERR:
              msg = 'INVALID_STATE_ERR';
              break;
            default:
              msg = 'Unknown Error';
              break;
        }
        console.log('Error: ' + msg);
    },
    onInitFs: function(fs){
        ide._fs = fs;
    },
    init_fs: function (grantedBytes) {
        window.requestFileSystem(TEMPORARY, 1024*1024, ide.onInitFs, ide.errorFileHandler, function(e) {console.log('Error', e);});
    },
    write_nesfile: function (filename, data){
        if (this._fs === undefined){
            this.init_fs();
        } else {
            var buf = new ArrayBuffer(data.length);
            var uint = new Uint8Array(buf);
            for (var i=0, strLen=data.length; i<strLen; i++) {
                uint[i] = data.charCodeAt(i);
            }
            this._fs.root.getFile(filename, {create: true}, function(fileEntry) {
                console.log(fileEntry.toURL());
                fileEntry.createWriter(function(fileWriter) {
                    fileWriter.onwriteend = function(e) {
                        console.log('Write completed.');
                    };
                    fileWriter.onerror = function(e) {
                        console.log('Write failed: ' + e.toString());
                    };
                    var bb = new BlobBuilder();
                    bb.append(uint.buffer);
                    fileWriter.write(bb.getBlob("application/octet-stream"));
                }, ide.errorHandler);
            }, ide.errorHandler);
        }
    },
    load_file: function(file){
        $.get(file, function(data) {
            var regex = /([a-z\/]+\/)([a-z\d]+\.asm)/;
            var m  = regex.exec(file);
            if (m) {
                files_opened = [];
                compiler.path = m[1];
                ide.codemirror.setValue(data);
                update();
                for (var f in this.files_opened){
                    if (this.files_opened[f].match(/\.chr$/)){
                        loader.load(this.files_opened[f]);
                        break;
                    }
                }
            }
        });
    },
    compiler_open_file: function(file){
        this.files_opened.push(file);
        if (loader.file == file){
            return String.fromCharCode.apply(undefined, loader.sprites);
        } else {
            return compiler.default_open_file(file);
        }
    }
};

ide.init();

$("#source_files").change(function() {
      var value = $(this).val();
      ide.load_file(value);
});

function update(){
    clearTimeout(_idleTimer);
    console.log('compilling');
    var data;
    try {
        data = compiler.nes_compiler(ide.codemirror.getValue());
        ide.write_nesfile('file.nes', data);
        _nes.loadRom(data);
        _nes.start();
    } catch (e){
        console.log(e);
    }
}

var _idleTimer = null;

function scheduleUpdate(){
    if (_idleTimer !== null){
        clearTimeout(_idleTimer);
    }
    _idleTimer = setTimeout(update, 3000);
}

var _nes;
{'fraction_non_alphanumeric': 0.13389830508474576, 'fraction_numerical': 0.003852080123266564, 'mean_word_length': 1.6569791240278346, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '593668', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1856, 'n_words': 329}