Code Monkey home page Code Monkey logo

smartwrap's Introduction

smartwrap

Build Status Dependency Status NPM version

Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Optionally break words when wrapping strings.

Why?

I needed a javascript package to correctly wrap wide characters - which have a "length" property value of 1 but occupy 2 or more spaces in the terminal.

Example Usages:

Terminal:

npm i -g smartwrap
echo somestring you want to wrap | smartwrap --width=3 --paddingLeft=1

Output:

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

Node module:

Wide Character Wrapping

var Smartwrap = require('smartwrap');
var exampleText1 = '宽字符';
console.log(Smartwrap(exampleText1,{
  width: 2
}));
  • Output:
宽
字
符

String Wrapping

let exampleText2 = "break at word"

console.log(smartwrap(exampleText2,{
  width: 10,
  breakword: false //default
}))
  • Output:
break at
word

Breaking Words When Wrapping Strings

console.log(smartwrap(exampleText2,{
  width: 10,
  breakword: true
}))
  • Output:
break at w
ord

Options

--breakword       Choose whether or not to break words when wrapping a string
                                                                 [default: false]
--errorChar       Placeholder for wide characters when minWidth < 2
                                                                 [default: �]
--minWidth        Never change this unless you are certin you are not using
                  wide characters and you want a column 1 space wide. Then
                  change to 1.                   [choices: 1, 2] [default: 2]
--paddingLeft     Set the left padding of the output             [default: 0]
--paddingRight    Set the right padding of the output            [default: 0]
--splitAt         Characters at which to split input    [default: [" ","\t"]]
--trim            Trim the whitespace from end of input       [default: true]
--width, -w       Set the line width of the output (in spaces)
                                                     [required] [default: 10]

Compatibility

node 6.0 <

License

MIT

smartwrap's People

Contributors

dependabot[bot] avatar hipsterzipster avatar mscalora avatar tecfu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

smartwrap's Issues

upgrade GPL 2.0 to GPL 3.0

Hi man. I tried to use your module to integrate to my module. Can you please to consider upgrade your GPL 2.0 license to GPL 3.0? That would be more clearly.

smartwrap is not as smart as breakword

smartwrap is not able to break words as well as breakword

I think maybe there is some use of .split() and/or .substr() that needs to be changed to arrays created with the spread operator [...str] which knows how to correctly divide strings to characters

See output from:

const breakword = require("breakword")

const str = "😀😀😀😀"

console.log(`String: ${str}`)
for (let w = 0; w < 10; w++) {
  let x = breakword(str,w),
    part1 = [...str].slice(0, x + 1).join(""),
    part2 = [...str].slice(x + 1).join("")

  console.log(`w=${w} bw=${x} "${part1}"   "${part2}"`)
}

const smartwrap = require("smartwrap")

console.log(`String: ${str}`)
for (let w = 0; w < 10; w++) {
  let wrapped = smartwrap(str, {width: w, breakword: true, errorChar: "é"}),
    lines = wrapped.split("\n"),
    part1 = lines[0],
    part2 = lines[1] || null

  console.log(`lines=${lines.length} ${JSON.stringify(part1)}   ${JSON.stringify(part2)}`)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.