-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27ed208
commit 1c65dfa
Showing
3 changed files
with
66 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
declare namespace cliTruncate { | ||
interface Options { | ||
/** | ||
Position to truncate the string. | ||
@default 'end' | ||
*/ | ||
readonly position?: 'start' | 'middle' | 'end'; | ||
} | ||
} | ||
|
||
/** | ||
Truncate a string to a specific width in the terminal. | ||
@param input - Text to truncate. | ||
@param columns - Columns to occupy in the terminal. | ||
@example | ||
``` | ||
import cliTruncate = require('cli-truncate'); | ||
cliTruncate('unicorn', 4); | ||
//=> 'uni…' | ||
// Truncate at different positions | ||
cliTruncate('unicorn', 4, {position: 'start'}); | ||
//=> '…orn' | ||
cliTruncate('unicorn', 4, {position: 'middle'}); | ||
//=> 'un…n' | ||
cliTruncate('\u001B[31municorn\u001B[39m', 4); | ||
//=> '\u001B[31muni\u001B[39m…' | ||
// Truncate Unicode surrogate pairs | ||
cliTruncate('uni\uD83C\uDE00corn', 5); | ||
//=> 'uni\uD83C\uDE00…' | ||
// Truncate fullwidth characters | ||
cliTruncate('안녕하세요', 3); | ||
//=> '안…' | ||
// Truncate the paragraph to the terminal width | ||
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.'; | ||
cliTruncate(paragraph, process.stdout.columns)); | ||
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…' | ||
``` | ||
*/ | ||
declare function cliTruncate( | ||
input: string, | ||
columns: number, | ||
options?: cliTruncate.Options | ||
): string; | ||
|
||
export = cliTruncate; |
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,7 @@ | ||
import {expectType} from 'tsd'; | ||
import cliTruncate = require('.'); | ||
|
||
expectType<string>(cliTruncate('unicorn', 4)); | ||
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'})); | ||
expectType<string>(cliTruncate('unicorn', 4, {position: 'middle'})); | ||
expectType<string>(cliTruncate('unicorn', 4, {position: 'end'})); |
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