Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ Textwrap for JavaScript/Node.js, written in **TypeScript**.
Correctly handles wide characters (宽字符) and emojis (😃).
Optionally break words when wrapping strings. Preserves ANSI escape codes.

Ships compiled CommonJS + declaration files (`.d.ts`).
Ships compiled CommonJS + ESM + declaration files (`.d.ts`).

## Installation

**Library only** (no CLI, no `yargs`):

```bash
npm install smartwrap
```

CLI (global):
**CLI** (separate package):

```bash
npm install -g smartwrap
npm install -g smartwrap-cli
```

| Install | Package | Contents |
|---------|---------|----------|
| `npm install smartwrap` | **smartwrap** | Core library only |
| `npm install -g smartwrap-cli` | **smartwrap-cli** | `smartwrap` binary + yargs |

## Usage

```ts
Expand Down Expand Up @@ -76,12 +83,14 @@ npm test

Source is pure TypeScript under `src/`. Published artifacts live in `dist/`.

The CLI lives in `packages/smartwrap-cli` and is published separately as `smartwrap-cli`.

## Breaking changes in 3.0.0

- **Node.js ≥ 14** required.
- Source rewritten in TypeScript; `main` / `types` point at `dist/`.
- Removed unused `grapheme-splitter` and the `array.prototype.flat` polyfill.
- See prior changelog notes on the modernize release for dependency cleanup details.
- CLI moved to the separate **`smartwrap-cli`** package (core no longer depends on `yargs`).
- Removed unused `grapheme-splitter`, `breakword`, and the `array.prototype.flat` polyfill.

## License

Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "smartwrap",
"version": "3.0.0",
"description": "Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Wraps strings with option to break on words.",
"description": "Textwrap for javascript/nodejs. Correctly handles wide characters (\u5bbd\u5b57\u7b26) and emojis (\ud83d\ude03). Wraps strings with option to break on words.",
"main": "./dist/cjs/main.js",
"module": "./dist/esm/main.js",
"types": "./dist/cjs/main.d.ts",
Expand All @@ -22,9 +22,6 @@
"engines": {
"node": ">=14"
},
"bin": {
"smartwrap": "./dist/cjs/terminal-adapter.js"
},
"scripts": {
"build": "npm run build:cjs && npm run build:esm && node scripts/write-package-jsons.js",
"build:cjs": "tsc -p tsconfig.cjs.json",
Expand Down Expand Up @@ -53,13 +50,11 @@
"license": "MIT",
"dependencies": {
"strip-ansi": "^6.0.1",
"wcwidth": "^1.0.1",
"yargs": "^17.7.2"
"wcwidth": "^1.0.1"
},
"devDependencies": {
"@types/node": "^18.19.0",
"@types/strip-ansi": "^5.2.1",
"@types/yargs": "^17.0.33",
"chai": "^4.3.10",
"eslint": "^8.57.0",
"mocha": "^10.7.3",
Expand Down
19 changes: 19 additions & 0 deletions packages/smartwrap-cli/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions packages/smartwrap-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# smartwrap-cli

Command-line interface for [smartwrap](https://www.npmjs.com/package/smartwrap).

## Install

```bash
npm install -g smartwrap-cli
```

For library-only use (no CLI, no `yargs`), install [`smartwrap`](https://www.npmjs.com/package/smartwrap) instead.

## Usage

```bash
echo "somestring you want to wrap" | smartwrap --width=3 --paddingLeft=1
smartwrap --help
```

## License

MIT
33 changes: 33 additions & 0 deletions packages/smartwrap-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "smartwrap-cli",
"version": "3.0.0",
"description": "CLI for smartwrap — text wrapping with wide-character and emoji support",
"bin": {
"smartwrap": "src/terminal-adapter.js"
},
"engines": {
"node": ">=14"
},
"repository": {
"type": "git",
"url": "https://github.com/tecfu/smartwrap.git",
"directory": "packages/smartwrap-cli"
},
"keywords": [
"smartwrap",
"wordwrap",
"cli",
"terminal"
],
"author": "tecfu",
"license": "MIT",
"dependencies": {
"smartwrap": "^3.0.0",
"yargs": "^17.7.2"
},
"files": [
"src/",
"LICENSE",
"README.md"
]
}
23 changes: 12 additions & 11 deletions src/terminal-adapter.ts → ...ges/smartwrap-cli/src/terminal-adapter.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
import smartwrap, { type SmartwrapOptions } from './main'
import yargs from 'yargs'
'use strict'

const smartwrap = require('smartwrap')
const yargs = require('yargs')

const argv = yargs
.option('breakword', {
Expand All @@ -13,8 +15,8 @@ const argv = yargs
describe: 'Placeholder for wide characters when minWidth < 2'
})
.option('minWidth', {
choices: [1, 2] as const,
default: 2 as 1 | 2,
choices: [1, 2],
default: 2,
describe:
'Minimum line width. Use 1 only if you are certain you are not using wide characters and want a 1-space column.'
})
Expand All @@ -29,7 +31,7 @@ const argv = yargs
type: 'number'
})
.option('splitAt', {
default: [' ', '\t'] as string[],
default: [' ', '\t'],
describe: 'Characters at which to split input'
})
.option('trim', {
Expand All @@ -42,7 +44,7 @@ const argv = yargs
default: 10,
describe: 'Set the line width of the output (in spaces)',
demandOption: true,
coerce: (arg: string | number): number => {
coerce: (arg) => {
const n = Number(arg)
if (Number.isNaN(n)) {
throw new Error('Invalid width specified.')
Expand All @@ -54,7 +56,7 @@ const argv = yargs
.alias('h', 'help')
.parseSync()

const options: SmartwrapOptions = {}
const options = {}
const keys = [
'breakword',
'errorChar',
Expand All @@ -64,18 +66,17 @@ const keys = [
'splitAt',
'trim',
'width'
] as const
]

for (const key of keys) {
if (typeof argv[key] !== 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(options as any)[key] = argv[key]
options[key] = argv[key]
}
}

process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk: string) => {
process.stdin.on('data', (chunk) => {
const out = smartwrap(chunk, options)
console.log(out)
})
9 changes: 0 additions & 9 deletions scripts/write-package-jsons.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,5 @@ if (fs.existsSync(cjsMain)) {
}
}

const bin = path.join('dist', 'cjs', 'terminal-adapter.js')
if (fs.existsSync(bin)) {
let content = fs.readFileSync(bin, 'utf8')
if (!content.startsWith('#!')) {
content = '#!/usr/bin/env node\n' + content
fs.writeFileSync(bin, content)
}
fs.chmodSync(bin, 0o755)
}

console.log('Wrote dist package metadata and CJS interop')
Loading