Skip to content

Commit 977c15d

Browse files
committed
refactor: use built-in truncation of Listr2
1 parent 44a4f6c commit 977c15d

7 files changed

+14
-39
lines changed

lib/getRenderer.js

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
4040

4141
return {
4242
renderer: 'update',
43+
rendererOptions: {
44+
formatOutput: 'truncate',
45+
},
4346
}
4447
}
4548

lib/makeCmdTasks.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
1-
import cliTruncate from 'cli-truncate'
21
import debug from 'debug'
32

43
import { configurationError } from './messages.js'
54
import { resolveTaskFn } from './resolveTaskFn.js'
65

76
const debugLog = debug('lint-staged:makeCmdTasks')
87

9-
const STDOUT_COLUMNS_DEFAULT = 80
10-
11-
const listrPrefixLength = {
12-
update: ` X `.length, // indented task title where X is a checkmark or a cross (failure)
13-
verbose: `[STARTED] `.length, // verbose renderer uses 7-letter STARTED/SUCCESS prefixes
14-
}
15-
16-
/**
17-
* Get length of title based on the number of available columns prefix length
18-
* @param {string} renderer The name of the Listr renderer
19-
* @returns {number}
20-
*/
21-
const getTitleLength = (renderer, columns = process.stdout.columns) => {
22-
const prefixLength = listrPrefixLength[renderer] || 0
23-
return (columns || STDOUT_COLUMNS_DEFAULT) - prefixLength
24-
}
25-
268
/**
279
* Creates and returns an array of listr tasks which map to the given commands.
2810
*
@@ -31,11 +13,10 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => {
3113
* @param {string} options.cwd
3214
* @param {Array<string>} options.files
3315
* @param {string} options.gitDir
34-
* @param {string} options.renderer
3516
* @param {Boolean} shell
3617
* @param {Boolean} verbose
3718
*/
38-
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => {
19+
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbose }) => {
3920
debugLog('Creating listr tasks for commands %o', commands)
4021
const commandArray = Array.isArray(commands) ? commands : [commands]
4122
const cmdTasks = []
@@ -60,10 +41,8 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, she
6041
)
6142
}
6243

63-
// Truncate title to single line based on renderer
64-
const title = cliTruncate(command, getTitleLength(renderer))
6544
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
66-
cmdTasks.push({ title, command, task })
45+
cmdTasks.push({ title: command, command, task })
6746
}
6847
}
6948

lib/runAll.js

-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ export const runAll = async (
182182
cwd: groupCwd,
183183
files: task.fileList,
184184
gitDir,
185-
renderer: listrOptions.renderer,
186185
shell,
187186
verbose,
188187
}).then((subTasks) => {

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
},
3535
"dependencies": {
3636
"chalk": "5.3.0",
37-
"cli-truncate": "3.1.0",
3837
"commander": "11.0.0",
3938
"debug": "4.3.4",
4039
"execa": "7.2.0",

test/unit/getRenderer.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,29 @@ describe('getRenderer', () => {
3636
expect(getRenderer({}, console, {})).toEqual({
3737
renderer: 'update',
3838
fallbackRenderer: 'verbose',
39+
rendererOptions: {
40+
formatOutput: 'truncate',
41+
},
3942
})
4043
})
4144

4245
it('should return update main renderer and verbose fallback renderer when colors are not forced', () => {
4346
expect(getRenderer({}, console, { FORCE_COLOR: '0' })).toEqual({
4447
renderer: 'update',
4548
fallbackRenderer: 'verbose',
49+
rendererOptions: {
50+
formatOutput: 'truncate',
51+
},
4652
})
4753
})
4854

4955
it('should return update renderers when colors are forced', () => {
5056
expect(getRenderer({}, console, { FORCE_COLOR: '1' })).toEqual({
5157
renderer: 'update',
5258
fallbackRenderer: 'update',
59+
rendererOptions: {
60+
formatOutput: 'truncate',
61+
},
5362
})
5463
})
5564
})

test/unit/makeCmdTasks.spec.js

-13
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,4 @@ describe('makeCmdTasks', () => {
124124
Function task should return a string or an array of strings"
125125
`)
126126
})
127-
128-
it('should truncate task title', async () => {
129-
const longString = new Array(1000)
130-
.fill()
131-
.map((_, index) => index)
132-
.join('')
133-
134-
const res = await makeCmdTasks({ commands: () => longString, gitDir, files: ['test.js'] })
135-
expect(res.length).toBe(1)
136-
expect(res[0].title).toMatchInlineSnapshot(
137-
`"0123456789101112131415161718192021222324252627282930313233343536373839404142434…"`
138-
)
139-
})
140127
})

0 commit comments

Comments
 (0)