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
83 changes: 30 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@ Textwrap for JavaScript/Node.js.
Correctly handles wide characters (宽字符) and emojis (😃).
Optionally break words when wrapping strings. Preserves ANSI escape codes.

## Why?
## Packages in this repo

JavaScript's `String.length` counts code units, not visual terminal width.
Wide characters (CJK) and many emoji occupy 2 columns but have length 1.
This package wraps text to a target **visual** width using `wcwidth`.
| Install | Package | What you get |
|---------|---------|----------------|
| `npm install smartwrap` | **smartwrap** (this package) | Core library only — **no CLI, no `yargs`** |
| `npm install -g smartwrap-cli` | [smartwrap-cli](./packages/smartwrap-cli) | `smartwrap` binary |

## Installation
Library users who only need wrapping no longer pull in CLI dependencies ([#19](https://github.com/tecfu/smartwrap/issues/19)).

```bash
npm install smartwrap
```

CLI (global):
## Installation (library)

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

## Node module

### Wide character wrapping

```js
const smartwrap = require('smartwrap')
console.log(smartwrap('宽字符', { width: 2 }))
Expand All @@ -34,24 +27,14 @@ console.log(smartwrap('宽字符', { width: 2 }))
// 符
```

### Word wrapping (default)

```js
console.log(smartwrap('break at word', {
width: 10,
breakword: false // default
}))
console.log(smartwrap('break at word', { width: 10 }))
// break at
// word
```

### Force-break long words

```js
console.log(smartwrap('break at word', {
width: 10,
breakword: true
}))
console.log(smartwrap('break at word', { width: 10, breakword: true }))
// break at w
// ord
```
Expand All @@ -72,41 +55,35 @@ console.log(smartwrap('break at word', {
## CLI

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

```
so
me
st
ri
ng
yo
u
wa
nt
to
wr
ap
```

Run `smartwrap --help` for all flags.

## Breaking changes in 3.0.0

- **Node.js ≥ 12** required (native `Array.prototype.flat`).
- Removed unused `grapheme-splitter` and the `array.prototype.flat` polyfill (eliminates ~50 transitive dependencies).
- Updated `yargs` and other dependencies.
- CLI now correctly passes `breakword` and `errorChar`.
- Test runner switched from Grunt to plain Mocha.
- Minor code cleanups (fixed undeclared variable, modernized style).
- **CLI moved** to the separate package `smartwrap-cli`.
`npm install smartwrap` no longer installs `yargs` or the `smartwrap` binary.
- Removed unused `grapheme-splitter` and the `array.prototype.flat` polyfill.
- CLI options (`breakword`, `errorChar`, etc.) are forwarded correctly in `smartwrap-cli`.
- Test runner switched from Grunt to Mocha.

Existing wrapping behavior for supported inputs is intentionally preserved.
## Development

## Compatibility
```bash
npm install
npm test
```

To exercise the CLI locally against this checkout:

- Node.js ≥ 12
- CommonJS
```bash
cd packages/smartwrap-cli
npm install
npm link ../.. # or: npm install file:../..
# then: echo "宽字符" | node src/terminal-adapter.js -w 2
```

## License

Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
"engines": {
"node": ">=12"
},
"bin": {
"smartwrap": "src/terminal-adapter.js"
},
"scripts": {
"test": "mocha test/test.js",
"test-display": "mocha test/test.js --require test/display-hook.js",
"lint": "eslint src/",
"lint-fix": "eslint src/ --fix"
"lint": "eslint src/ packages/smartwrap-cli/src/"
},
"repository": {
"type": "git",
Expand All @@ -32,8 +28,7 @@
"dependencies": {
"breakword": "^1.0.5",
"strip-ansi": "^6.0.1",
"wcwidth": "^1.0.1",
"yargs": "^17.7.2"
"wcwidth": "^1.0.1"
},
"devDependencies": {
"chai": "^4.3.10",
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": ">=12"
},
"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"
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict'

const smartwrap = require('./main.js')
const smartwrap = require('smartwrap')
const yargs = require('yargs')

yargs
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chai = require('chai');
const expect = chai.expect;
const assert = chai.assert;
const should = chai.should();
const smartwrap = require("../");
const smartwrap = require("..");
const data = require("./data")

let test = function(testResult,savedResult){
Expand Down