ns Docsns Docs
Guides

Advanced Usage

Advanced patterns and techniques for using nstypocolors in complex applications and CLIs.

This guide explores advanced usage patterns for nstypocolors when building larger applications, production-grade CLIs, or developer-focused tools.
The goal is clarity, control, and intentional use of color.

Conditional Logging


In real-world applications, colored output should often be opt-in.

import { logInfo } from 'nstypocolors'

const isVerbose = process.argv.includes('--verbose')

if (isVerbose) {
  logInfo('Verbose mode enabled')
}
const { logInfo } = require('nstypocolors')

const isVerbose = process.argv.includes('--verbose')

if (isVerbose) {
  logInfo('Verbose mode enabled')
}

This keeps default output clean while still allowing richer feedback when needed.

Prefer explicit flags like --verbose or --debug over always-on colored logs.

Environment-Based Logging


Different environments often require different logging behavior.

import { logBuild } from 'nstypocolors'

if (process.env.NODE_ENV === 'development') {
  logBuild('Running in development mode')
}
const { logBuild } = require('nstypocolors')

if (process.env.NODE_ENV === 'development') {
  logBuild('Running in development mode')
}

Avoid excessive gradients in production unless logs are user-facing.

Using Gradients as Section Headers


Gradients are most effective as visual separators, not continuous output.

logBuild('Starting Deployment')
console.log('Checking environment...')
console.log('Uploading assets...')
logBuild('Starting Deployment')
console.log('Checking environment...')
console.log('Uploading assets...')

This pattern significantly improves readability in long-running tasks.

Overusing gradients reduces their impact. Use them to mark transitions, not every step.

Layered Output Strategy


A recommended logging hierarchy for larger tools:

  1. Gradient logs → High-level state changes
  2. Plain logs → Details and progress
  3. Error logs → Immediate attention
logBuild('Initializing')
console.log('Loading configuration')
logSuccess('Initialization complete')
logBuild('Initializing')
console.log('Loading configuration')
logSuccess('Initialization complete')

This balance keeps output informative without becoming noisy.

Graceful Failures


Use colored logs before exiting, but avoid unnecessary stack traces.

if (!token) {
  logError('Missing authentication token')
  process.exit(1)
}
if (!token) {
  logError('Missing authentication token')
  process.exit(1)
}

Clear, human-readable messages are more valuable than verbose diagnostics.

Small Messages & Edge Cases


nstypocolors safely handles very short messages, but intent still matters.

logSuccess('OK')
logWarn('!')
logSuccess('OK')
logWarn('!')

Short messages work best for confirmations or alerts—not explanations.

Using nstypocolors in Libraries


If you’re building a library for others to consume:

  • Avoid logging by default
  • Expose an option to enable colored output
  • Respect user preferences
export function run({ silent = false }) {
  if (!silent) {
    logInfo('Running task')
  }
}
module.exports.run = function ({ silent = false }) {
  if (!silent) {
    logInfo('Running task')
  }
}
Libraries should empower users to control logging, not force it.

CLI Flags Integration


Combine nstypocolors with common CLI flags:

  • --quiet
  • --verbose
  • --no-color
if (flags.noColor) {
  console.log('Operation completed')
} else {
  logSuccess('Operation completed')
}
if (flags.noColor) {
  console.log('Operation completed')
} else {
  logSuccess('Operation completed')
}

Always provide a plain-text fallback.

Cross-Platform Considerations


nstypocolors uses ANSI escape codes, which work in most modern terminals.

Best practices:

  • Test on Windows Terminal
  • Avoid assuming fixed-width fonts
  • Don’t rely on color alone to convey meaning

Pair color with clear wording every time.

Future-Proofing Your Logs


Design logs so they still make sense when:

  • Colors are disabled
  • Output is redirected to a file
  • The terminal doesn’t support gradients

Well-written text matters more than styling.

Final Thoughts


Advanced usage of nstypocolors is about restraint and intention.

Use gradients to mark moments. Use color to communicate state. Let content do the rest.

Last updated on

On this page