Skip to content

Commit

Permalink
add wrapping for non-cjs
Browse files Browse the repository at this point in the history
- default width for deno
- make opts optional for clients
- make opts.width optional for clients
  • Loading branch information
shadowspawn committed May 13, 2023
1 parent af3145d commit 2be22e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
22 changes: 15 additions & 7 deletions deno.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
5 changes: 3 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface UIOptions {
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

0 comments on commit 2be22e0

Please sign in to comment.