Skip to content

support east asian full-with character #23

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
31 changes: 24 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 @@ -84,6 +84,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.0",
"@types/vscode": "^1.79.0",
"eastasianwidth": "^0.3.0",
"glob": "^8.1.0",
"minimist": "^1.2.7",
"mkdirp": "^1.0.4",
Expand All @@ -93,4 +94,4 @@
"@vscode/test-electron": "^2.4.1"
},
"dependencies": {}
}
}
12 changes: 7 additions & 5 deletions src/TableWriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isNumber } from 'util';
import CsvColumn from './CsvColumn';
import CsvRecord from './CsvRecord';
import * as eaw from 'eastasianwidth';

/**
* Class resposible for rendering a provided list of CsvRecord's into an ASCII table
Expand Down Expand Up @@ -109,8 +110,9 @@ export default class TableWriter

// Calculate left and right padding
const isRightAligned = rightAlignNumbers && this.isNumberValue(value);
const leftPaddingLength = useValuePadding ? (isRightAligned ? maxLen - value.length - 1 : 1) : 0;
const rightPaddingLength = isRightAligned ? 0 : maxLen - (ValuePadding.length * 2) - value.length;
const stringWidth = eaw.length(value);
const leftPaddingLength = useValuePadding ? (isRightAligned ? maxLen - stringWidth - 1 : 1) : 0;
const rightPaddingLength = isRightAligned ? 0 : maxLen - (ValuePadding.length * 2) - stringWidth;

// Start with column separator?
if (i === 0) {
Expand Down Expand Up @@ -147,8 +149,8 @@ export default class TableWriter
const record = records[i];
const columns = record.getColumns();

for(var colIndex = 0; colIndex < columns.length; colIndex++) {
const len = columns[colIndex].getValue().length;
for (var colIndex = 0; colIndex < columns.length; colIndex++) {
const len = eaw.length(columns[colIndex].getValue());

if (columnLengths[colIndex] === undefined || len > columnLengths[colIndex]) {
columnLengths[colIndex] = len;
Expand All @@ -169,7 +171,7 @@ export default class TableWriter
return false;

// Look for number-like characters
for(let i = 0; i < value.length; i++) {
for (let i = 0; i < value.length; i++) {
const char = value[i];

// Check for number signs
Expand Down
28 changes: 28 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,32 @@ suite('Extension Test Suite', () => {
assert.equal("c", columns2[0].getValue());
assert.equal("quoted", columns2[1].getValue());
});

test('The width of East Asian character is 2', () => {
// Arrange
const parser = new CsvParser(`\
origin name,english name
메이플스토리,MapleStory
スーパーマリオ,Super Mario
League Of Legends,League Of Legends
`, ',');
const writer = new TableWriter();

// Act
const records = parser.getRecords();
const result = writer.getFormattedTable(records, false, false, false);

// Assert
assert.equal(`
|-------------------|-------------------|
| origin name | english name |
|-------------------|-------------------|
| 메이플스토리 | MapleStory |
|-------------------|-------------------|
| スーパーマリオ | Super Mario |
|-------------------|-------------------|
| League Of Legends | League Of Legends |
|-------------------|-------------------|
`.trim(), result.replace(/\r?\n/g, "\n").trim());
});
});