Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace picocolors with ansis #160

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
"@types/yargs": "^17.0.33",
"ansis": "^3.12.0",
"bumpp": "^10.0.3",
"cli-progress": "^3.12.0",
"cross-env": "^7.0.3",
Expand All @@ -67,7 +68,6 @@
"npm-package-arg": "^12.0.2",
"npm-registry-fetch": "^18.0.2",
"p-limit": "^6.2.0",
"picocolors": "^1.1.1",
"prompts": "^2.4.2",
"restore-cursor": "^5.1.0",
"rimraf": "^6.0.1",
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Argv } from 'yargs'
import type { CommonOptions } from './types'
import process from 'node:process'
import c from 'picocolors'
import c from 'ansis'
import restoreCursor from 'restore-cursor'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
Expand Down
6 changes: 3 additions & 3 deletions src/commands/check/checkGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CheckOptions, GlobalPackageMeta, RawDep } from '../../types'
/* eslint-disable no-console */
import { getCommand } from '@antfu/ni'
import c from 'picocolors'
import c from 'ansis'
import prompts from 'prompts'
import { exec } from 'tinyexec'
import { dumpDependencies } from '../../io/dependencies'
Expand Down Expand Up @@ -145,7 +145,7 @@ async function loadGlobalPnpmPackage(options: CheckOptions): Promise<GlobalPacka
filepath: '',
relative: '',
deps,
name: c.red('pnpm') + c.gray(c.dim(' (global)')) + c.gray(c.dim(` ${pnpmOuts[i].path}`)),
name: c.red`pnpm` + c.gray.dim` (global) ` + c.gray.dim(pnpmOuts[i].path),
}))

return pkgMetas
Expand Down Expand Up @@ -175,7 +175,7 @@ async function loadGlobalNpmPackage(options: CheckOptions): Promise<GlobalPackag
filepath: '',
relative: '',
deps,
name: c.red('npm') + c.gray(c.dim(' (global)')),
name: c.red`npm` + c.gray.dim` (global)`,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
PackageMeta,
} from '../../types'
import { detect, parseNi, parseNu, run } from '@antfu/ni'
import c from 'picocolors'
import c from 'ansis'
import prompts from 'prompts'
import { builtinAddons } from '../../addons'
import { CheckPackages } from '../../api/check'
Expand Down Expand Up @@ -128,7 +128,7 @@ export async function check(options: CheckOptions) {
if (!options.install && !options.update && !options.interactive) {
packageManager = await detect()
console.log(
c.yellow(`ℹ changes written to package.json, run ${c.cyan(`${packageManager} i`)} to install updates.`),
c.yellow`ℹ changes written to package.json, run ${c.cyan`${packageManager} i`} to install updates.`,
)
}

Expand Down
12 changes: 6 additions & 6 deletions src/commands/check/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import process from 'node:process'
/* eslint-disable no-console */
import readline from 'node:readline'
import { createControlledPromise, notNullish } from '@antfu/utils'
import c from 'picocolors'
import c from 'ansis'
import { getVersionOfRange, updateTargetVersion } from '../../io/resolves'
import { colorizeVersionDiff, createSliceRender, FIG_BLOCK, FIG_NO_POINTER, FIG_POINTER, formatTable } from '../../render'
import { sortDepChanges } from '../../utils/sort'
Expand Down Expand Up @@ -81,10 +81,10 @@ export async function promptInteractive(pkgs: PackageMeta[], options: CheckOptio
return {
render() {
const sr = createSliceRender()
const Y = (v: string) => c.bold(c.green(v))
const Y = (v: string) => c.bold.green(v)
console.clear()
sr.push({ content: `${FIG_BLOCK} ${c.gray(`${Y('↑↓')} to select, ${Y('space')} to toggle, ${Y('→')} to change version`)}`, fixed: true })
sr.push({ content: `${FIG_BLOCK} ${c.gray(`${Y('enter')} to confirm, ${Y('esc')} to cancel, ${Y('a')} to select/unselect all`)}`, fixed: true })
sr.push({ content: `${FIG_BLOCK} ${c.gray`${Y('↑↓')} to select, ${Y('space')} to toggle, ${Y('→')} to change version`}`, fixed: true })
sr.push({ content: `${FIG_BLOCK} ${c.gray`${Y('enter')} to confirm, ${Y('esc')} to cancel, ${Y('a')} to select/unselect all`}`, fixed: true })
sr.push({ content: '', fixed: true })

pkgs.forEach((pkg) => {
Expand Down Expand Up @@ -165,15 +165,15 @@ export async function promptInteractive(pkgs: PackageMeta[], options: CheckOptio
return {
render() {
console.clear()
console.log(`${FIG_BLOCK} ${c.gray(`Select a version for ${c.green(c.bold(dep.name))}${c.gray(` (current ${dep.currentVersion})`)}`)}`)
console.log(`${FIG_BLOCK} ${c.gray`Select a version for ${c.green.bold(dep.name)}${c.gray` (current ${dep.currentVersion})`}`}`)
console.log()
console.log(
formatTable(versions.map((v, idx) => {
return [
(index === idx ? FIG_POINTER : FIG_NO_POINTER) + (index === idx ? v.name : c.gray(v.name)),
timediff ? timeDifference(dep.currentVersionTime) : '',
c.gray(dep.currentVersion),
c.dim(c.gray('→')),
c.dim.gray('→'),
colorizeVersionDiff(dep.currentVersion, v.targetVersion),
timediff ? timeDifference(v.time) : '',
]
Expand Down
16 changes: 8 additions & 8 deletions src/commands/check/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
PackageMeta,
ResolvedDepChange,
} from '../../types'
import c from 'picocolors'
import c from 'ansis'
import semver from 'semver'
import { colorizeVersionDiff, FIG_CHECK, FIG_NO_POINTER, FIG_POINTER, FIG_UNCHECK, formatTable } from '../../render'
import { DependenciesTypeShortMap } from '../../types'
Expand All @@ -30,22 +30,22 @@ export function renderChange(

let name = change.name
if (change.aliasName)
name = c.dim(`${change.aliasName} ← `) + change.name
name = c.dim`${change.aliasName} ← ` + change.name

return [
`${pre} ${update ? name : c.gray(name)}`,
grouped ? '' : c.gray(DependenciesTypeShortMap[change.source]),
timediff ? timeDifference(change.currentVersionTime) : '',
c.gray(change.currentVersion),
update ? c.dim(c.gray('→')) : '',
update ? c.dim.gray('→') : '',
update
? colorizeVersionDiff(change.currentVersion, change.targetVersion)
: c.gray(c.strikethrough(change.targetVersion)),
: c.gray.strikethrough(change.targetVersion),
update && timediff
? timeDifference(change.targetVersionTime)
: '',
(change.latestVersionAvailable && semver.minVersion(change.targetVersion)!.toString() !== change.latestVersionAvailable)
? c.dim(c.magenta(`(${change.latestVersionAvailable} available)`))
? c.dim.magenta`(${change.latestVersionAvailable} available)`
: '',
]
}
Expand Down Expand Up @@ -159,20 +159,20 @@ function renderResolveError(dep: ResolvedDepChange) {
return lines

if (dep.resolveError === '404') {
lines.push(c.red(`> ${c.underline(dep.name)} not found`))
lines.push(c.red`> ${c.underline(dep.name)} not found`)
}
else if (dep.resolveError === 'invalid_range') {
// lines.push(c.yellow(`> ${c.underline(dep.name)} has an unresolvable version range: ${c.underline(dep.currentVersion)}`))
}
else {
lines.push(c.red(`> ${c.underline(dep.name)} unknown error`))
lines.push(c.red`> ${c.underline(dep.name)} unknown error`)
lines.push(c.red(dep.resolveError.toString()))
}
return lines
}

export function outputErr(errLines: string[]) {
console.error(c.inverse(c.red(c.bold(' ERROR '))))
console.error(c.inverse.red.bold` ERROR `)
console.error()
console.error(errLines.join('\n'))
console.error()
Expand Down
6 changes: 3 additions & 3 deletions src/commands/usage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SingleBar } from 'cli-progress'
import type { UsageOptions } from '../types'
import c from 'picocolors'
import c from 'ansis'
import { CheckUsages } from '../api/usage'
import { createMultiProgressBar, TableLogger, wrapJoin } from '../log'
import { colorizeVersionDiff, visualPadStart } from '../render'
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function usage(options: UsageOptions) {
if (options.detail) {
logger.log()
logger.row(
`${c.green(name)} ${c.gray(`· ${versions.length} versions · latest: ${c.blue(latest)}`)}`,
`${c.green(name)} ${c.gray`· ${versions.length} versions · latest: ${c.blue(latest)}`}`,
)
const pad = Math.max(8, ...Object.keys(versionMap).map(i => i.length)) + 2

Expand All @@ -62,7 +62,7 @@ export async function usage(options: UsageOptions) {
else {
logger.row(
c.green(name),
c.gray(`${c.cyan(packagesCount.toString())} in use / ${c[color](versions.length.toString())} versions`),
c.gray`${c.cyan(packagesCount.toString())} in use / ${c[color](versions.length.toString())} versions`,
versions.map(v => c.gray(colorizeVersionDiff(latest || v, v, false))).join(c.gray(', ')),
c.gray('→'),
latest,
Expand Down
2 changes: 1 addition & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LogLevel } from './types'
import process from 'node:process'
import c from 'ansis'
import { MultiBar, Presets } from 'cli-progress'
import c from 'picocolors'
import { LOG_LEVELS } from './constants'
import { visualLength, visualPadEnd, visualPadStart } from './render'

Expand Down
4 changes: 2 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* eslint-disable no-console */

import process from 'node:process'
import c from 'picocolors'
import c from 'ansis'
import { SemVer } from 'semver'
import { getDiff } from './io/resolves'
import { DiffColorMap } from './utils/diff'
Expand All @@ -15,7 +15,7 @@ export const FIG_CHECK = c.green('◉')
export const FIG_UNCHECK = c.gray('◌')
export const FIG_POINTER = c.cyan('❯ ')
export const FIG_NO_POINTER = ' '
export const FIG_BLOCK = c.bold(c.dim(c.gray('┃')))
export const FIG_BLOCK = c.bold.dim.gray('┃')

function ansiRegex({ onlyFirst = false } = {}) {
const pattern = [
Expand Down
8 changes: 4 additions & 4 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import c from 'picocolors'
import c from 'ansis'

const msPerMinute = 60 * 1000
const msPerHour = msPerMinute * 60
Expand All @@ -23,11 +23,11 @@ export function timeDifference(from?: number | string, to = +new Date()) {
return c.gray('⩽1d')

else if (elapsed < msPerMonth)
return c.green(`~${Math.round(elapsed / msPerDay)}d`)
return c.green`~${Math.round(elapsed / msPerDay)}d`

else if (elapsed < msPerYear)
return c.yellow(`~${Math.round(elapsed / msPerMonth)}mo`)
return c.yellow`~${Math.round(elapsed / msPerMonth)}mo`

else
return c.red(`~${+(elapsed / msPerYear).toFixed(1)}y`)
return c.red`~${+(elapsed / msPerYear).toFixed(1)}y`
}
Loading