Add REPL dark and light color themes (#383)

- detect terminal background from COLORFGBG environment variable
- add `.dark` and `.light` meta commands
- catch `loadScript` exceptions
This commit is contained in:
Charlie Gordon 2024-04-16 14:18:37 +02:00 committed by GitHub
parent 29b45337f0
commit 70a60f0aa1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8571 additions and 8262 deletions

16773
gen/repl.c

File diff suppressed because it is too large Load diff

60
repl.js
View file

@ -61,20 +61,37 @@ import * as os from "os";
bright_white: "\x1b[37;1m", bright_white: "\x1b[37;1m",
}; };
var styles = { var themes = {
'default': 'bright_green', dark: {
'comment': 'white', 'default': 'bright_green',
'string': 'bright_cyan', 'comment': 'white',
'regex': 'cyan', 'string': 'bright_cyan',
'number': 'green', 'regex': 'cyan',
'keyword': 'bright_white', 'number': 'green',
'function': 'bright_yellow', 'keyword': 'bright_white',
'type': 'bright_magenta', 'function': 'bright_yellow',
'identifier': 'bright_green', 'type': 'bright_magenta',
'error': 'red', 'identifier': 'bright_green',
'result': 'bright_white', 'error': 'red',
'error_msg': 'bright_red', 'result': 'bright_white',
'error_msg': 'bright_red',
},
light: {
'default': 'bright_green',
'comment': 'grey',
'string': 'bright_cyan',
'regex': 'cyan',
'number': 'green',
'keyword': 'bright_magenta',
'function': 'bright_yellow',
'type': 'bright_magenta',
'identifier': 'bright_green',
'error': 'red',
'result': 'grey',
'error_msg': 'bright_red',
},
}; };
var styles = themes.dark;
var history = []; var history = [];
var clip_board = ""; var clip_board = "";
@ -1044,6 +1061,8 @@ import * as os from "os";
".dec " + sel(!hex_mode) + "decimal number display\n" + ".dec " + sel(!hex_mode) + "decimal number display\n" +
".time " + sel(show_time) + "toggle timing display\n" + ".time " + sel(show_time) + "toggle timing display\n" +
".color " + sel(show_colors) + "toggle colored output\n" + ".color " + sel(show_colors) + "toggle colored output\n" +
".dark " + sel(styles == themes.dark) + "select dark color theme\n" +
".light " + sel(styles == themes.light) + "select light color theme\n" +
".clear clear the terminal\n" + ".clear clear the terminal\n" +
".load load source code from a file\n" + ".load load source code from a file\n" +
".quit exit\n"); ".quit exit\n");
@ -1052,7 +1071,11 @@ import * as os from "os";
function load(s) { function load(s) {
if (s.lastIndexOf(".") <= s.lastIndexOf("/")) if (s.lastIndexOf(".") <= s.lastIndexOf("/"))
s += ".js"; s += ".js";
std.loadScript(s); try {
std.loadScript(s);
} catch (e) {
std.puts(`${e}\n`);
}
} }
function to_bool(s, def) { function to_bool(s, def) {
@ -1066,6 +1089,8 @@ import * as os from "os";
"dec": (s) => { hex_mode = !to_bool(s, true); }, "dec": (s) => { hex_mode = !to_bool(s, true); },
"time": (s) => { show_time = to_bool(s, !show_time); }, "time": (s) => { show_time = to_bool(s, !show_time); },
"color": (s) => { show_colors = to_bool(s, !show_colors); }, "color": (s) => { show_colors = to_bool(s, !show_colors); },
"dark": () => { styles = themes.dark; },
"light": () => { styles = themes.light; },
"clear": () => { std.puts("\x1b[H\x1b[J") }, "clear": () => { std.puts("\x1b[H\x1b[J") },
"quit": () => { std.exit(0); }, "quit": () => { std.exit(0); },
}, null); }, null);
@ -1371,6 +1396,13 @@ import * as os from "os";
return [ state, level, r ]; return [ state, level, r ];
} }
var m, s = std.getenv("COLORFGBG");
if (s && (m = s.match(/(\d+);(\d+)/))) {
if (+m[2] !== 0) { // light background
styles = themes.light;
}
}
termInit(); termInit();
cmd_start(); cmd_start();