filetree-pro
Version:
A powerful file tree generator for VS Code and Cursor. Generate beautiful file trees in multiple formats with smart exclusions and custom configurations.
86 lines (72 loc) • 2.29 kB
text/typescript
/**
* ASCII formatter for file trees
* Generates classic tree structure using box-drawing characters
*
* @module formatters
* @since 0.2.0
*/
import * as path from 'path';
import { FileTreeItem } from '../types';
import { getFileTypeInfo } from '../utils/fileUtils';
import { FormatOptions, FormatResult, TreeFormatter } from './treeFormatter.interface';
/**
* Formats file trees as ASCII art
* Perfect for terminals and plain text
*/
export class ASCIIFormatter implements TreeFormatter {
getName(): string {
return 'ASCII';
}
getExtension(): string {
return 'txt';
}
getLanguageId(): string {
return 'plaintext';
}
async format(items: FileTreeItem[], options: FormatOptions): Promise<FormatResult> {
const lines: string[] = [];
// Add header
lines.push(`File Tree: ${path.basename(options.rootPath)}`);
lines.push(`Generated on: ${new Date().toLocaleString()}`);
lines.push(`Root path: ${options.rootPath}`);
lines.push('');
lines.push('─'.repeat(80));
lines.push('');
// Generate tree structure
this.generateTreeLines(items, '', lines, options);
// Add footer
lines.push('');
lines.push('─'.repeat(80));
lines.push('Generated by FileTree Pro Extension');
return {
content: lines.join('\n'),
extension: this.getExtension(),
languageId: this.getLanguageId(),
};
}
/**
* Generate tree lines recursively
*/
private generateTreeLines(
items: FileTreeItem[],
prefix: string,
lines: string[],
options: FormatOptions
): void {
for (let i = 0; i < items.length; i++) {
const item = items[i];
const isLast = i === items.length - 1;
const connector = isLast ? '└── ' : '├── ';
const newPrefix = prefix + (isLast ? ' ' : '│ ');
// Get icon if needed
const fileInfo = getFileTypeInfo(item.name);
const icon = options.showIcons ? fileInfo.icon + ' ' : '';
const suffix = item.type === 'folder' ? '/' : '';
lines.push(`${prefix}${connector}${icon}${item.name}${suffix}`);
// Recursively process children
if (item.children && item.children.length > 0) {
this.generateTreeLines(item.children, newPrefix, lines, options);
}
}
}
}