UNPKG

@kui-shell/plugin-core-support

Version:

Kui plugin offering core extensions such as help and screenshot commands

128 lines 5.56 kB
/* * Copyright 2019 The Kubernetes Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import Debug from 'debug'; const debug = Debug('plugins/core-support/theme'); function i18n() { return __awaiter(this, void 0, void 0, function* () { const { i18n } = yield import('@kui-shell/core/mdist/api/i18n'); return i18n('plugin-core-support'); }); } function strings(key) { return __awaiter(this, void 0, void 0, function* () { return (yield i18n())(key); }); } /** * List themes * */ const list = ({ REPL }) => __awaiter(void 0, void 0, void 0, function* () { const strings = yield i18n(); const { uiThemes } = yield import('@kui-shell/core/mdist/api/Settings'); const { CellShould } = yield import('@kui-shell/core/mdist/api/Table'); const { getPersistedThemeChoice, getDefaultTheme, findThemeByName } = yield import('@kui-shell/core/mdist/api/Themes'); const header = { cells: [ yield strings('Theme'), { value: yield strings('Style'), hints: "hide-with-narrow-window" /* CellShould.HideWhenNarrow */ }, { value: yield strings('Provider'), hints: "hide" /* CellShould.BeHidden */ } ] }; // careful: the user's chosen theme might not be available in the // settings.themes model; e.g. they previously selected a theme that // has since been eliminated const currentTheme = () => __awaiter(void 0, void 0, void 0, function* () { const chosenTheme = (yield getPersistedThemeChoice()) || (yield getDefaultTheme()); return findThemeByName(chosenTheme) ? chosenTheme : getDefaultTheme(); }); debug('currentTheme', yield currentTheme()); // debug('theme list', uiThemes()) const { flatten } = yield import('@kui-shell/core/mdist/api/Util'); const body = flatten((yield uiThemes()).map(({ plugin, themes }) => themes.map((theme) => ({ nameIdx: 0, cells: [ { value: theme.name }, { value: strings(theme.style), hints: "hide-with-narrow-window" /* CellShould.HideWhenNarrow */ }, { value: plugin, hints: ["hide" /* CellShould.BeHidden */] } ], onSelectExec: 'qexec', onSelect: `theme set ${REPL.encodeComponent(theme.name)}` })))).sort((a, b) => (a.cells[2].value === 'plugin-core-themes' ? 1 : b.cells[2].value === 'plugin-core-themes' ? -1 : 0)); const getSelectedIdx = () => __awaiter(void 0, void 0, void 0, function* () { const current = yield currentTheme(); return body.findIndex(_ => _.cells[0].value === current); }); const defaultSelectedIdx = yield getSelectedIdx(); const table = { apiVersion: 'kui-shell/v1', kind: 'RadioTable', title: yield strings('theme.Available Themes'), header, body, defaultSelectedIdx }; return table; }); /** * REPL command to switch themes * */ const set = ({ argvNoOptions }) => __awaiter(void 0, void 0, void 0, function* () { const theme = argvNoOptions[argvNoOptions.indexOf('set') + 1]; debug('set', theme); const { switchToTheme } = yield import('@kui-shell/core/mdist/api/Themes'); yield switchToTheme(theme); return true; }); function getPersistedThemeChoice() { return __awaiter(this, void 0, void 0, function* () { const { getPersistedThemeChoice } = yield import('@kui-shell/core/mdist/api/Themes'); return getPersistedThemeChoice(); }); } function resetToDefaultTheme() { return __awaiter(this, void 0, void 0, function* () { const { resetToDefaultTheme } = yield import('@kui-shell/core/mdist/api/Themes'); return resetToDefaultTheme(); }); } /** * The command handlers * */ export const plugin = (commandTree) => { debug('plugin'); commandTree.listen('/theme/list', list); commandTree.listen('/themes', list); commandTree.listen('/theme', list); commandTree.listen('/theme/set', set); // returns the current persisted theme choice; helpful for debugging commandTree.listen('/theme/current', () => __awaiter(void 0, void 0, void 0, function* () { return (yield getPersistedThemeChoice()) || (yield strings('theme.currentTheme')); }), { hidden: true }); // for debugging commandTree.listen('/theme/reset', resetToDefaultTheme); }; //# sourceMappingURL=theme.js.map