MCPcopy Create free account
hub / github.com/diillson/chatcli / processDirectorySummary

Method processDirectorySummary

cli/cli_file_processing.go:397–513  ·  view source on GitHub ↗
(ctx context.Context, path string, tokenEstimator func(string) int, maxTokens int)

Source from the content-addressed store, hash-verified

395}
396
397func (cli *ChatCLI) processDirectorySummary(ctx context.Context, path string, tokenEstimator func(string) int, maxTokens int) (string, error) {
398 path, err := utils.ExpandPath(path)
399 if err != nil {
400 return "", fmt.Errorf("erro ao expandir o caminho: %w", err)
401 }
402
403 fileInfo, err := os.Stat(path)
404 if err != nil {
405 return "", fmt.Errorf("erro ao acessar o caminho: %w", err)
406 }
407
408 // Se for um arquivo único
409 if !fileInfo.IsDir() {
410 extension := filepath.Ext(path)
411 fileType := utils.DetectFileType(path)
412 size := fileInfo.Size()
413
414 return fmt.Sprintf("📄 %s (%s, %.2f KB)\nTipo: %s\nTamanho: %d bytes\n",
415 path, extension, float64(size)/1024, fileType, size), nil
416 }
417
418 // Se for um diretório, escanear a estrutura
419 var builder strings.Builder
420 builder.WriteString(fmt.Sprintf("📁 ESTRUTURA DO DIRETÓRIO: %s\n\n", path))
421
422 // Mapeamentos para estatísticas
423 fileTypes := make(map[string]int)
424 var totalSize int64
425 var totalFiles int
426 var totalDirs int
427
428 // Função recursiva para construir árvore de diretórios
429 var buildTree func(dir string, prefix string, depth int) error
430 buildTree = func(dir string, prefix string, depth int) error {
431 if depth > 3 { // Limitar profundidade para evitar estruturas gigantes
432 builder.WriteString(prefix + "...\n")
433 return nil
434 }
435
436 entries, err := os.ReadDir(dir)
437 if err != nil {
438 return err
439 }
440
441 for i, entry := range entries {
442 // Verificar se estamos dentro do limite de tokens
443 if tokenEstimator(builder.String()) > maxTokens/2 {
444 builder.WriteString(prefix + "... (truncado por limite de tokens)\n")
445 return nil
446 }
447
448 isLast := i == len(entries)-1
449
450 var newPrefix string
451 if isLast {
452 builder.WriteString(prefix + "└── ")
453 newPrefix = prefix + " "
454 } else {

Callers 2

processFileCommandMethod · 0.95
processDirectorySmartMethod · 0.95

Calls 8

ExpandPathFunction · 0.92
DetectFileTypeFunction · 0.92
ShouldSkipDirFunction · 0.92
ErrorfMethod · 0.80
NameMethod · 0.65
InfoMethod · 0.65
SizeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected