UNPKG

aahook

Version:

A CLI tool that displays ASCII art when commands succeed or fail

195 lines 7.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.animateCommand = animateCommand; exports.listAnimations = listAnimations; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const animation_engine_1 = require("../animation/animation-engine"); const color_engine_1 = require("../color/color-engine"); const aa_manager_1 = require("../aa-manager"); const config_1 = require("../config"); /** * Animate command - Apply animations to ASCII arts */ async function animateCommand(artName, options = {}) { try { // Load art content using AAManager const manager = new aa_manager_1.AAManager(); let content; try { const art = await manager.getLocal(artName); if (!art) { throw new Error(`Art not found: ${artName}`); } content = art.content; } catch (error) { console.error(`Art file not found: ${artName}`); console.log('Use "npx aahook gallery" to see available arts'); process.exit(1); } // Apply color theme if specified if (options.theme) { const colorEngine = new color_engine_1.ColorEngine(); const themes = await colorEngine.listThemes(); if (!themes.includes(options.theme)) { console.error(`Theme not found: ${options.theme}`); console.log(`Available themes: ${themes.join(', ')}`); process.exit(1); } // Load the theme first, then apply it const theme = await colorEngine.loadTheme(options.theme); content = colorEngine.applyTheme(content, theme); } // Prepare animation options const animationOptions = { timing: { fps: 30, loop: typeof options.loop === 'string' ? -1 : (options.loop || 1), charDelay: options.speed ? 1000 / options.speed : 20, // Faster default speed lineDelay: options.speed ? 1000 / options.speed : 50, // Faster default speed }, effects: { direction: options.direction || 'top', }, preview: options.preview, theme: options.theme, }; // Create animation engine const engine = new animation_engine_1.AnimationEngine(animationOptions); // Determine animation type const animationType = options.type || 'typing'; // Handle frame animation differently if (animationType === 'frames') { const frames = await loadFrames(artName); if (frames.length === 0) { console.error('No frames found for frame animation'); process.exit(1); } await engine.animate(frames, 'frames'); } else { await engine.animate(content, animationType); } // Save animation definition if requested if (options.save) { await saveAnimationDefinition(artName, options); console.log(`\nAnimation saved for ${artName}`); } } catch (error) { console.error('Animation failed:', error); process.exit(1); } } /** * List available animations */ async function listAnimations() { const animationsPath = path.join((0, config_1.getConfigDir)(), 'animations'); try { const files = await fs.readdir(animationsPath); const animations = files.filter(f => f.endsWith('.json')); if (animations.length === 0) { console.log('No saved animations found'); return; } console.log('Available animations:'); for (const anim of animations) { const name = anim.replace('.json', ''); const definition = await loadAnimationDefinition(name); console.log(` - ${name} (${definition.type})`); } } catch (error) { console.log('No animations directory found'); } } /** * Load frames for frame animation */ async function loadFrames(baseName) { const frames = []; const artsDir = path.join((0, config_1.getConfigDir)(), 'arts'); // Look for numbered frame files (e.g., cat-1.txt, cat-2.txt) let frameNumber = 1; while (frameNumber <= 100) { // Max 100 frames const framePath = path.join(artsDir, `${baseName}-${frameNumber}.txt`); try { const content = await fs.readFile(framePath, 'utf-8'); frames.push(content); frameNumber++; } catch { break; } } return frames; } /** * Save animation definition */ async function saveAnimationDefinition(artName, options) { const animationsPath = path.join((0, config_1.getConfigDir)(), 'animations'); // Create animations directory if it doesn't exist await fs.mkdir(animationsPath, { recursive: true }); const definition = { name: artName, version: '1.0.0', type: options.type || 'typing', fps: 30, loop: options.loop || 1, effects: { direction: options.direction, }, timing: { speed: options.speed, }, theme: options.theme, }; const definitionPath = path.join(animationsPath, `${artName}.json`); await fs.writeFile(definitionPath, JSON.stringify(definition, null, 2)); } /** * Load animation definition */ async function loadAnimationDefinition(name) { const animationsPath = path.join((0, config_1.getConfigDir)(), 'animations'); const definitionPath = path.join(animationsPath, `${name}.json`); const content = await fs.readFile(definitionPath, 'utf-8'); return JSON.parse(content); } //# sourceMappingURL=animate.js.map