Skip to content

Commit ac5b9b3

Browse files
committed
fix: terminal columns, moved limitText to utils
1 parent c59865a commit ac5b9b3

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

src/commands/ni.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ runCli(async (agent, args, ctx) => {
8585
choices: [
8686
{
8787
title: 'prod',
88-
value: null,
88+
value: '',
8989
selected: true,
9090
},
9191
{

src/commands/nr.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import process from 'node:process'
22
import type { Choice } from '@posva/prompts'
33
import prompts from '@posva/prompts'
4-
import c from 'kleur'
54
import { Fzf } from 'fzf'
65
import { dump, load } from '../storage'
76
import { parseNr } from '../parse'
87
import { getPackageJSON } from '../fs'
98
import { runCli } from '../runner'
9+
import { limitText } from '../utils'
1010

1111
runCli(async (agent, args, ctx) => {
1212
const storage = await load()
@@ -44,11 +44,6 @@ runCli(async (agent, args, ctx) => {
4444

4545
const terminalColumns = process.stdout?.columns || 80
4646

47-
function limitText(text: string, maxWidth: number) {
48-
if (text.length <= maxWidth)
49-
return text
50-
return `${text.slice(0, maxWidth)}${c.dim('…')}`
51-
}
5247
const choices: Choice[] = raw
5348
.map(({ key, description }) => ({
5449
title: key,

src/commands/nun.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ runCli(async (agent, args, ctx) => {
6060
name: 'depsToRemove',
6161
choices,
6262
instructions: false,
63-
message: 'remove dependencies',
63+
message: `remove ${isMultiple ? 'dependencies' : 'dependency'}`,
6464
async suggest(input: string, choices: Choice[]) {
6565
const results = fzf.find(input)
6666
return results.map(r => choices.find(c => c.value === r.item[0]))

src/fetch.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process'
22
import type { Choice } from '@posva/prompts'
33
import terminalLink from 'terminal-link'
4+
import { limitText } from './utils'
45

56
export interface NpmPackage {
67
name: string
@@ -23,14 +24,16 @@ export async function fetchNpmPackages(pattern: string): Promise<Choice[]> {
2324
const registryLink = (pattern: string) =>
2425
`https://registry.npmjs.com/-/v1/search?text=${pattern}&size=35`
2526

27+
const terminalColumns = process.stdout?.columns || 80
28+
2629
try {
2730
const result = await fetch(registryLink(pattern))
2831
.then(res => res.json()) as NpmRegistryResponse
2932

3033
return result.objects.map(({ package: pkg }) => ({
3134
title: terminalLink(pkg.name, pkg.links.repository ?? pkg.links.npm),
3235
value: pkg,
33-
description: `${pkg.version} - ${pkg.description}`,
36+
description: limitText(pkg.version, terminalColumns - 20),
3437
}))
3538
}
3639
catch (e) {

src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { existsSync, promises as fs } from 'node:fs'
44
import type { Buffer } from 'node:buffer'
55
import process from 'node:process'
66
import which from 'which'
7+
import c from 'kleur'
78

89
export const CLI_TEMP_DIR = join(os.tmpdir(), 'antfu-ni')
910

@@ -107,3 +108,9 @@ export function invariant(condition: unknown, errorMsg?: string) {
107108
*/
108109
process.exit(0)
109110
}
111+
112+
export function limitText(text: string, maxWidth: number) {
113+
if (text.length <= maxWidth)
114+
return text
115+
return `${text.slice(0, maxWidth)}${c.dim('…')}`
116+
}

0 commit comments

Comments
 (0)