summary refs log tree commit diff stats
path: root/doc/pydoc/ranger.gui.widgets.console_mode.html
blob: b6d0772591e7ec918b171940a6b61377141e004b (plain) (blame)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.gui.widgets.console_mode</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.gui.html"><font color="#ffffff">gui</font></a>.<a href="ranger.gui.widgets.html"><font color="#ffffff">widgets</font></a>.console_mode</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/gui/widgets/console_mode.py">/home/hut/work/ranger/ranger/gui/widgets/console_mode.py</a></font></td></tr></table>
    <p><tt>#&nbsp;Copyright&nbsp;(c)&nbsp;2009,&nbsp;2010&nbsp;hut&nbsp;&lt;hut@lavabit.com&gt;<br>
#<br>
#&nbsp;Permission&nbsp;to&nbsp;use,&nbsp;copy,&nbsp;modify,&nbsp;and/or&nbsp;distribute&nbsp;this&nbsp;software&nbsp;for&nbsp;any<br>
#&nbsp;purpose&nbsp;with&nbsp;or&nbsp;without&nbsp;fee&nbsp;is&nbsp;hereby&nbsp;granted,&nbsp;provided&nbsp;that&nbsp;the&nbsp;above<br>
#&nbsp;copyright&nbsp;notice&nbsp;and&nbsp;this&nbsp;permission&nbsp;notice&nbsp;appear&nbsp;in&nbsp;all&nbsp;copies.<br>
#<br>
#&nbsp;THE&nbsp;SOFTWARE&nbsp;IS&nbsp;PROVIDED&nbsp;"AS&nbsp;IS"&nbsp;AND&nbsp;THE&nbsp;AUTHOR&nbsp;DISCLAIMS&nbsp;ALL&nbsp;WARRANTIES<br>
#&nbsp;WITH&nbsp;REGARD&nbsp;TO&nbsp;THIS&nbsp;SOFTWARE&nbsp;INCLUDING&nbsp;ALL&nbsp;IMPLIED&nbsp;WARRANTIES&nbsp;OF<br>
#&nbsp;MERCHANTABILITY&nbsp;AND&nbsp;FITNESS.&nbsp;IN&nbsp;NO&nbsp;EVENT&nbsp;SHALL&nbsp;THE&nbsp;AUTHOR&nbsp;BE&nbsp;LIABLE&nbsp;FOR<br>
#&nbsp;ANY&nbsp;SPECIAL,&nbsp;DIRECT,&nbsp;INDIRECT,&nbsp;OR&nbsp;CONSEQUENTIAL&nbsp;DAMAGES&nbsp;OR&nbsp;ANY&nbsp;DAMAGES<br>
#&nbsp;WHATSOEVER&nbsp;RESULTING&nbsp;FROM&nbsp;LOSS&nbsp;OF&nbsp;USE,&nbsp;DATA&nbsp;OR&nbsp;PROFITS,&nbsp;WHETHER&nbsp;IN&nbsp;AN<br>
#&nbsp;ACTION&nbsp;OF&nbsp;CONTRACT,&nbsp;NEGLIGENCE&nbsp;OR&nbsp;OTHER&nbsp;TORTIOUS&nbsp;ACTION,&nbsp;ARISING&nbsp;OUT&nbsp;OF<br>
#&nbsp;OR&nbsp;IN&nbsp;CONNECTION&nbsp;WITH&nbsp;THE&nbsp;USE&nbsp;OR&nbsp;PERFORMANCE&nbsp;OF&nbsp;THIS&nbsp;SOFTWARE.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#eeaa77">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
    
<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl><dt><a name="-all_modes"><strong>all_modes</strong></a>(mode)</dt><dd><tt>Returns&nbsp;a&nbsp;generator&nbsp;containing&nbsp;all&nbsp;valid&nbsp;modes.</tt></dd></dl>
 <dl><dt><a name="-is_valid_mode"><strong>is_valid_mode</strong></a>(mode)</dt><dd><tt>Returns&nbsp;True&nbsp;or&nbsp;False&nbsp;depending&nbsp;on&nbsp;whether&nbsp;the&nbsp;mode&nbsp;is&nbsp;valid&nbsp;or&nbsp;not.</tt></dd></dl>
 <dl><dt><a name="-mode_to_class"><strong>mode_to_class</strong></a>(mode)</dt><dd><tt>Associates&nbsp;modes&nbsp;with&nbsp;the&nbsp;actual&nbsp;classes<br>
from&nbsp;ranger.gui.widgets.console.</tt></dd></dl>
</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#55aa55">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
    
<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><strong>COMMAND</strong> = 1<br>
<strong>COMMAND_QUICK</strong> = 2<br>
<strong>DEFAULT</strong> = 0<br>
<strong>OPEN</strong> = 3<br>
<strong>OPEN_QUICK</strong> = 4<br>
<strong>SEARCH</strong> = 5</td></tr></table>
</body></html>
ght .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Playground</title>
    <meta name="description" content="A JavaScript jungle-gym for doing experiments and sharing scrappy fiddles.">
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #ddd;
            padding: 10px;
            height: 100vh;
            margin: 0;
        }

        textarea {
            width: 100%;
            height: 64%;
            font-family: monospace;
            background-color: #FFFEEC;
            border: 2px solid #000;
            scrollbar-width: none;            
            font-size: 1rem;
            margin-bottom: 10px;
            padding: 10px;
            box-sizing: border-box;
            resize: none;
            border-bottom: 12px solid teal;
            -webkit-user-modify: read-write-plaintext-only;
        }

        textarea::-webkit-scrollbar {
            display: none;
        }

        textarea::selection {
            background-color: #EFECA7;
        }

        textarea:focus {
            outline: none;
        }

        #console {
            width: 100%;
            height: 22%;
            background-color: #000;
            color: #0fc;
            font-family: monospace;
            font-size: 1rem;
            overflow-y: auto;
            padding: 10px;
            box-sizing: border-box;
            white-space: pre-wrap;
        }

        .button-container {
            width: 100%;
            display: flex;
            justify-content: flex-end;
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            font-size: 1rem;
            margin-left: 10px;
            cursor: pointer;
            border: none;
            transition: background-color 0.3s ease;
        }

        button:hover, button:focus {
            outline: none; 
        }

        button.run {
            background-color: teal;
            color: #FFFFFF;
        }
    </style>
</head>
<body>

    <div class="playground" id="playground">    
        <div class="seesaw" id="seesaw"></div>
        <div class="slide" id="slide"></div>
    </div>

    <div class="button-container">
        <button onclick="clearEverything()">Clear</button>
        <button onclick="downloadCodeAndEditor()">Download</button>
        <button onclick="shareCode()">Share</button>
        <button onclick="evaluateCode()" class="run">Run Code</button>
    </div>
    <textarea id="codeInput">// Inspired by <https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html>
const matchHere = (pattern, text) => {
    if (pattern.length === 0) return true;
    
    // If pattern ends with $, match end of string
    if (pattern[0] === '
    <div id="console"></div>

    <script>
        function evaluateCode() {
            const code = document.getElementById('codeInput').value;
            const consoleDiv = document.getElementById('console');
            consoleDiv.innerHTML = '';

            // Custom console.log function to output to the console div
            const originalConsoleLog = console.log;
            console.log = function(...args) {
                args.forEach(arg => {
                    const output = document.createElement('div');
                    output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
                    consoleDiv.appendChild(output);
                });
                originalConsoleLog.apply(console, args);
            };

            try {
                eval(code);
            } catch (error) {
                const errorOutput = document.createElement('div');
                errorOutput.textContent = error;
                errorOutput.style.color = 'red';
                consoleDiv.appendChild(errorOutput);
            }

            // Restore browser's console.log
            console.log = originalConsoleLog;
        }

        function downloadCodeAndEditor() {
            const codeInput = document.getElementById('codeInput').value;
            const htmlContent = document.documentElement.outerHTML.replace(
                /<textarea id="codeInput"[^>]*>.*<\/textarea>/,
                `<textarea id="codeInput">${codeInput}</textarea>`
            );

            const blob = new Blob([htmlContent], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'code_editor.html';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }

        function shareCode() {
            const code = document.getElementById('codeInput').value;
            const encodedCode = btoa(encodeURIComponent(code));
            window.location.hash = encodedCode;
            window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
        }

        function clearEverything() {
            if (!confirm('Are you sure you want to reset the playground?')) {
                return;
            } else {               
                window.location.hash = '';
                window.location.reload();
            }
        }

        function loadCodeFromHash() {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    const decodedCode = decodeURIComponent(atob(hash));
                    document.getElementById('codeInput').value = decodedCode;
                } catch (error) {
                    console.error('Failed to decode the URL hash:', error);
                }
            }
        }

        function help() {
            const helpText = `
            Welcome to the JavaScript Playground! Here are some tips to get you started:

            1. Enter your JavaScript code in the textarea.
            2. Click the "Run Code" button to execute your code.
            3. The console output will be displayed below the textarea.
            4. Click the "Clear" button to reset the playground.
            5. Click the "Download" button to save your code and editor as an HTML file.
            6. Click the "Share" button to generate a URL to share your code with others.
            7. You can also press "Cmd + Enter" to run your code.
            8. There's an empty div above the buttons with the id "playground"
            9. You can mount stuff to it using the "mount" function, for more info run "mountHelp()"
            10. You can use the "clear()" function to clear the content's of the console.

            Go nuts! Share scrappy fiddles!
            `;
            console.log(helpText);
        }

        function clear() {
            document.getElementById('console').innerHTML = '';
        }

        function mountHelp() {
            console.log(`
            The mount function is used to mount stuff to the playground div.
            It takes a function as an argument, which in turn receives the playground div as an argument.
            Before mounting, it clears the playground div.
            Here's an example of how to use the mount function:

            mount(playground => {
                const h1 = document.createElement('h1');
                h1.textContent = 'Hell is empty and all the devils are here.';
                playground.appendChild(h1);
            });

            This will add an h1 element to the playground div.
            `);
        }

        function mount(mountFunction) {
            const playground = document.getElementById('playground');
            if (!playground) {
                console.error("Couldn't find a div with the id 'playground'! You may need to reload the page.");
                return;
            }

            if (playground.innerHTML.trim() !== "") {
                playground.innerHTML = "";
            }
            mountFunction(playground);
        }


        document.getElementById('codeInput').addEventListener('keydown', function(event) {
            if (event.metaKey && event.key === 'Enter') {
                event.preventDefault();
                evaluateCode();
            }
        });

        window.onload = loadCodeFromHash;
    </script>


</body></html> && pattern.length === 1) return text.length === 0;

    // If next char is '*', match zero or more occurrences of prev char
    if (pattern[1] === '*') return matchStar(pattern[0], pattern.slice(2), text);

    // Match . or literal character
    if (text.length !== 0 && (pattern[0] === '.' || pattern[0] === text[0])) {
        return matchHere(pattern.slice(1), text.slice(1));
    }

    return false;
};

const matchStar = (prevChar, pattern, text) => {
    // Try matching zero occurrences first
    if (matchHere(pattern, text)) return true;

    // Then, match one or more occurrences of prevChar
    while (text.length > 0 && (text[0] === prevChar || prevChar === '.')) {
        text = text.slice(1);
        if (matchHere(pattern, text)) return true;
    }

    return false;
};

const match = (pattern, text) => {
    // Handle ^ anchor at the beginning
    if (pattern[0] === '^') {
        return matchHere(pattern.slice(1), text);
    }

    // Otherwise, check the entire string
    for (let i = 0; i <= text.length; i++) {
        if (matchHere(pattern, text.slice(i))) return true;
    }

    return false;
};

const assertEqual = (actual, expected, testName) => 
    console.log(actual === expected ? `Passed: ${testName}` : `Failed: ${testName}. Expected ${expected} but got ${actual}`);

assertEqual(match("ab*c", "ac"), true, "Star match 'ab*c' vs 'ac' (zero occurrences)");
assertEqual(match("ab*c", "abbbc"), true, "Star match 'ab*c' vs 'abbbc' (multiple occurrences)");
assertEqual(match("^ab.*c$", "abc"), true, "Complex match '^ab.*c
    <div id="console"></div>

    <script>
        function evaluateCode() {
            const code = document.getElementById('codeInput').value;
            const consoleDiv = document.getElementById('console');
            consoleDiv.innerHTML = '';

            // Custom console.log function to output to the console div
            const originalConsoleLog = console.log;
            console.log = function(...args) {
                args.forEach(arg => {
                    const output = document.createElement('div');
                    output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
                    consoleDiv.appendChild(output);
                });
                originalConsoleLog.apply(console, args);
            };

            try {
                eval(code);
            } catch (error) {
                const errorOutput = document.createElement('div');
                errorOutput.textContent = error;
                errorOutput.style.color = 'red';
                consoleDiv.appendChild(errorOutput);
            }

            // Restore browser's console.log
            console.log = originalConsoleLog;
        }

        function downloadCodeAndEditor() {
            const codeInput = document.getElementById('codeInput').value;
            const htmlContent = document.documentElement.outerHTML.replace(
                /<textarea id="codeInput"[^>]*>.*<\/textarea>/,
                `<textarea id="codeInput">${codeInput}</textarea>`
            );

            const blob = new Blob([htmlContent], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'code_editor.html';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }

        function shareCode() {
            const code = document.getElementById('codeInput').value;
            const encodedCode = btoa(encodeURIComponent(code));
            window.location.hash = encodedCode;
            window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
        }

        function clearEverything() {
            if (!confirm('Are you sure you want to reset the playground?')) {
                return;
            } else {               
                window.location.hash = '';
                window.location.reload();
            }
        }

        function loadCodeFromHash() {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    const decodedCode = decodeURIComponent(atob(hash));
                    document.getElementById('codeInput').value = decodedCode;
                } catch (error) {
                    console.error('Failed to decode the URL hash:', error);
                }
            }
        }

        function help() {
            const helpText = `
            Welcome to the JavaScript Playground! Here are some tips to get you started:

            1. Enter your JavaScript code in the textarea.
            2. Click the "Run Code" button to execute your code.
            3. The console output will be displayed below the textarea.
            4. Click the "Clear" button to reset the playground.
            5. Click the "Download" button to save your code and editor as an HTML file.
            6. Click the "Share" button to generate a URL to share your code with others.
            7. You can also press "Cmd + Enter" to run your code.
            8. There's an empty div above the buttons with the id "playground"
            9. You can mount stuff to it using the "mount" function, for more info run "mountHelp()"
            10. You can use the "clear()" function to clear the content's of the console.

            Go nuts! Share scrappy fiddles!
            `;
            console.log(helpText);
        }

        function clear() {
            document.getElementById('console').innerHTML = '';
        }

        function mountHelp() {
            console.log(`
            The mount function is used to mount stuff to the playground div.
            It takes a function as an argument, which in turn receives the playground div as an argument.
            Before mounting, it clears the playground div.
            Here's an example of how to use the mount function:

            mount(playground => {
                const h1 = document.createElement('h1');
                h1.textContent = 'Hell is empty and all the devils are here.';
                playground.appendChild(h1);
            });

            This will add an h1 element to the playground div.
            `);
        }

        function mount(mountFunction) {
            const playground = document.getElementById('playground');
            if (!playground) {
                console.error("Couldn't find a div with the id 'playground'! You may need to reload the page.");
                return;
            }

            if (playground.innerHTML.trim() !== "") {
                playground.innerHTML = "";
            }
            mountFunction(playground);
        }


        document.getElementById('codeInput').addEventListener('keydown', function(event) {
            if (event.metaKey && event.key === 'Enter') {
                event.preventDefault();
                evaluateCode();
            }
        });

        window.onload = loadCodeFromHash;
    </script>


</body></html> vs 'abc'");
assertEqual(match("^ab.*c$", "abcd"), false, "Complex mismatch '^ab.*c
    <div id="console"></div>

    <script>
        function evaluateCode() {
            const code = document.getElementById('codeInput').value;
            const consoleDiv = document.getElementById('console');
            consoleDiv.innerHTML = '';

            // Custom console.log function to output to the console div
            const originalConsoleLog = console.log;
            console.log = function(...args) {
                args.forEach(arg => {
                    const output = document.createElement('div');
                    output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
                    consoleDiv.appendChild(output);
                });
                originalConsoleLog.apply(console, args);
            };

            try {
                eval(code);
            } catch (error) {
                const errorOutput = document.createElement('div');
                errorOutput.textContent = error;
                errorOutput.style.color = 'red';
                consoleDiv.appendChild(errorOutput);
            }

            // Restore browser's console.log
            console.log = originalConsoleLog;
        }

        function downloadCodeAndEditor() {
            const codeInput = document.getElementById('codeInput').value;
            const htmlContent = document.documentElement.outerHTML.replace(
                /<textarea id="codeInput"[^>]*>.*<\/textarea>/,
                `<textarea id="codeInput">${codeInput}</textarea>`
            );

            const blob = new Blob([htmlContent], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'code_editor.html';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }

        function shareCode() {
            const code = document.getElementById('codeInput').value;
            const encodedCode = btoa(encodeURIComponent(code));
            window.location.hash = encodedCode;
            window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
        }

        function clearEverything() {
            if (!confirm('Are you sure you want to reset the playground?')) {
                return;
            } else {               
                window.location.hash = '';
                window.location.reload();
            }
        }

        function loadCodeFromHash() {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    const decodedCode = decodeURIComponent(atob(hash));
                    document.getElementById('codeInput').value = decodedCode;
                } catch (error) {
                    console.error('Failed to decode the URL hash:', error);
                }
            }
        }

        function help() {
            const helpText = `
            Welcome to the JavaScript Playground! Here are some tips to get you started:

            1. Enter your JavaScript code in the textarea.
            2. Click the "Run Code" button to execute your code.
            3. The console output will be displayed below the textarea.
            4. Click the "Clear" button to reset the playground.
            5. Click the "Download" button to save your code and editor as an HTML file.
            6. Click the "Share" button to generate a URL to share your code with others.
            7. You can also press "Cmd + Enter" to run your code.
            8. There's an empty div above the buttons with the id "playground"
            9. You can mount stuff to it using the "mount" function, for more info run "mountHelp()"
            10. You can use the "clear()" function to clear the content's of the console.

            Go nuts! Share scrappy fiddles!
            `;
            console.log(helpText);
        }

        function clear() {
            document.getElementById('console').innerHTML = '';
        }

        function mountHelp() {
            console.log(`
            The mount function is used to mount stuff to the playground div.
            It takes a function as an argument, which in turn receives the playground div as an argument.
            Before mounting, it clears the playground div.
            Here's an example of how to use the mount function:

            mount(playground => {
                const h1 = document.createElement('h1');
                h1.textContent = 'Hell is empty and all the devils are here.';
                playground.appendChild(h1);
            });

            This will add an h1 element to the playground div.
            `);
        }

        function mount(mountFunction) {
            const playground = document.getElementById('playground');
            if (!playground) {
                console.error("Couldn't find a div with the id 'playground'! You may need to reload the page.");
                return;
            }

            if (playground.innerHTML.trim() !== "") {
                playground.innerHTML = "";
            }
            mountFunction(playground);
        }


        document.getElementById('codeInput').addEventListener('keydown', function(event) {
            if (event.metaKey && event.key === 'Enter') {
                event.preventDefault();
                evaluateCode();
            }
        });

        window.onload = loadCodeFromHash;
    </script>


</body></html> vs 'abcd'");</textarea>
    <div id="console"></div>

    <script>
        function evaluateCode() {
            const code = document.getElementById('codeInput').value;
            const consoleDiv = document.getElementById('console');
            consoleDiv.innerHTML = '';

            // Custom console.log function to output to the console div
            const originalConsoleLog = console.log;
            console.log = function(...args) {
                args.forEach(arg => {
                    const output = document.createElement('div');
                    output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
                    consoleDiv.appendChild(output);
                });
                originalConsoleLog.apply(console, args);
            };

            try {
                eval(code);
            } catch (error) {
                const errorOutput = document.createElement('div');
                errorOutput.textContent = error;
                errorOutput.style.color = 'red';
                consoleDiv.appendChild(errorOutput);
            }

            // Restore browser's console.log
            console.log = originalConsoleLog;
        }

        function downloadCodeAndEditor() {
            const codeInput = document.getElementById('codeInput').value;
            const htmlContent = document.documentElement.outerHTML.replace(
                /<textarea id="codeInput"[^>]*>.*<\/textarea>/,
                `<textarea id="codeInput">${codeInput}</textarea>`
            );

            const blob = new Blob([htmlContent], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'code_editor.html';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }

        function shareCode() {
            const code = document.getElementById('codeInput').value;
            const encodedCode = btoa(encodeURIComponent(code));
            window.location.hash = encodedCode;
            window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
        }

        function clearEverything() {
            if (!confirm('Are you sure you want to reset the playground?')) {
                return;
            } else {               
                window.location.hash = '';
                window.location.reload();
            }
        }

        function loadCodeFromHash() {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    const decodedCode = decodeURIComponent(atob(hash));
                    document.getElementById('codeInput').value = decodedCode;
                } catch (error) {
                    console.error('Failed to decode the URL hash:', error);
                }
            }
        }

        function help() {
            const helpText = `
            Welcome to the JavaScript Playground! Here are some tips to get you started:

            1. Enter your JavaScript code in the textarea.
            2. Click the "Run Code" button to execute your code.
            3. The console output will be displayed below the textarea.
            4. Click the "Clear" button to reset the playground.
            5. Click the "Download" button to save your code and editor as an HTML file.
            6. Click the "Share" button to generate a URL to share your code with others.
            7. You can also press "Cmd + Enter" to run your code.
            8. There's an empty div above the buttons with the id "playground"
            9. You can mount stuff to it using the "mount" function, for more info run "mountHelp()"
            10. You can use the "clear()" function to clear the content's of the console.

            Go nuts! Share scrappy fiddles!
            `;
            console.log(helpText);
        }

        function clear() {
            document.getElementById('console').innerHTML = '';
        }

        function mountHelp() {
            console.log(`
            The mount function is used to mount stuff to the playground div.
            It takes a function as an argument, which in turn receives the playground div as an argument.
            Before mounting, it clears the playground div.
            Here's an example of how to use the mount function:

            mount(playground => {
                const h1 = document.createElement('h1');
                h1.textContent = 'Hell is empty and all the devils are here.';
                playground.appendChild(h1);
            });

            This will add an h1 element to the playground div.
            `);
        }

        function mount(mountFunction) {
            const playground = document.getElementById('playground');
            if (!playground) {
                console.error("Couldn't find a div with the id 'playground'! You may need to reload the page.");
                return;
            }

            if (playground.innerHTML.trim() !== "") {
                playground.innerHTML = "";
            }
            mountFunction(playground);
        }


        document.getElementById('codeInput').addEventListener('keydown', function(event) {
            if (event.metaKey && event.key === 'Enter') {
                event.preventDefault();
                evaluateCode();
            }
        });

        window.onload = loadCodeFromHash;
    </script>


</body></html>