Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Width Limit of Column * Updated dependencies
- Loading branch information
Showing
17 changed files
with
585 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import findWidthInConsole from './console-utils'; | ||
import { ALIGNMENT } from './table-constants'; | ||
import { cellText } from './table-helpers'; | ||
|
||
// ("How are you?",center, 20) => " How are you? " | ||
// ("How are you?",right, 20) => " How are you?" | ||
export const textWithPadding = ( | ||
text: string, | ||
alignment: ALIGNMENT, | ||
mxColumnLen: number | ||
): string => { | ||
const curTextSize = findWidthInConsole(text); | ||
// alignments for center padding case | ||
const leftPadding = Math.floor((mxColumnLen - curTextSize) / 2); | ||
const rightPadding = mxColumnLen - leftPadding - curTextSize; | ||
switch (alignment) { | ||
case 'left': | ||
return text.concat(' '.repeat(mxColumnLen - curTextSize)); | ||
case 'center': | ||
return ' ' | ||
.repeat(leftPadding) | ||
.concat(text) | ||
.concat(' '.repeat(rightPadding)); | ||
case 'right': | ||
default: | ||
return ' '.repeat(mxColumnLen - curTextSize).concat(text); | ||
} | ||
}; | ||
|
||
// ("How are you?",10) => ["How are ", "you?"] | ||
export const limitWidth = (inpStr: string, width: number): string[] => { | ||
const ret: string[] = []; | ||
|
||
const spaceSeparatedStrings = inpStr.split(' '); | ||
|
||
let now: string[] = []; | ||
let cnt = 0; | ||
spaceSeparatedStrings.forEach((strWithoutSpace) => { | ||
const consoleWidth = findWidthInConsole(strWithoutSpace); | ||
if (cnt + consoleWidth <= width) { | ||
cnt += consoleWidth + 1; // 1 for the space | ||
now.push(strWithoutSpace); | ||
} else { | ||
ret.push(now.join(' ')); | ||
now = [strWithoutSpace]; | ||
cnt = consoleWidth + 1; | ||
} | ||
}); | ||
ret.push(now.join(' ')); | ||
|
||
return ret; | ||
}; | ||
|
||
// ("How are you?",10) => ["How are ", "you?"] | ||
export const biggestWordInSentence = (inpStr: string): number => | ||
inpStr.split(' ').reduce((a, b) => Math.max(a, findWidthInConsole(b)), 0); |
Oops, something went wrong.