Comparing version 0.1.4 to 0.2.0
@@ -0,1 +1,2 @@ | ||
require('colors'); | ||
const boxed = require('./'); | ||
@@ -5,2 +6,4 @@ | ||
log(''); | ||
log('Celebrate what you want to see more of'); | ||
@@ -17,1 +20,24 @@ | ||
I have a dream today!`, {color: 'green', padding: 5, theme: 'round'}); | ||
log(`I am aligned to the left | ||
see?`, {align: 'left'}); | ||
log(`I, however, | ||
am aligned to the right.`, {align: 'end'}); | ||
log( | ||
['My white spaces counts! ', | ||
'I can move blocks of text from side to side.', | ||
' and I control it manually', | ||
' ๐ ', | ||
].join('\n')); | ||
log(`Common ๐ emojis ${'and decorated text'.green.bold.underline} | ||
are supported ๐บ and celebrated ๐ | ||
we also support left, right, and center alignment โฝ๏ธ `, {align: 'left'}); | ||
log(`some comopund emojis still need work | ||
๐จโ๐งโ๐ฆ | ||
๐จโ๐จโ๐ฆโ๐ฆ | ||
๐ง๐ท | ||
๐๐พ`); |
33
index.js
const colors = require('colors'); | ||
const length = require('./lib/length'); | ||
const themes = require('./themes.json') | ||
@@ -9,3 +10,3 @@ | ||
*/ | ||
const maxLength = (...args) => args.reduce((max, arg) => Math.max(arg.length, max), 0); | ||
const maxLength = (...args) => args.reduce((max, arg) => Math.max(length(arg), max), 0); | ||
@@ -33,2 +34,3 @@ /** | ||
const PADDING = 2; | ||
/** | ||
@@ -41,2 +43,8 @@ * default boxedOptions theme | ||
/** | ||
* default align value | ||
* @type {String} | ||
*/ | ||
const ALIGN = 'center'; | ||
/** | ||
* @typedef boxedOptions | ||
@@ -69,2 +77,3 @@ * @type {Object} | ||
theme = THEME, | ||
align = ALIGN, | ||
} = options; | ||
@@ -82,3 +91,3 @@ | ||
const lines = message.split('\n'); | ||
const width = maxLength(...message.strip.split('\n')); | ||
const width = maxLength(...message.split('\n')); | ||
const space = width + padding * 2; | ||
@@ -109,9 +118,23 @@ const times = (string = ' ', length = space) => arrayOf(string, length); | ||
...lines.map(line => { | ||
const w = width + line.length - line.strip.length; // white space width including style chars | ||
const linePad = Math.ceil(w - (w - line.length) / 2); | ||
const w = width + line.length - length(line); // white space width including style chars | ||
const content = (() => { | ||
switch (align) { | ||
case 'left': | ||
case 'start': | ||
return line.padEnd(w, ' '); | ||
case 'right': | ||
case 'end': | ||
return line.padStart(w, ' '); | ||
case 'center': | ||
default: | ||
const linePad = Math.ceil(w - (w - length(line)) / 2); | ||
return line.padEnd(linePad, ' ').padStart(w, ' ') | ||
} | ||
})(); | ||
return [ | ||
v, | ||
times(' ', padding), | ||
line.padEnd(linePad, ' ').padStart(w, ' '), | ||
content, | ||
times(' ', padding), | ||
@@ -118,0 +141,0 @@ v |
{ | ||
"name": "boxt", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Create boxes around text", | ||
@@ -12,3 +12,4 @@ "author": "omri", | ||
"scripts": { | ||
"test": "node examples" | ||
"start": "node examples", | ||
"test": "mocha test.js lib/**/test.js" | ||
}, | ||
@@ -18,3 +19,7 @@ "license": "MIT", | ||
"colors": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"mocha": "^5.0.0" | ||
} | ||
} |
@@ -5,2 +5,4 @@ # boxt | ||
<p align="center"><img width="467" alt="image" src="https://user-images.githubusercontent.com/516342/35336267-777d3386-0120-11e8-8e9f-4af2fb7480f0.png"></p> | ||
## Use | ||
@@ -28,10 +30,16 @@ `npm i -S boxt` | ||
### Examples | ||
| option | meaning | values | ||
| --- | --- | --- | ||
| theme | border style | 'single', 'double', 'round' | ||
| theme | border style | 'single' (default), 'double', 'round' | ||
| color | border colour | see [`colors` on NPM](https://www.npmjs.com/package/colors) | ||
| padding | space from borders to text | | ||
| align | Where to align the lines | 'center' (default), 'left'/'start', 'right'/'end' | ||
<p align="center"><img width="420" alt="image" src="https://user-images.githubusercontent.com/516342/34468263-dcb7e87e-ef0d-11e7-9ae6-1f44d144a1b1.png"></p> | ||
| options | result | | ||
| --- | --- | | ||
| **theme**: 'double'<br>**color**: 'bgBlue' | <img width="420" alt="image" src="https://user-images.githubusercontent.com/516342/34468263-dcb7e87e-ef0d-11e7-9ae6-1f44d144a1b1.png"> | | ||
| **theme**: 'round'<br>**color**: 'green' | <img width="402" alt="image" src="https://user-images.githubusercontent.com/516342/34468268-f85f6d40-ef0d-11e7-9fc5-746cd20f4e25.png"> | | ||
| **padding**: 10<br>**align**: 'left' | <img width="371" alt="image" src="https://user-images.githubusercontent.com/516342/35336411-01797bb2-0121-11e8-931f-03a7ff9df225.png"> | | ||
<p align="center"><img width="402" alt="image" src="https://user-images.githubusercontent.com/516342/34468268-f85f6d40-ef0d-11e7-9fc5-746cd20f4e25.png"></p> |
11386
9
266
44
2