Skip to content

Commit 0ea9434

Browse files
committed
[javascript] terminal emulator: max width 60em
1 parent 33275a9 commit 0ea9434

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

00_Common/javascript/WebTerminal/HtmlTerminal.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:root {
2-
--terminal-font: 1em "Lucida Console", "Courier New", monospace;
2+
--terminal-font: 1rem "Lucida Console", "Courier New", monospace;
33
--background-color: transparent;
44
--text-color: var(--text);
55
--prompt-char: '$ ';
@@ -10,12 +10,15 @@
1010
* If you wan t to overwrite them use custom properties (variables).
1111
*/
1212
.terminal {
13+
display: block;
1314
font: var(--terminal-font);
1415
background-color: var(--background-color);
1516
color: var(--text-color);
1617

1718
overflow-y: scroll;
18-
width: max-content;
19+
width: 100%;
20+
max-width: 60rem;
21+
margin: 0 auto;
1922
}
2023

2124
/* The terminal consits of multiple "line" elements

00_Common/javascript/WebTerminal/terminal.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<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">
55
<link
66
rel="stylesheet"
77
href="../../../00_Utilities/javascript/style_terminal.css"
@@ -37,7 +37,8 @@ <h1><a href="../../../../">BASIC Computer Games</a></h1>
3737
<main id="output"></main>
3838
<script src="HtmlTerminal.js" type="text/javascript"></script>
3939
<script>
40-
const term = new HtmlTerminal(document.getElementById("output"));
40+
const $output = document.getElementById("output");
41+
const term = new HtmlTerminal($output);
4142

4243
function getGameScriptFromHash() {
4344
const hash = window.location.hash;
@@ -93,13 +94,25 @@ <h1><a href="../../../../">BASIC Computer Games</a></h1>
9394
document.body.append($scriptTag);
9495
}
9596

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+
96108
/* Redirect stdin/stdout to the HtmlTerminal.
97109
* This is VERY hacky and should never be done in a serious project!
98110
* We can use this here because we know what we are doing and...
99111
* ...it's just simple games ;-) */
100112
window.process = {
101113
stdout: {
102114
write: (t) => term.write(t),
115+
columns: getOutputColumns($output)
103116
},
104117
stdin: {
105118
on: (event, callback) => term.input(callback),

0 commit comments

Comments
 (0)