or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-api.mdcustom-prompts.mdform-survey-prompts.mdindex.mdselection-prompts.mdspecialized-prompts.mdtext-prompts.md
tile.json

form-survey-prompts.mddocs/

Form and Survey Prompts

Form and survey prompts provide advanced data collection capabilities for gathering structured information through multi-field forms, rating scales, and comprehensive surveys.

Capabilities

Form Prompt

Multi-field form prompt that collects multiple related values in a single interactive interface.

/**
 * Multi-field form prompt
 * @param options - Form prompt options
 * @returns Promise resolving to object with form field values
 */
function form(options);

interface FormOptions extends BasePromptOptions {
  type: 'form';
  choices: FormField[];
  align?: 'left' | 'right';
  margin?: number;
  validate?: (value: object) => boolean | string | Promise<boolean | string>;
}

interface FormField {
  name: string;
  message: string;
  initial?: string;
  hint?: string;
  validate?: (value: string, state: object) => boolean | string | Promise<boolean | string>;
}

Usage Examples:

const { prompt } = require('enquirer');

// User registration form
const response = await prompt({
  type: 'form',
  name: 'user',
  message: 'User Registration',
  choices: [
    { name: 'username', message: 'Username', initial: 'guest' },
    { name: 'email', message: 'Email' },
    { name: 'firstName', message: 'First Name' },
    { name: 'lastName', message: 'Last Name' }
  ]
});

// Configuration form with validation
const response = await prompt({
  type: 'form',
  name: 'config',
  message: 'Server Configuration',
  choices: [
    { 
      name: 'host', 
      message: 'Host',
      initial: 'localhost',
      validate: value => value.length > 0 || 'Host is required'
    },
    { 
      name: 'port', 
      message: 'Port',
      initial: '3000',
      validate: value => /^\d+$/.test(value) || 'Port must be a number'
    },
    { 
      name: 'env', 
      message: 'Environment',
      initial: 'development' 
    }
  ],
  validate: (values) => {
    const port = parseInt(values.port);
    return port > 0 && port < 65536 || 'Port must be between 1 and 65535';
  }
});

Survey Prompt

Comprehensive survey prompt for collecting responses to multiple questions with various input types.

/**
 * Multi-question survey prompt
 * @param options - Survey prompt options
 * @returns Promise resolving to object with survey responses
 */
function survey(options);

interface SurveyOptions extends BasePromptOptions {
  type: 'survey';
  choices: SurveyQuestion[];
  margin?: number;
  sort?: boolean;
  validate?: (value: object) => boolean | string | Promise<boolean | string>;
}

interface SurveyQuestion {
  name: string;
  message: string;
  scale?: number[];
  initial?: number;
  margin?: number;
}

Usage Examples:

// Customer satisfaction survey
const response = await prompt({
  type: 'survey',
  name: 'satisfaction',
  message: 'Customer Satisfaction Survey',
  choices: [
    {
      name: 'quality',
      message: 'How would you rate the product quality?'
    },
    {
      name: 'service', 
      message: 'How satisfied are you with our customer service?'
    },
    {
      name: 'recommend',
      message: 'How likely are you to recommend us to others?'
    },
    {
      name: 'overall',
      message: 'Overall satisfaction with your experience?'
    }
  ]
});

// Custom scale survey
const response = await prompt({
  type: 'survey',
  name: 'feedback',
  message: 'Product Feedback (1-10 scale)',
  choices: [
    {
      name: 'usability',
      message: 'Ease of use',
      scale: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      initial: 5
    },
    {
      name: 'design',
      message: 'Visual design',
      scale: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      initial: 5
    }
  ]
});

Scale Prompt

Rating scale prompt for collecting numeric ratings or Likert-scale responses.

/**
 * Rating scale prompt
 * @param options - Scale prompt options
 * @returns Promise resolving to selected scale value
 */
function scale(options);

interface ScaleOptions extends BasePromptOptions {
  type: 'scale';
  scale: number[];
  initial?: number;
  margin?: number;
  indent?: number;
  newline?: string;
  align?: 'left' | 'right';
}

Usage Examples:

// Simple 1-5 rating scale
const response = await prompt({
  type: 'scale',
  name: 'rating',
  message: 'Rate this feature',
  scale: [1, 2, 3, 4, 5],
  initial: 3
});

// Custom scale with labels
const response = await prompt({
  type: 'scale',
  name: 'agreement',
  message: 'I find this tool useful',
  scale: [
    { name: '1', message: 'Strongly Disagree' },
    { name: '2', message: 'Disagree' },
    { name: '3', message: 'Neutral' },
    { name: '4', message: 'Agree' },
    { name: '5', message: 'Strongly Agree' }
  ],
  initial: 3
});

// 0-10 likelihood scale
const response = await prompt({
  type: 'scale',
  name: 'likelihood',
  message: 'How likely are you to recommend this product?',
  scale: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  initial: 5,
  margin: 2
});

Numeral Prompt

Numeric input prompt with optional constraints and formatting for collecting numeric data.

/**
 * Numeric input prompt
 * @param options - Numeral prompt options
 * @returns Promise resolving to numeric value
 */
function numeral(options);

interface NumeralOptions extends BasePromptOptions {
  type: 'numeral';
  min?: number;
  max?: number;
  delay?: number;
  float?: boolean;
  round?: boolean;
  major?: number;
  minor?: number;
  initial?: number;
}

Usage Examples:

// Basic numeric input
const response = await prompt({
  type: 'numeral',
  name: 'age',
  message: 'What is your age?',
  initial: 25
});

// Numeric input with constraints
const response = await prompt({
  type: 'numeral', 
  name: 'quantity',
  message: 'Enter quantity (1-100)',
  min: 1,
  max: 100,
  initial: 1
});

// Float input with precision
const response = await prompt({
  type: 'numeral',
  name: 'price',
  message: 'Enter price',
  float: true,
  round: 2,
  initial: 0.00
});

Form Field Validation

Form prompts support field-level and form-level validation:

const response = await prompt({
  type: 'form',
  name: 'registration',
  message: 'Account Registration',
  choices: [
    {
      name: 'username',
      message: 'Username',
      validate: async (value) => {
        if (value.length < 3) return 'Username must be at least 3 characters';
        // Could check availability here
        return true;
      }
    },
    {
      name: 'password',
      message: 'Password',
      validate: (value) => {
        const hasUpper = /[A-Z]/.test(value);
        const hasLower = /[a-z]/.test(value);
        const hasNumber = /\d/.test(value);
        const hasSpecial = /[!@#$%^&*]/.test(value);
        
        if (!hasUpper || !hasLower || !hasNumber || !hasSpecial) {
          return 'Password must contain uppercase, lowercase, number, and special character';
        }
        return true;
      }
    },
    {
      name: 'email',
      message: 'Email',
      validate: (value) => {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(value) || 'Please enter a valid email address';
      }
    }
  ],
  validate: (values) => {
    // Form-level validation
    if (values.username === values.password) {
      return 'Username and password cannot be the same';
    }
    return true;
  }
});

Survey Response Processing

Survey prompts return structured data that can be easily processed:

const surveyResult = await prompt({
  type: 'survey',
  name: 'feedback',
  message: 'Please rate the following',
  choices: [
    { name: 'quality', message: 'Product Quality' },
    { name: 'service', message: 'Customer Service' },
    { name: 'value', message: 'Value for Money' }
  ]
});

// Process survey results
const averageRating = Object.values(surveyResult.feedback)
  .reduce((sum, rating) => sum + rating, 0) / 3;

console.log(`Average rating: ${averageRating.toFixed(1)}`);

// Individual ratings
console.log(`Quality: ${surveyResult.feedback.quality}`);
console.log(`Service: ${surveyResult.feedback.service}`);
console.log(`Value: ${surveyResult.feedback.value}`);