|
1 | 1 | <html>
|
2 | 2 | <head>
|
3 | 3 | <title>Minimal node.js terminal</title>
|
4 |
| - <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 4 | + <meta name="viewport" content="width=device-width, initial-scale=.75"> |
5 | 5 | <link
|
6 | 6 | rel="stylesheet"
|
7 | 7 | href="../../../00_Utilities/javascript/style_terminal.css"
|
@@ -37,7 +37,8 @@ <h1><a href="../../../../">BASIC Computer Games</a></h1>
|
37 | 37 | <main id="output"></main>
|
38 | 38 | <script src="HtmlTerminal.js" type="text/javascript"></script>
|
39 | 39 | <script>
|
40 |
| - const term = new HtmlTerminal(document.getElementById("output")); |
| 40 | + const $output = document.getElementById("output"); |
| 41 | + const term = new HtmlTerminal($output); |
41 | 42 |
|
42 | 43 | function getGameScriptFromHash() {
|
43 | 44 | const hash = window.location.hash;
|
@@ -93,13 +94,25 @@ <h1><a href="../../../../">BASIC Computer Games</a></h1>
|
93 | 94 | document.body.append($scriptTag);
|
94 | 95 | }
|
95 | 96 |
|
| 97 | + /** |
| 98 | + * Determine how much chars will fit in each terminal line. |
| 99 | + */ |
| 100 | + function getOutputColumns($element) { |
| 101 | + |
| 102 | + const fontWidth = 10; //TODO: this width could be measured but it may be complicated! |
| 103 | + const columnWidth = Math.trunc($element.clientWidth / fontWidth); |
| 104 | + console.warn(`[terminal] document.body.clientWidth:${$element.clientWidth} fontsize:${fontWidth} columnWidth:${columnWidth}`); |
| 105 | + return columnWidth; |
| 106 | + } |
| 107 | + |
96 | 108 | /* Redirect stdin/stdout to the HtmlTerminal.
|
97 | 109 | * This is VERY hacky and should never be done in a serious project!
|
98 | 110 | * We can use this here because we know what we are doing and...
|
99 | 111 | * ...it's just simple games ;-) */
|
100 | 112 | window.process = {
|
101 | 113 | stdout: {
|
102 | 114 | write: (t) => term.write(t),
|
| 115 | + columns: getOutputColumns($output) |
103 | 116 | },
|
104 | 117 | stdin: {
|
105 | 118 | on: (event, callback) => term.input(callback),
|
|
0 commit comments