Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle unicode characters #4

Merged
merged 1 commit into from
Jan 10, 2018
Merged
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
6 changes: 3 additions & 3 deletions lib/tweets.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { JSDOM } = require('jsdom');
const { wrap } = require('./utils');
const { wrap, getDisplaySize } = require('./utils');

function renderRegularBox(content, width) {
let box = content.reduce((agg, line) => {
agg += `| ${line.padEnd(width + 1, ' ')}|\n`;
agg += `| ${line}${' '.repeat((width - getDisplaySize(line)) + 1)}|\n`;

return agg;
}, `.${'-'.repeat(width + 2)}.\n`);
Expand All @@ -18,7 +18,7 @@ function renderContextBox(content, width) {
let box = content.reduce((agg, line, index) => {
const side = index === 0 ? " '-| " : ' | ';

agg += `${side}${line.padEnd(width + 1, ' ')}| \n`;
agg += `${side}${line}${' '.repeat((width - getDisplaySize(line)) + 1)}| \n`;

return agg;
}, ` | .${'-'.repeat(width + 2)}. \n`);
Expand Down
28 changes: 20 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
const { execSync } = require('child_process');
const wcwidth = require('wcwidth');

function getDisplaySize(text) {
return text
.split('')
.reduce((count, char) => count + wcwidth(char), 0);
}

function wrap(text, maxLineLength) {
const { length } = text;

if (length < maxLineLength) {
return text;
}

let i = 0;
let offset = 0;
let lineLength = 0;
let lastSpace;
let lastSpace = 0;
const output = [];
const sizeMap = [];

while (i < length) {
sizeMap[i] = wcwidth(text[i]);

if (text[i - 1] === ' ') {
lastSpace = i;
}

if (text[i] === '\n') {
lineLength = 0;
} else if (lineLength >= maxLineLength) {
if (i - lastSpace < maxLineLength) {
let chunkSize = 0;
for (let k = lastSpace; k <= i; k++) {
chunkSize += sizeMap[k];
}

if (chunkSize < maxLineLength) {
i = lastSpace;
}

Expand All @@ -32,8 +43,8 @@ function wrap(text, maxLineLength) {

output[i + offset] = text[i];

lineLength += sizeMap[i] > 0 ? sizeMap[i] : 1;
++i;
++lineLength;
}

return output.join('').split('\n');
Expand All @@ -45,7 +56,7 @@ function colorize(code, string) {

function center(width, text) {
const padding =
' '.repeat(Math.floor(width / 2) - Math.ceil(text.length / 2));
' '.repeat(Math.floor(width / 2) - Math.ceil(getDisplaySize(text) / 2));

return padding + text + padding;
}
Expand All @@ -63,4 +74,5 @@ module.exports = {
colorize,
center,
fetchFromTwitter,
getDisplaySize,
};
35 changes: 28 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"jsdom": "^11.5.1"
"jsdom": "^11.5.1",
"wcwidth": "^1.0.1"
},
"devDependencies": {
"eslint": "^4.14.0",
Expand Down