Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: implement smart wrapping for all contexts #140

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Expand Up @@ -11,6 +11,9 @@
}
}
],
"globals": {
"Deno": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
Expand Down
13 changes: 13 additions & 0 deletions browser.mjs
@@ -0,0 +1,13 @@
// Bootstrap cliui with esm dependencies:
import { cliui } from './build/lib/index.js'
import stringWidth from 'https://esm.sh/string-width@^6'
import stripAnsi from 'https://esm.sh/strip-ansi@^7'
import wrap from 'https://esm.sh/wrap-ansi@^8'

export default function ui (opts) {
return cliui(opts, {
stringWidth,
stripAnsi,
wrap
})
}
23 changes: 16 additions & 7 deletions deno.ts
@@ -1,13 +1,22 @@
// Bootstrap cliui with CommonJS dependencies:
// Bootstrap cliui with esm dependencies:
import { cliui, UI } from './build/lib/index.js'
import type { UIOptions } from './build/lib/index.d.ts'
import { wrap, stripAnsi } from './build/lib/string-utils.js'

export default function ui (opts: UIOptions): UI {
return cliui(opts, {
stringWidth: (str: string) => {
return [...str].length
},
import stringWidth from 'https://esm.sh/string-width@6'
import stripAnsi from 'https://esm.sh/strip-ansi@7'
import wrap from 'https://esm.sh/wrap-ansi@8'

export default function ui (opts?: UIOptions): UI {
let optsWithWidth = opts ?? {}
if (!optsWithWidth.width) {
const { columns } = Deno.consoleSize()
if (columns) {
optsWithWidth = Object.assign(optsWithWidth, { width: columns })
}
}

return cliui(optsWithWidth, {
stringWidth,
stripAnsi,
wrap
})
Expand Down
8 changes: 4 additions & 4 deletions index.mjs
@@ -1,12 +1,12 @@
// Bootstrap cliui with CommonJS dependencies:
import { cliui } from './build/lib/index.js'
import { wrap, stripAnsi } from './build/lib/string-utils.js'
import stringWidth from 'string-width'
import stripAnsi from 'strip-ansi'
import wrap from 'wrap-ansi'

export default function ui (opts) {
return cliui(opts, {
stringWidth: (str) => {
return [...str].length
},
stringWidth,
stripAnsi,
wrap
})
Expand Down
2 changes: 1 addition & 1 deletion lib/cjs.ts
Expand Up @@ -3,7 +3,7 @@ import { cliui, UIOptions } from './index.js'
const stringWidth = require('string-width')
const stripAnsi = require('strip-ansi')
const wrap = require('wrap-ansi')
export default function ui (opts: UIOptions) {
export default function ui (opts?: UIOptions) {
return cliui(opts, {
stringWidth,
stripAnsi,
Expand Down
7 changes: 4 additions & 3 deletions lib/index.ts
Expand Up @@ -11,10 +11,11 @@ const bottom = 2
const left = 3

export interface UIOptions {
width: number;
width?: number;
wrap?: boolean;
rows?: string[];
}
type UIConstructorOptions = UIOptions & {width:number};

interface Column {
text: string;
Expand Down Expand Up @@ -45,7 +46,7 @@ export class UI {
wrap: boolean;
rows: ColumnArray[];

constructor (opts: UIOptions) {
constructor (opts: UIConstructorOptions) {
this.width = opts.width
this.wrap = opts.wrap ?? true
this.rows = []
Expand Down Expand Up @@ -380,7 +381,7 @@ function alignCenter (str: string, width: number): string {
}

let mixin: Mixin
export function cliui (opts: Partial<UIOptions>, _mixin: Mixin) {
export function cliui (opts: UIOptions | undefined, _mixin: Mixin) {
mixin = _mixin
return new UI({
width: opts?.width || getWindowWidth(),
Expand Down
4 changes: 4 additions & 0 deletions lib/string-utils.ts
Expand Up @@ -2,6 +2,10 @@
// to facilitate ESM and Deno modules.
// TODO: look at porting https://www.npmjs.com/package/wrap-ansi to ESM.

// The current approach is to use the cjs version of the modules for node, and the esm version
// for contexts which can import from URLs. This file could be deleted if/once we are happy with
// the new approach.

// The npm application
// Copyright (c) npm, Inc. and Contributors
// Licensed on the terms of The Artistic License 2.0
Expand Down
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,10 @@
"require": "./build/index.cjs"
},
"./build/index.cjs"
]
],
"./browser.mjs": {
"import": "./browser.mjs"
}
},
"type": "module",
"module": "./index.mjs",
Expand All @@ -20,6 +23,7 @@
"pretest": "rimraf build && tsc -p tsconfig.test.json && cross-env NODE_ENV=test npm run build:cjs",
"test": "c8 mocha ./test/*.cjs",
"test:esm": "c8 mocha ./test/esm/cliui-test.mjs",
"test:deno": "deno test ./test/deno/cliui-test.ts",
"postest": "check",
"coverage": "c8 report --check-coverage",
"precompile": "rimraf build",
Expand Down Expand Up @@ -75,6 +79,7 @@
"files": [
"build",
"index.mjs",
"browser.mjs",
"!*.d.ts"
],
"engines": {
Expand Down
9 changes: 5 additions & 4 deletions test/deno/cliui-test.ts
Expand Up @@ -39,10 +39,11 @@ Deno.test('evenly divides text across columns if multiple columns are given', ()
// TODO: we should flesh out the Deno and ESM implementation
// such that it spreads words out over multiple columns appropriately:
const expected = [
'i am a string ti am a seconi am a third',
'hat should be wd string tha string that',
'rapped t should be should be w',
' wrapped rapped'
'i am a string i am a i am a third',
'that should be second string that',
'wrapped string that should be',
' should be wrapped',
' wrapped'
]

ui.toString().split('\n').forEach((line: string, i: number) => {
Expand Down
11 changes: 6 additions & 5 deletions test/esm/cliui-test.mjs
Expand Up @@ -35,11 +35,12 @@ describe('ESM', () => {
// TODO: we should flesh out the Deno and ESM implementation
// such that it spreads words out over multiple columns appropriately:
const expected = [
'i am a string ti am a seconi am a third',
'hat should be wd string tha string that',
'rapped t should be should be w',
' wrapped rapped'
]
'i am a string i am a i am a third',
'that should be second string that',
'wrapped string that should be',
' should be wrapped',
' wrapped'
]
ui.toString().split('\n').forEach((line, i) => {
strictEqual(line, expected[i])
})
Expand Down