A Comprehensive Guide to CLUI A Versatile Command Line User Interface Library

Introduction to CLUI

CLUI (Command Line User Interface) offers a powerful means to create interactive and user-friendly command line applications. With an array of APIs at your disposal, CLUI makes building CLI tools easier and more efficient. From basic input methods to complex wizard-like experiences, CLUI has it all. Here, we introduce some of the most useful CLUI APIs with code snippets and a sample application.

Useful API Examples

1. Basic Input

Prompt users for simple input.

  const { input } = require('clui');
  const name = input('What is your name? ');
  console.log(`Hello, ${name}!`);

2. Confirmation

Ask for a yes/no confirmation.

  const { confirm } = require('clui');
  const answer = confirm('Do you want to proceed?');
  if (answer) {
    console.log('Proceeding...');
  } else {
    console.log('Operation cancelled.');
  }

3. Select Menu

Create a selection menu.

  const { select } = require('clui');
  const choice = select('Choose an option:', ['Option 1', 'Option 2', 'Option 3']);
  console.log(`You chose ${choice}`);

4. Multi-select Menu

Allow the user to select multiple options.

  const { multiselect } = require('clui');
  const choices = multiselect('Select options:', ['Option 1', 'Option 2', 'Option 3']);
  console.log(`You selected ${choices.join(', ')}`);

5. Password Input

Securely prompt for passwords.

  const { password } = require('clui');
  const pass = password('Enter your password: ');
  console.log('Password collected securely.');

6. Spinner

Show a loading spinner while performing an asynchronous task.

  const { spinner } = require('clui');
  const spin = spinner('Loading...');
  setTimeout(() => {
    spin.stop();
    console.log('Loading complete.');
  }, 3000);

7. Progress Bar

Display a progress bar for long-running operations.

  const { progress } = require('clui');
  const bar = progress(100);
  let completed = 0;

  const interval = setInterval(() => {
    completed += 10;
    bar.update(completed);

    if (completed >= 100) {
      clearInterval(interval);
      console.log('Operation completed.');
    }
  }, 500);

Example Application

Combining the above APIs into a single application.

  const { input, confirm, select, multiselect, password, spinner, progress } = require('clui');

  async function runApp() {
    const name = input('What is your name? ');
    console.log(`Hello, ${name}!`);

    const proceed = confirm('Do you want to set up your profile?');
    if (!proceed) {
      console.log('Setup cancelled.');
      return;
    }

    const setupSpin = spinner('Setting up your profile...');
    setTimeout(() => {
      setupSpin.stop();
      console.log('Profile setup complete.');

      const profileChoice = select('Choose your profile type:', ['Basic', 'Advanced', 'Custom']);
      console.log(`Profile type: ${profileChoice}`);

      const extraChoices = multiselect('Select additional features:', ['Feature 1', 'Feature 2', 'Feature 3']);
      console.log(`Extras: ${extraChoices.join(', ')}`);

      const userPassword = password('Enter a secure password: ');
      console.log('Password saved securely.');

      const progressBar = progress(100);
      let progressCount = 0;
      const setupInterval = setInterval(() => {
        progressCount += 20;
        progressBar.update(progressCount);

        if (progressCount >= 100) {
          clearInterval(setupInterval);
          console.log('Profile setup process finished.');
        }
      }, 1000);
    }, 2000);
  }

  runApp();

With these examples, you can create interactive and user-friendly command line applications using the powerful APIs provided by CLUI. Happy coding!

Hash: 747bd36554735fa1005f663987137d9233fbf4270096bcd834d6eccacac5e143

Leave a Reply

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