From 0ad56a0680d14fb419af44a68cca33cd8bdb55aa Mon Sep 17 00:00:00 2001 From: Christian7573 Date: Thu, 3 Jun 2021 22:04:16 -0500 Subject: [PATCH] Added automatic saving. Bug wackamole. termResize being called with NaN RenderService values --- src/assets/xterm_config/functionality.js | 22 ++-- src/assets/xterm_config/index.html | 3 - .../xterm_config/xterm_advanced_options.js | 42 +++--- src/assets/xterm_config/xterm_color_theme.js | 40 +++--- src/assets/xterm_config/xterm_defaults.js | 120 +++++++++--------- .../xterm_config/xterm_general_options.js | 30 ++--- src/client/wetty/term/confiruragtion.ts | 11 +- .../wetty/term/confiruragtion/editor.ts | 18 ++- src/client/wetty/term/confiruragtion/load.ts | 4 +- 9 files changed, 149 insertions(+), 141 deletions(-) diff --git a/src/assets/xterm_config/functionality.js b/src/assets/xterm_config/functionality.js index e85f005..8a0f2ad 100644 --- a/src/assets/xterm_config/functionality.js +++ b/src/assets/xterm_config/functionality.js @@ -2,6 +2,8 @@ function optionGenericGet() { return this.el.querySelector("input").value; } function optionGenericSet(value) { this.el.querySelector("input").value = value; } function optionEnumGet() { return this.el.querySelector("select").value; } function optionEnumSet(value) { this.el.querySelector("select").value = value; } +function optionBoolGet() { return this.el.querySelector("input").value === "true"; } +function optionBoolSet(value) { this.el.querySelector("input").value = value ? "true" : "false"; } const allOptions = []; function inflateOptions(optionsSchema) { @@ -23,6 +25,8 @@ function inflateOptions(optionsSchema) { switch (option.type) { case "boolean": el = booleanOption.cloneNode(true); + option.get = optionBoolGet.bind(option); + option.set = optionBoolSet.bind(option); break; case "enum": @@ -90,19 +94,21 @@ window.loadOptions = function(config) { if (window.top === window) alert("Error: Page is top level. This page is supposed to be accessed from inside Wetty."); -document.querySelector("#save_button").addEventListener("click", () => { +function saveConfig() { const newConfig = {}; allOptions.forEach(option => { let newValue = option.get(); - if (option.nullable === true && option.type === "text" && newValue === "") newValue = null; - else if (option.nullable === true && option.type === "number" && newValue < 0) newValue = null; + if (option.nullable === true && option.type === "text" && newValue === "") newValue = undefined; + else if (option.nullable === true && option.type === "number" && newValue < 0) newValue = undefined; if (option.json === true && option.type === "text") newValue = JSON.parse(newValue); setItem(newConfig, option.path, newValue); }); - console.log(newConfig); window.wetty_save_config(newConfig); - window.wetty_close_config(); -}); -document.querySelector("#cancel_button").addEventListener("click", () => { - window.wetty_close_config(); +}; + +window.addEventListener("load", () => { + const els = document.querySelectorAll("input, select"); + for (let i = 0; i < els.length; i++) { + els[i].addEventListener("input", saveConfig); + } }); diff --git a/src/assets/xterm_config/index.html b/src/assets/xterm_config/index.html index a742e9e..1a5953e 100644 --- a/src/assets/xterm_config/index.html +++ b/src/assets/xterm_config/index.html @@ -7,9 +7,6 @@

Configure

-
- -
diff --git a/src/assets/xterm_config/xterm_advanced_options.js b/src/assets/xterm_config/xterm_advanced_options.js index b1b745b..147a826 100644 --- a/src/assets/xterm_config/xterm_advanced_options.js +++ b/src/assets/xterm_config/xterm_advanced_options.js @@ -3,119 +3,113 @@ window.inflateOptions([ type: "boolean", name: "Allow Proposed XTerm APIs", description: "When set to false, any experimental/proposed APIs will throw errors.", - path: ["allowProposedApi"], + path: ["xterm", "allowProposedApi"], }, { type: "boolean", name: "Allow Transparent Background", description: "Whether the background is allowed to be a non-opaque color.", - path: ["allowTransparentBackground"], - }, - { - type: "boolean", - name: "Alt-Click To Move Cursor", - description: "If enabled, alt + click will move the prompt cursor to the position underneath the mouse.", - path: ["altClickMovesCursor"], + path: ["xterm", "allowTransparency"], }, { type: "text", name: "Bell Sound URI", description: "URI for a custom bell character sound.", - path: ["bellSound"], + path: ["xterm", "bellSound"], nullable: true, }, { type: "enum", name: "Bell Style", description: "How the terminal will react to the bell character", - path: ["bellStyle"], + path: ["xterm", "bellStyle"], enum: ["none", "sound"], }, { type: "boolean", name: "Force End-Of-Line", description: "When enabled, any new-line characters (\\n) will be interpreted as carriage-return new-line. (\\r\\n) Typically this is done by the shell program.", - path: ["convertEol"], + path: ["xterm", "convertEol"], }, { type: "boolean", name: "Disable Stdin", description: "Whether input should be disabled", - path: ["disableStdin"], + path: ["xterm", "disableStdin"], }, { type: "number", name: "Letter Spacing", description: "The spacing in whole pixels between characters.", - path: ["letterSpacing"], + path: ["xterm", "letterSpacing"], }, { type: "number", name: "Line Height", description: "Line height, multiplied by the font size to get the height of terminal rows.", - path: ["lineHeight"], + path: ["xterm", "lineHeight"], }, { type: "enum", name: "XTerm Log Level", description: "Log level for the XTerm library.", - path: ["logLevel"], + path: ["xterm", "logLevel"], enum: ["debug", "info", "warn", "error", "off"], }, { type: "boolean", name: "Macintosh Option Key as Meta Key", description: "When enabled, the Option key on Macs will be interpreted as the Meta key.", - path: ["macOptionIsMeta"], + path: ["xterm", "macOptionIsMeta"], }, { type: "boolean", name: "Macintosh Option Click Forces Selection", description: "Whether holding a modifier key will force normal selection behavior, regardless of whether the terminal is in mouse events mode. This will also prevent mouse events from being emitted by the terminal. For example, this allows you to use xterm.js' regular selection inside tmux with mouse mode enabled.", - path: ["macOptionClickForcesSelection"], + path: ["xterm", "macOptionClickForcesSelection"], }, { type: "number", name: "Forced Contrast Ratio", description: "Miminum contrast ratio for terminal text. This will alter the foreground color dynamically to ensure the ratio is met. Goes from 1 (do nothing) to 21 (strict black and white).", - path: ["minimumContrastRatio"], + path: ["xterm", "minimumContrastRatio"], }, { type: "enum", name: "Renderer Type", description: "The terminal renderer to use. Canvas is preferred, but a DOM renderer is also available. Note: Letter spacing and cursor blink do not work in the DOM renderer.", - path: ["rendererType"], + path: ["xterm", "rendererType"], enum: ["canvas", "dom"], }, { type: "boolean", name: "Right Click Selects Words", description: "Whether to select the word under the cursor on right click.", - path: ["rightClickSelectsWord"], + path: ["xterm", "rightClickSelectsWord"], }, { type: "boolean", name: "Screen Reader Support", description: "Whether screen reader support is enabled. When on this will expose supporting elements in the DOM to support NVDA on Windows and VoiceOver on macOS.", - path: ["screenReaderMode"], + path: ["xterm", "screenReaderMode"], }, { type: "number", name: "Tab Stop Width", description: "The size of tab stops in the terminal.", - path: ["tabStopWidth"], + path: ["xterm", "tabStopWidth"], }, { type: "boolean", name: "Windows Mode", description: "\"Whether 'Windows mode' is enabled. Because Windows backends winpty and conpty operate by doing line wrapping on their side, xterm.js does not have access to wrapped lines. When Windows mode is enabled the following changes will be in effect:\n- Reflow is disabled.\n- Lines are assumed to be wrapped if the last character of the line is not whitespace.", - path: ["windowsMode"], + path: ["xterm", "windowsMode"], }, { type: "text", name: "Word Separator", description: "All characters considered word separators. Used for double-click to select word logic. Encoded as JSON in this editor for editing convienience.", - path: ["wordSeparator"], + path: ["xterm", "wordSeparator"], json: true, } ]); diff --git a/src/assets/xterm_config/xterm_color_theme.js b/src/assets/xterm_config/xterm_color_theme.js index 7ca7fe1..2017b7c 100644 --- a/src/assets/xterm_config/xterm_color_theme.js +++ b/src/assets/xterm_config/xterm_color_theme.js @@ -2,7 +2,7 @@ const selectionColorOption = { type: "color", name: "Selection Color", description: "Background color for selected text. Can be transparent.", - path: ["theme", "selection"], + path: ["xterm", "theme", "selection"], }; const selectionColorOpacityOption = { type: "number", @@ -16,25 +16,25 @@ window.inflateOptions([ type: "color", name: "Foreground Color", description: "The default foreground (text) color.", - path: ["theme", "foreground"], + path: ["xterm", "theme", "foreground"], }, { type: "color", name: "Background Color", description: "The default background color.", - path: ["theme", "background"], + path: ["xterm", "theme", "background"], }, { type: "color", name: "Cursor Color", description: "Color of the cursor.", - path: ["theme", "cursor"], + path: ["xterm", "theme", "cursor"], }, { type: "color", name: "Block Cursor Accent Color", description: "The accent color of the cursor, used as the foreground color for block cursors.", - path: ["theme", "cursorAccent"], + path: ["xterm", "theme", "cursorAccent"], }, selectionColorOption, selectionColorOpacityOption, @@ -42,91 +42,91 @@ window.inflateOptions([ type: "color", name: "Black", description: "Color for ANSI Black text.", - path: ["theme", "black"], + path: ["xterm", "theme", "black"], }, { type: "color", name: "Red", description: "Color for ANSI Red text.", - path: ["theme", "red"], + path: ["xterm", "theme", "red"], }, { type: "color", name: "Green", description: "Color for ANSI Green text.", - path: ["theme", "green"], + path: ["xterm", "theme", "green"], }, { type: "color", name: "Yellow", description: "Color for ANSI Yellow text.", - path: ["theme", "yellow"], + path: ["xterm", "theme", "yellow"], }, { type: "color", name: "Blue", description: "Color for ANSI Blue text.", - path: ["theme", "blue"], + path: ["xterm", "theme", "blue"], }, { type: "color", name: "Magenta", description: "Color for ANSI Magenta text.", - path: ["theme", "magenta"], + path: ["xterm", "theme", "magenta"], }, { type: "color", name: "Cyan", description: "Color for ANSI Cyan text.", - path: ["theme", "cyan"], + path: ["xterm", "theme", "cyan"], }, { type: "color", name: "White", description: "Color for ANSI White text.", - path: ["theme", "white"], + path: ["xterm", "theme", "white"], }, { type: "color", name: "Bright Black", description: "Color for ANSI Bright Black text.", - path: ["theme", "brightBlack"], + path: ["xterm", "theme", "brightBlack"], }, { type: "color", name: "Bright Red", description: "Color for ANSI Bright Red text.", - path: ["theme", "brightRed"], + path: ["xterm", "theme", "brightRed"], }, { type: "color", name: "Bright Green", description: "Color for ANSI Bright Green text.", - path: ["theme", "brightGreen"], + path: ["xterm", "theme", "brightGreen"], }, { type: "color", name: "Bright Yellow", description: "Color for ANSI Bright Yellow text.", - path: ["theme", "brightYellow"], + path: ["xterm", "theme", "brightYellow"], }, { type: "color", name: "Bright Blue", description: "Color for ANSI Bright Blue text.", - path: ["theme", "brightBlue"], + path: ["xterm", "theme", "brightBlue"], }, { type: "color", name: "Bright Magenta", description: "Color for ANSI Bright Magenta text.", - path: ["theme", "brightMagenta"], + path: ["xterm", "theme", "brightMagenta"], }, { type: "color", name: "Bright White", description: "Color for ANSI Bright White text.", - path: ["theme", "brightWhite"], + path: ["xterm", "theme", "brightWhite"], } ]); diff --git a/src/assets/xterm_config/xterm_defaults.js b/src/assets/xterm_config/xterm_defaults.js index d891d04..33e10aa 100644 --- a/src/assets/xterm_config/xterm_defaults.js +++ b/src/assets/xterm_config/xterm_defaults.js @@ -3,66 +3,68 @@ window.loadOptions({ wetty_fit_terminal: true, wetty_void: 0, - cols: 80, - rows: 24, - cursorBlink: false, - cursorStyle: 'block', - cursorWidth: 1, - bellSound: DEFAULT_BELL_SOUND, - bellStyle: 'none', - drawBoldTextInBrightColors: true, - fastScrollModifier: 'alt', - fastScrollSensitivity: 5, - fontFamily: 'courier-new, courier, monospace', - fontSize: 15, - fontWeight: 'normal', - fontWeightBold: 'bold', - lineHeight: 1.0, - linkTooltipHoverDuration: 500, - letterSpacing: 0, - logLevel: 'info', - scrollback: 1000, - scrollSensitivity: 1, - screenReaderMode: false, - macOptionIsMeta: false, - macOptionClickForcesSelection: false, - minimumContrastRatio: 1, - disableStdin: false, - allowProposedApi: true, - allowTransparency: false, - tabStopWidth: 8, - rightClickSelectsWord: false, - rendererType: 'canvas', - windowOptions: {}, - windowsMode: false, - wordSeparator: ' ()[]{}\',"`', - altClickMovesCursor: true, - convertEol: false, - termName: 'xterm', - cancelEvents: false, + xterm: { + cols: 80, + rows: 24, + cursorBlink: false, + cursorStyle: 'block', + cursorWidth: 1, + bellSound: DEFAULT_BELL_SOUND, + bellStyle: 'none', + drawBoldTextInBrightColors: true, + fastScrollModifier: 'alt', + fastScrollSensitivity: 5, + fontFamily: 'courier-new, courier, monospace', + fontSize: 15, + fontWeight: 'normal', + fontWeightBold: 'bold', + lineHeight: 1.0, + linkTooltipHoverDuration: 500, + letterSpacing: 0, + logLevel: 'info', + scrollback: 1000, + scrollSensitivity: 1, + screenReaderMode: false, + macOptionIsMeta: false, + macOptionClickForcesSelection: false, + minimumContrastRatio: 1, + disableStdin: false, + allowProposedApi: true, + allowTransparency: false, + tabStopWidth: 8, + rightClickSelectsWord: false, + rendererType: 'canvas', + windowOptions: {}, + windowsMode: false, + wordSeparator: ' ()[]{}\',"`', + altClickMovesCursor: true, + convertEol: false, + termName: 'xterm', + cancelEvents: false, - theme: { - foreground: "#ffffff", - background: "#000000", - cursor: "#ffffff", - cursorAccent: "#000000", - selection: "#FFFFFF4D", + theme: { + foreground: "#ffffff", + background: "#000000", + cursor: "#ffffff", + cursorAccent: "#000000", + selection: "#FFFFFF4D", - black: "#2e3436", - red: "#cc0000", - green: "#4e9a06", - yellow: "#c4a000", - blue: "#3465a4", - magenta: "#75507b", - cyan: "#06989a", - white: "#d3d7cf", - brightBlack: "#555753", - brightRed: "#ef2929", - brightGreen: "#8ae234", - brightYellow: "#fce94f", - brightBlue: "#729fcf", - brightMagenta: "#ad7fa8", - brightCyan: "#34e2e2", - brightWhite: "#eeeeec" + black: "#2e3436", + red: "#cc0000", + green: "#4e9a06", + yellow: "#c4a000", + blue: "#3465a4", + magenta: "#75507b", + cyan: "#06989a", + white: "#d3d7cf", + brightBlack: "#555753", + brightRed: "#ef2929", + brightGreen: "#8ae234", + brightYellow: "#fce94f", + brightBlue: "#729fcf", + brightMagenta: "#ad7fa8", + brightCyan: "#34e2e2", + brightWhite: "#eeeeec" + } } }); diff --git a/src/assets/xterm_config/xterm_general_options.js b/src/assets/xterm_config/xterm_general_options.js index e3ddbd4..22df0a3 100644 --- a/src/assets/xterm_config/xterm_general_options.js +++ b/src/assets/xterm_config/xterm_general_options.js @@ -3,26 +3,26 @@ window.inflateOptions([ type: "text", name: "Font Family", description: "The font family for terminal text.", - path: ["fontFamily"], + path: ["xterm", "fontFamily"], }, { type: "number", name: "Font Size", description: "The font size in CSS pixels for terminal text.", - path: ["fontSize"], + path: ["xterm", "fontSize"], }, { type: "enum", name: "Regular Font Weight", description: "The font weight for non-bold text.", - path: ["fontWeight"], + path: ["xterm", "fontWeight"], enum: ["normal", "bold", "100", "200", "300", "400", "500", "600", "700", "800", "900"], }, { type: "enum", name: "Bold Font Weight", description: "The font weight for bold text.", - path: ["fontWeightBold"], + path: ["xterm", "fontWeightBold"], enum: ["normal", "bold", "100", "200", "300", "400", "500", "600", "700", "800", "900"], }, { @@ -35,70 +35,70 @@ window.inflateOptions([ type: "number", name: "Terminal Columns", description: "The number of columns in the terminal. Overridden by the Fit Terminal option.", - path: ["cols"], + path: ["xterm", "cols"], nullable: true, }, { type: "number", name: "Terminal Rows", description: "The number of rows in the terminal. Overridden by the Fit Terminal option.", - path: ["rows"], + path: ["xterm", "rows"], nullable: true, }, { type: "enum", name: "Cursor Style", description: "The style of the cursor", - path: ["cursorStyle"], + path: ["xterm", "cursorStyle"], enum: ["block", "underline", "bar"], }, { type: "boolean", name: "Blinking Cursor", description: "Whether the cursor blinks", - path: ["cursorBlink"], + path: ["xterm", "cursorBlink"], }, { type: "number", name: "Bar Cursor Width", description: "The width of the cursor in CSS pixels. Only applies when Cursor Style is set to 'bar'.", - path: ["cursorWidth"], + path: ["xterm", "cursorWidth"], }, { type: "boolean", name: "Draw Bold Text In Bright Colors", description: "Whether to draw bold text in bright colors", - path: ["drawBoldTextInBrightColors"], + path: ["xterm", "drawBoldTextInBrightColors"], }, { type: "number", name: "Scroll Sensitivity", description: "The scroll speed multiplier for regular scrolling.", - path: ["scrollSensitivity"], + path: ["xterm", "scrollSensitivity"], }, { type: "enum", name: "Fast Scroll Key", description: "The modifier key to hold to multiply scroll speed.", - path: ["fastScrollModifier"], + path: ["xterm", "fastScrollModifier"], enum: ["none", "alt", "shift", "ctrl"], }, { type: "number", name: "Fast Scroll Multiplier", description: "The scroll speed multiplier used for fast scrolling.", - path: ["fastScrollSensitivity"], + path: ["xterm", "fastScrollSensitivity"], }, { type: "number", name: "Scrollback Rows", description: "The amount of scrollback rows, rows you can scroll up to after they leave the viewport, to keep.", - path: ["scrollback"], + path: ["xterm", "scrollback"], }, { type: "number", name: "Tab Stop Width", description: "The size of tab stops in the terminal.", - path: ["tabStopWidth"], + path: ["xterm", "tabStopWidth"], }, ]); diff --git a/src/client/wetty/term/confiruragtion.ts b/src/client/wetty/term/confiruragtion.ts index 9d31a75..9617a1d 100644 --- a/src/client/wetty/term/confiruragtion.ts +++ b/src/client/wetty/term/confiruragtion.ts @@ -2,15 +2,16 @@ import _ from 'lodash'; import type { Term } from '../shared/type'; import { copySelected, copyShortcut } from './confiruragtion/clipboard'; -import { onInput } from './confiruragtion/editor'; +import { onInput, setOptions } from './confiruragtion/editor'; import { editor } from '../../shared/elements'; import { loadOptions } from './confiruragtion/load'; export function configureTerm(term: Term): void { - const options = loadOptions(); - Object.entries(options).forEach(([key, value]) => { - term.setOption(key, value); - }); + let options = loadOptions(); + //Convert old options to new options + if (!("xterm" in options)) options = { xterm: options }; + try { setOptions(term, options); } catch {} + const toggle = document.querySelector('#options .toggler'); const optionsElem = document.getElementById('options'); if (editor == null || toggle == null || optionsElem == null) throw new Error("Couldn't initialize configuration menu"); diff --git a/src/client/wetty/term/confiruragtion/editor.ts b/src/client/wetty/term/confiruragtion/editor.ts index 51317a9..e0c1495 100644 --- a/src/client/wetty/term/confiruragtion/editor.ts +++ b/src/client/wetty/term/confiruragtion/editor.ts @@ -5,11 +5,11 @@ export const onInput = (term: Term, updated: any) => { try { const updatedConf = JSON.stringify(updated, null, 2); if (localStorage.options === updatedConf) return; - Object.keys(updated).forEach(key => { - const value = updated[key]; - term.setOption(key, value); - }); - term.resizeTerm(); + setOptions(term, updated); + requestAnimationFrame(() => { + if (!updated.wetty_fit_terminal && updated.xterm.cols != null && updated.xterm.rows != null) term.resize(updated.xterm.cols, updated.xterm.rows); + term.resizeTerm(); + }); // editor.classList.remove('error'); localStorage.options = updatedConf; } catch (e) { @@ -18,3 +18,11 @@ export const onInput = (term: Term, updated: any) => { // editor.classList.add('error'); } }; + +export function setOptions(term: Term, options: any) { + Object.keys(options.xterm).forEach(key => { + if (key === "cols" || key === "rows") return; + const value = options[key]; + term.setOption(key, value); + }); +} diff --git a/src/client/wetty/term/confiruragtion/load.ts b/src/client/wetty/term/confiruragtion/load.ts index ecfcae6..00b7c50 100644 --- a/src/client/wetty/term/confiruragtion/load.ts +++ b/src/client/wetty/term/confiruragtion/load.ts @@ -1,7 +1,7 @@ import _ from 'lodash'; -export function loadOptions(): object { - const defaultOptions = { fontSize: 14 }; +export function loadOptions(): any { + const defaultOptions = { xterm: { fontSize: 14 } }; try { return _.isUndefined(localStorage.options) ? defaultOptions