Ultimate Guide to CLI-Box Creating Stunning Command Line Interfaces Made Simple

Introduction to CLI-Box: Building Stylish Command Line Interfaces

CLI-Box is a powerful Node.js library for creating aesthetically pleasing command-line interface (CLI) boxes. It provides numerous APIs to customize and stylize the content within the boxes. Whether it’s for headers, notifications, or logs, CLI-Box has got you covered. Let’s dive into the library with dozens of useful API examples and an app leveraging all those features.

Basic Usage

  const CLIBox = require('cli-box');
  const box = new CLIBox('50x10');
  console.log(box.toString());

Advanced Usage

Create a box with custom borders and padding:

  const box = new CLIBox({
    w: 60,
    h: 20,
    marks: {
      nw: '+',
      n: '-',
      ne: '+',
      e: '|',
      se: '+',
      s: '-',
      sw: '+',
      w: '|'
    },
    stringify: true
  });
  
  console.log(box.toString());

Text Alignment

Center align text within a box:

  const box = new CLIBox({
    w: 40,
    h: 5,
    mark: 'center'
  }, 'Hello World');
  
  console.log(box.stringify());

Dynamic Content

Add dynamic content with variables:

  const content = 'Dynamic Content';
  const box = new CLIBox({
    w: 40,
    h: 7,
  }, `This box contains: ${content}`);
  
  console.log(box.stringify());

Complex Layouts

Create nested boxes for complex layouts:

  const outerBox = new CLIBox('60x20');
  const innerBox = new CLIBox({w: 30, h: 10, marks: {nw: '*', n: '*', ne: '*'}});
  
  const content = innerBox.stringify();
  const finalBox = new CLIBox({w: 60, h: 20, hAlign: 'center', vAlign: 'middle'}, content);
  
  console.log(finalBox.stringify());

Real-World Application

An example application logging user activities:

  const CLIBox = require('cli-box');

  function logActivity(activity) {
    const activityBox = new CLIBox({
      w: 50,
      h: 3,
      marks: {
        nw: '/',
        n: '-',
        ne: '\\',
        e: '|',
        se: '/',
        s: '-',
        sw: '\\',
        w: '|'
      }
    }, activity);

    console.log(activityBox.stringify());
  }

  logActivity('User logged in');
  logActivity('User viewed dashboard');
  logActivity('User logged out');

In this example, CLI-Box is used to log activities with well-formatted boxes, making it easier to track and read through command-line logs.

Hash: 4718fe54dad50840563be812609a8eebb8a5068babf147b5a9b19cad69829a63

Leave a Reply

Your email address will not be published. Required fields are marked *