Skip to content

Commit 8a564b2

Browse files
Refactor terminal initialization and usage
Updated terminal variable initialization to use $state in Svelte components and replaced direct method calls with optional chaining for safer access. Changed options declarations to use const and improved example code in README. Removed outdated FAQ section and updated real-world usage references.
1 parent c797f47 commit 8a564b2

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ Here's a basic example of how to use xterm-svelte in your SvelteKit application:
5757
Terminal
5858
} from '@battlefieldduck/xterm-svelte';
5959
60-
let terminal: Terminal;
60+
let terminal = $state<Terminal>();
6161
62-
let options: ITerminalOptions & ITerminalInitOnlyOptions = {
62+
const options: ITerminalOptions & ITerminalInitOnlyOptions = {
6363
fontFamily: 'Consolas'
6464
};
6565
@@ -68,10 +68,10 @@ Here's a basic example of how to use xterm-svelte in your SvelteKit application:
6868
6969
// FitAddon Usage
7070
const fitAddon = new (await XtermAddon.FitAddon()).FitAddon();
71-
terminal.loadAddon(fitAddon);
71+
terminal?.loadAddon(fitAddon);
7272
fitAddon.fit();
7373
74-
terminal.write('Hello World');
74+
terminal?.write('Hello World');
7575
}
7676
7777
function onData(data: string) {
@@ -92,14 +92,10 @@ Here's a basic example of how to use xterm-svelte in your SvelteKit application:
9292

9393
The `onLoad()` function fires when the xterm terminal is first initialized. You can use this function to perform actions such as initializing xterm addons.
9494

95-
### Why is the `terminal` undefined?
96-
97-
One possible cause is that you called the `terminal` function before it was initialized. For example, if you run the `terminal` function in the `onMount` function without wrapping it with `if (terminal !== undefined)`, it can lead to this issue.
98-
9995
## Real-world uses
100-
101-
- [Thymis](https://thymis.io): Thymis is an open-source project that aims to provide a seamless and secure IoT device management solution.
102-
With Thymis, users can easily configure and manage their devices running on the NixOS operating system.
96+
- [Bitor](https://github.com/bitorscanner/Bitor): Bitor Scanning Software
97+
- [Asm editor](https://github.com/Specy/asm-editor): A modern webapp to write, run and learn M68K, MIPS, RISC-V, X86 assembly
98+
- [Sylve](https://github.com/AlchemillaHQ/Sylve): Lightweight GUI for managing Bhyve, Jails, ZFS, networking, and more on FreeBSD
10399
- [And much more...](https://github.com/BattlefieldDuck/xterm-svelte/network/dependents)
104100

105101
## Contributing

src/lib/Xterm.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let parent = $state<HTMLElement>();
77
88
let {
9-
terminal = $bindable<Terminal>(),
9+
terminal = $bindable<Terminal | undefined>(),
1010
options,
1111
onBell,
1212
onBinary,

src/routes/Code.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
Terminal
77
} from '@battlefieldduck/xterm-svelte';
88
9-
let terminal: Terminal;
9+
let terminal = $state<Terminal>();
1010
11-
let options: ITerminalOptions & ITerminalInitOnlyOptions = {
11+
const options: ITerminalOptions & ITerminalInitOnlyOptions = {
1212
fontFamily: 'Consolas'
1313
};
1414
@@ -17,10 +17,10 @@
1717
1818
// FitAddon Usage
1919
const fitAddon = new (await XtermAddon.FitAddon()).FitAddon();
20-
terminal.loadAddon(fitAddon);
20+
terminal?.loadAddon(fitAddon);
2121
fitAddon.fit();
2222
23-
terminal.write('Hello World');
23+
terminal?.write('Hello World');
2424
}
2525
2626
function onData(data: string) {

src/routes/Terminal.svelte

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22
import { Xterm, XtermAddon } from '$lib/index.js';
33
import type { ITerminalOptions, ITerminalInitOnlyOptions, Terminal } from '$lib/index.js';
44
5-
let terminal: Terminal;
6-
let handleResize: () => void;
7-
let isCommandRunning = false;
5+
let terminal = $state<Terminal>();
6+
let handleResize = $state<() => void>();
7+
let isCommandRunning = $state(false);
88
99
async function runCommand() {
1010
isCommandRunning = true;
11-
terminal.writeln('\r\n\r\nPinging 1.1.1.1 with 32 bytes of data:');
12-
terminal.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
11+
terminal?.writeln('\r\n\r\nPinging 1.1.1.1 with 32 bytes of data:');
12+
terminal?.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
1313
await new Promise((resolve) => setTimeout(resolve, 1000));
14-
terminal.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
14+
terminal?.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
1515
await new Promise((resolve) => setTimeout(resolve, 1000));
16-
terminal.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
16+
terminal?.writeln('Reply from 1.1.1.1: bytes=32 time=4ms TTL=63');
1717
await new Promise((resolve) => setTimeout(resolve, 1000));
18-
terminal.writeln('Reply from 1.1.1.1: bytes=32 time=3ms TTL=63');
19-
terminal.writeln('\nPing statistics for 1.1.1.1:');
20-
terminal.writeln(' Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),');
21-
terminal.writeln('Approximate round trip times in milli-seconds:');
22-
terminal.writeln(' Minimum = 3ms, Maximum = 4ms, Average = 3ms\r\n');
23-
terminal.write('C:\\Users\\Administrator>ping 1.1.1.1');
18+
terminal?.writeln('Reply from 1.1.1.1: bytes=32 time=3ms TTL=63');
19+
terminal?.writeln('\nPing statistics for 1.1.1.1:');
20+
terminal?.writeln(' Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),');
21+
terminal?.writeln('Approximate round trip times in milli-seconds:');
22+
terminal?.writeln(' Minimum = 3ms, Maximum = 4ms, Average = 3ms\r\n');
23+
terminal?.write('C:\\Users\\Administrator>ping 1.1.1.1');
2424
isCommandRunning = false;
2525
}
2626
2727
async function onLoad() {
28-
terminal.write('C:\\Users\\Administrator>ping 1.1.1.1');
28+
terminal?.write('C:\\Users\\Administrator>ping 1.1.1.1');
2929
3030
// FitAddon Usage
3131
const fitAddon = new (await XtermAddon.FitAddon()).FitAddon();
32-
terminal.loadAddon(fitAddon);
32+
terminal?.loadAddon(fitAddon);
3333
fitAddon.fit();
3434
handleResize = () => fitAddon.fit();
3535
}
@@ -40,7 +40,7 @@
4040
}
4141
}
4242
43-
let options: ITerminalOptions & ITerminalInitOnlyOptions = {
43+
const options: ITerminalOptions & ITerminalInitOnlyOptions = {
4444
theme: {
4545
background: '#000000'
4646
},

0 commit comments

Comments
 (0)