Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/cli-truncate
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2f422c146701b9e76ac93b2f88753a915be37301
Choose a base ref
...
head repository: sindresorhus/cli-truncate
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 42f602d1af13387515c8d6b7f02915c7021c0544
Choose a head ref
  • 3 commits
  • 9 files changed
  • 2 contributors

Commits on Jan 1, 2021

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    addaleax Anna Henningsen
    Copy the full SHA
    0c2e152 View commit details

Commits on Aug 10, 2021

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    addaleax Anna Henningsen
    Copy the full SHA
    38ea95c View commit details
  2. 3.0.0

    sindresorhus committed Aug 10, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    addaleax Anna Henningsen
    Copy the full SHA
    42f602d View commit details
Showing with 104 additions and 85 deletions.
  1. +20 −0 .github/workflows/main.yml
  2. +0 −5 .travis.yml
  3. +44 −44 index.d.ts
  4. +19 −20 index.js
  5. +1 −1 index.test-d.ts
  6. +1 −1 license
  7. +10 −8 package.json
  8. +8 −5 readme.md
  9. +1 −1 test.js
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

88 changes: 44 additions & 44 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
declare namespace cliTruncate {
interface Options {
/**
Position to truncate the string.
export interface Options {
/**
The position to truncate the string.
@default 'end'
*/
readonly position?: 'start' | 'middle' | 'end';
@default 'end'
*/
readonly position?: 'start' | 'middle' | 'end';

/**
Add a space between the text and the ellipsis.
/**
Add a space between the text and the ellipsis.
@default false
@default false
@example
```
cliTruncate('unicorns', 5, {position: 'end', space: true});
//=> 'uni …'
@example
```
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns', 5, {position: 'end', space: false});
//=> 'unic…'
cliTruncate('unicorns', 5, {position: 'end', space: true});
//=> 'uni …'
cliTruncate('unicorns', 6, {position: 'start', space: true});
//=> '… orns'
cliTruncate('unicorns', 5, {position: 'end', space: false});
//=> 'unic…'
cliTruncate('unicorns', 7, {position: 'middle', space: true});
//=> 'uni … s'
```
*/
readonly space?: boolean;
cliTruncate('unicorns', 6, {position: 'start', space: true});
//=> '… orns'
/**
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
cliTruncate('unicorns', 7, {position: 'middle', space: true});
//=> 'uni … s'
```
*/
readonly space?: boolean;

@default false
/**
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
@example
```
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
//=> '…rainbow dragons'
@default false
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
//=> 'unicorns…dragons'
@example
```
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
//=> 'unico…'
````
*/
readonly preferTruncationOnSpace?: boolean;
}
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
//=> '…rainbow dragons'
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
//=> 'unicorns…dragons'
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
//=> 'unico…'
````
*/
readonly preferTruncationOnSpace?: boolean;
}

/**
Truncate a string to a specific width in the terminal.
@param text - Text to truncate.
@param columns - Columns to occupy in the terminal.
@param columns - The number of columns to occupy in the terminal.
@example
```
import cliTruncate = require('cli-truncate');
import cliTruncate from 'cli-truncate';
cliTruncate('unicorn', 4);
//=> 'uni…'
@@ -87,10 +89,8 @@ cliTruncate(paragraph, process.stdout.columns));
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
```
*/
declare function cliTruncate(
export default function cliTruncate(
text: string,
columns: number,
options?: cliTruncate.Options
options?: Options
): string;

export = cliTruncate;
39 changes: 19 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
'use strict';
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');
import sliceAnsi from 'slice-ansi';
import stringWidth from 'string-width';

function getIndexOfNearestSpace(string, index, shouldSearchRight) {
if (string.charAt(index) === ' ') {
return index;
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
if (string.charAt(wantedIndex) === ' ') {
return wantedIndex;
}

for (let i = 1; i <= 3; i++) {
for (let index = 1; index <= 3; index++) {
if (shouldSearchRight) {
if (string.charAt(index + i) === ' ') {
return index + i;
if (string.charAt(wantedIndex + index) === ' ') {
return wantedIndex + index;
}
} else if (string.charAt(index - i) === ' ') {
return index - i;
} else if (string.charAt(wantedIndex - index) === ' ') {
return wantedIndex - index;
}
}

return index;
return wantedIndex;
}

module.exports = (text, columns, options) => {
export default function cliTruncate(text, columns, options) {
options = {
position: 'end',
preferTruncationOnSpace: false,
...options
...options,
};

const {position, space, preferTruncationOnSpace} = options;
@@ -69,7 +68,7 @@ module.exports = (text, columns, options) => {

if (position === 'middle') {
if (space === true) {
ellipsis = ' ' + ellipsis + ' ';
ellipsis = ` ${ellipsis} `;
ellipsisWidth = 3;
}

@@ -82,9 +81,9 @@ module.exports = (text, columns, options) => {
}

return (
sliceAnsi(text, 0, half) +
ellipsis +
sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
sliceAnsi(text, 0, half)
+ ellipsis
+ sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
);
}

@@ -95,12 +94,12 @@ module.exports = (text, columns, options) => {
}

if (space === true) {
ellipsis = ' ' + ellipsis;
ellipsis = ` ${ellipsis}`;
ellipsisWidth = 2;
}

return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
}

throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import cliTruncate = require('.');
import cliTruncate from './index.js';

expectType<string>(cliTruncate('unicorn', 4));
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "cli-truncate",
"version": "2.1.0",
"version": "3.0.0",
"description": "Truncate a string to a specific width in the terminal",
"license": "MIT",
"repository": "sindresorhus/cli-truncate",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -35,12 +37,12 @@
"string"
],
"dependencies": {
"slice-ansi": "^3.0.0",
"string-width": "^4.2.0"
"slice-ansi": "^5.0.0",
"string-width": "^5.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
13 changes: 8 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cli-truncate [![Build Status](https://travis-ci.org/sindresorhus/cli-truncate.svg?branch=master)](https://travis-ci.org/sindresorhus/cli-truncate)
# cli-truncate

> Truncate a string to a specific width in the terminal
@@ -13,7 +13,7 @@ $ npm install cli-truncate
## Usage

```js
const cliTruncate = require('cli-truncate');
import cliTruncate from 'cli-truncate';

cliTruncate('unicorn', 4);
//=> 'uni…'
@@ -59,7 +59,7 @@ Text to truncate.

Type: `number`

Columns to occupy in the terminal.
The number of columns to occupy in the terminal.

#### options

@@ -71,7 +71,7 @@ Type: `string`\
Default: `'end'`\
Values: `'start'` `'middle'` `'end'`

Position to truncate the string.
The position to truncate the string.

##### space

@@ -81,6 +81,8 @@ Default: `false`
Add a space between the text and the ellipsis.

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorns', 5, {space: false});
//=> 'unic…'

@@ -102,6 +104,8 @@ Default: `false`
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
//=> '…rainbow dragons'

@@ -125,7 +129,6 @@ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncation
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes


---

<div align="center">
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import cliTruncate from '.';
import cliTruncate from './index.js';

test('main', t => {
t.is(cliTruncate('unicorn', 4), 'uni…');