Skip to content

Commit e84647e

Browse files
smarter version info handling
1 parent a746e8d commit e84647e

5 files changed

Lines changed: 22 additions & 26 deletions

File tree

src/abstract_shell.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,3 @@ export abstract class AbstractShell {
3232
// edit_doc_title should default to true in implementations
3333
abstract execute(line: string, edit_doc_title?: boolean, program_final_completion_callback?: (exit_code?: number) => void): Promise<boolean>;
3434
}
35-
36-
let version = "unknown";
37-
let env = "unknown";
38-
39-
export const set_special_vars = (new_version: string, new_env: string) => {
40-
version = new_version;
41-
env = new_env;
42-
};
43-
44-
export const apply_special_vars = (shell: AbstractShell) => {
45-
shell.memory.set_variable("VERSION", version);
46-
shell.memory.set_variable("ENV", env);
47-
}
48-
49-
// bit of a hack to get version and env into shells without assumptions (about shell choice or about if its running in the browser), so long as the shell calls it at startup
50-
// it works at least

src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { WebLinksAddon } from "@xterm/addon-web-links";
66
import { ImageAddon } from "@xterm/addon-image";
77

88
import "./load_global_externals";
9-
import {ProgramRegistry, recurse_mount_and_register_with_output} from "./prog_registry";
9+
import {ProgramRegistry} from "./prog_registry";
1010
import * as programs from "./programs/@ALL";
1111

1212
import { SoundRegistry } from "./sfx_registry";
@@ -16,10 +16,8 @@ import { LocalStorageFS } from "./fs_impl/localstorage";
1616
import { OPFSFileSystem } from "./fs_impl/opfs";
1717
import { initial_fs_setup } from "./initial_fs_setup";
1818

19-
import Swal from "sweetalert2";
2019
import {DOMWindowManager} from "./window_impl/dom";
2120
import {Kernel} from "./kernel";
22-
import {set_special_vars} from "./abstract_shell";
2321

2422

2523
const boot_screen = document.getElementById("boot_screen");
@@ -216,9 +214,7 @@ async function main() {
216214

217215
// create the kernel
218216
const kernel = new Kernel(term, fs, prog_reg, sfx_reg, wm);
219-
220-
// set version and env info in shell special vars
221-
set_special_vars(document.body.dataset.version, "web");
217+
kernel.set_env_info(document.body.dataset.version, "web");
222218

223219
loaded(term);
224220

src/kernel.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export class Kernel {
2727

2828
_panicked = false;
2929

30+
_env_info = {
31+
version: "unknown",
32+
env: "unknown"
33+
}
34+
3035
get panicked(): boolean {
3136
return this._panicked;
3237
}
@@ -59,6 +64,15 @@ export class Kernel {
5964
return this._process_manager.ipc_manager;
6065
}
6166

67+
get_env_info(): {version: string, env: string} {
68+
return this._env_info;
69+
}
70+
71+
set_env_info(version: string, env: string) {
72+
this._env_info.version = version;
73+
this._env_info.env = env;
74+
}
75+
6276
spawn = (command: string, args: string[] = [], shell?: AbstractShell, original_line_parse?: LineParseResultCommand): SpawnResult => {
6377
// search for the command in the registry
6478
const program = this._prog_registry.getProgram(command);

src/programs/core/ash/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {Program} from "../../../types";
22

33
import {AshShell} from "./core";
44
import {make_read_line_key_handlers, make_read_line_printable_handler} from "./key_handlers";
5-
import {apply_special_vars} from "../../../abstract_shell";
65

76
export default {
87
name: "ash",
@@ -17,7 +16,10 @@ export default {
1716
const {kernel, term, process, args} = data;
1817

1918
const shell = new AshShell(term, kernel);
20-
apply_special_vars(shell);
19+
20+
const env_info = kernel.get_env_info();
21+
shell.memory.set_variable("VERSION", env_info.version);
22+
shell.memory.set_variable("ENV", env_info.env);
2123

2224
const fs = kernel.get_fs();
2325

src/programs/mefetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ export default {
110110
completion: async () => [],
111111
main: async (data) => {
112112
// extract from data to make code less verbose
113-
const { kernel, term, shell, args } = data;
113+
const { kernel, term, args } = data;
114114

115115
// extract from ANSI to make code less verbose
116116
const { STYLE, FG } = ANSI;
117117

118118
// get version string
119-
const version_str = shell ? shell.memory.get_variable("VERSION") || "unknown" : "unknown";
119+
const version_str = kernel.get_env_info().version;
120120

121121
// restrict to first 3 quarters of screen
122122
const max_columns = Math.floor(term.cols * 0.75);

0 commit comments

Comments
 (0)