From 364456c5327d36104a2aabd99abe66f5eddde7b6 Mon Sep 17 00:00:00 2001 From: Nick Mitchell Date: Wed, 7 Sep 2022 11:32:37 -0400 Subject: [PATCH] fix: remove ancient pretty printers for ancient usage model BREAKING CHANGE: this removes some of the exported pretty printers from kui-shell/core --- packages/core/src/core/usage/pretty-code.ts | 95 -------------------- packages/core/src/core/usage/render-usage.ts | 35 -------- packages/core/src/index.ts | 3 - plugins/plugin-s3/src/browse/index.ts | 17 ++-- 4 files changed, 5 insertions(+), 145 deletions(-) delete mode 100644 packages/core/src/core/usage/pretty-code.ts delete mode 100644 packages/core/src/core/usage/render-usage.ts diff --git a/packages/core/src/core/usage/pretty-code.ts b/packages/core/src/core/usage/pretty-code.ts deleted file mode 100644 index d0989b5aa2e..00000000000 --- a/packages/core/src/core/usage/pretty-code.ts +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2021 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. - */ - -import wrap from 'word-wrap' -import colors from 'colors/safe' - -import { isHeadless } from '../capabilities' -import { Title, Option, Example, Usage, Related } from './types' - -export function command(cmdline: string) { - return `${colors.cyan(cmdline)}` -} - -export function intro(paragraph: string) { - return wrap(paragraph, { width: 100, indent: ' ' }) + '\n' -} - -export function option(opts: string | string[]) { - return typeof opts === 'string' - ? colors.bold(opts) + colors.reset('') - : opts.map(_ => colors.bold(_)).join(', ') + colors.reset('') -} - -export function title(_: Title) { - return `${colors.bold(colors.yellow(_.command))} ${colors.reset(_.doc)} -` -} - -/** @return pretty-printed version of the given bash pipeline */ -export function pipeline(code: string): string { - return code - .replace(/(\s+\|\s+)(\w+)/g, (_, p1, p2) => `${p1}${colors.cyan(p2)}`) - .replace(/(\s+--\s+)(\w+)/g, (_, p1, p2) => `${p1}${colors.cyan(p2)}`) - .replace(/\[[^\]]*options[^\]]*\]/, _ => colors.gray(_)) - .replace(/\[[^\]]*name[^\]]*\]/, _ => colors.gray(_)) - .replace(/\/s3\/aws\/commoncrawl\/[./\-{},\w]+/, _ => colors.dim(_)) - .replace(/ \|/g, _ => colors.magenta(_)) - .replace(/ \\/g, _ => colors.dim(_)) - .replace(/kiwi super/, _ => colors.yellow(_)) -} - -// \u2063 is "invisible separator" https://www.fileformat.info/info/unicode/char/2063/index.htm -// needed for columnify, which deletes whitespace -const indent = ' ' -const indentedLines = '\n ' - -export function usage(usages: Usage[]) { - return `Usage: - ${usages.map(pipeline).join(indentedLines)} -` -} - -function clickable(cmdline: string) { - return isHeadless() ? command(cmdline) : `[${cmdline}](#kuiexec?command=${encodeURIComponent(cmdline)})` -} - -export function examples(examples: Example[], sectionTitle = 'Examples') { - const data = examples.map(_ => ({ - command: indent + clickable(_.command), - doc: _.doc - })) - - return `${sectionTitle}: -${require('columnify')(data, { showHeaders: false })} -` -} - -export function related(related: Related[]) { - return `Related: - ${colors.dim(related.map(command).join(', '))} -` -} - -export function options(options: Option[]) { - const data = options.map(_ => ({ - option: `${indent}${option(_.flags)}`, - doc: `${_.doc}${_.default ? ` ${colors.gray('default: ' + _.default)}` : ''}` - })) - return `Options: -${require('columnify')(data, { showHeaders: false })} -` -} diff --git a/packages/core/src/core/usage/render-usage.ts b/packages/core/src/core/usage/render-usage.ts deleted file mode 100644 index 02311db5249..00000000000 --- a/packages/core/src/core/usage/render-usage.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2021 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. - */ - -import PrettyUsageModel from './types' -import { title, intro, usage, examples, options, related } from './pretty-code' - -export default function renderUsage(_: PrettyUsageModel) { - try { - return [ - _.title && title(_.title), - _.intro && intro(_.intro), - _.usages && usage(_.usages), - _.examples && examples(_.examples), - _.options && options(_.options), - _.related && related(_.related) - ] - .filter(_ => _) - .join('\n') - } catch (err) { - console.error('Internal error formatting usage', err) - } -} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 7d9a6981077..f2d9ad88ac9 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -59,10 +59,7 @@ export { isUsageError, UsageError, UsageModel, UsageRow } from './core/usage-err export { isMessageWithUsageModel, isMessageWithCode } from './core/usage-error' // ascii usage -export { default as renderUsage } from './core/usage/render-usage' export { default as PrettyUsageModel } from './core/usage/types' -import * as pc from './core/usage/pretty-code' -export { pc } // eventChannelUnsafe export * from './api' diff --git a/plugins/plugin-s3/src/browse/index.ts b/plugins/plugin-s3/src/browse/index.ts index 67d5f5170d8..5e69fe4d267 100644 --- a/plugins/plugin-s3/src/browse/index.ts +++ b/plugins/plugin-s3/src/browse/index.ts @@ -14,23 +14,16 @@ * limitations under the License. */ -import { Arguments, PrettyUsageModel, Registrar, encodeComponent, renderUsage } from '@kui-shell/core' - -import * as Usage from './usage' +import { Arguments, Registrar, encodeComponent } from '@kui-shell/core' const opts = { needsUI: true, width: 720, height: 900 } -function doBrowse(this: string, usage: PrettyUsageModel, { REPL, parsedOptions }: Arguments) { - if (parsedOptions.h || parsedOptions.help) { - return renderUsage(usage) - } - +function doBrowse(this: string, { REPL }: Arguments) { return REPL.qexec(`ls ${encodeComponent(this)}`) } export default async function(registrar: Registrar) { - registrar.listen('/browse', renderUsage.bind(undefined, Usage.browse)) - registrar.listen('/browse/s3', doBrowse.bind('/s3', Usage.s3), opts) - registrar.listen('/browse/cc', doBrowse.bind('/s3/aws/commoncrawl', Usage.cc('cc')), opts) - registrar.listen('/browse/commoncrawl', doBrowse.bind('/s3/aws/commoncrawl', Usage.cc('commoncrawl')), opts) + registrar.listen('/browse/s3', doBrowse.bind('/s3'), opts) + registrar.listen('/browse/cc', doBrowse.bind('/s3/aws/commoncrawl'), opts) + registrar.listen('/browse/commoncrawl', doBrowse.bind('/s3/aws/commoncrawl'), opts) }