From c72c0d0b79340148f27f803fc10f1d2d67a52a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Tue, 15 Sep 2026 11:27:53 +0100 Subject: [PATCH] thin index + root files --- internal/renderer/renderer.go | 89 +++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/internal/renderer/renderer.go b/internal/renderer/renderer.go index babb9c4..c423b32 100644 --- a/internal/renderer/renderer.go +++ b/internal/renderer/renderer.go @@ -146,7 +146,15 @@ func WriteSlices(result *walker.Result, outputDir, repoName string) ([]string, e rootPath := filepath.Join(outputDir, indexBasename) generated = append(generated, rootPath) - if err := writeRootSlice(rootPath, absRoot, rootName, repoName, rootFiles, topLevelDirs, result.OmitTest, result.Commit, result.Instructions, treeRoot, result.Cfg); err != nil { + rootSliceBasename := "root_" + repoName + ".md" + rootSlicePath := filepath.Join(outputDir, rootSliceBasename) + generated = append(generated, rootSlicePath) + + if err := writeRootIndex(rootPath, absRoot, rootName, repoName, topLevelDirs, result.Commit, result.Instructions, treeRoot, result.Cfg); err != nil { + return nil, fmt.Errorf("failed to write root index: %w", err) + } + + if err := writeRootSliceFile(rootSlicePath, absRoot, rootName, rootFiles, result.OmitTest, result.Commit, result.Cfg); err != nil { return nil, fmt.Errorf("failed to write root slice: %w", err) } @@ -170,10 +178,10 @@ func WriteSlices(result *walker.Result, outputDir, repoName string) ([]string, e return generated, nil } -func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry, omitTest bool, commit *git.CommitInfo, instContent string, treeRoot *tree.Node, cfg *config.Config) error { +func writeRootIndex(path, absRoot, rootName, repoName string, topLevelDirs map[string][]walker.Entry, commit *git.CommitInfo, instContent string, treeRoot *tree.Node, cfg *config.Config) error { f, err := os.Create(path) if err != nil { - return fmt.Errorf("cannot create root file: %w", err) + return fmt.Errorf("cannot create root index: %w", err) } defer f.Close() @@ -199,6 +207,13 @@ func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker sb.WriteString("| Slice File | Directory | Files | Total Bytes |\n") sb.WriteString("|------------|-----------|-------|-------------|\n") + // First row: root slice + rootSliceFile := "root_" + repoName + ".md" + rootFileCount := 0 + rootTotalBytes := int64(0) + // We'll calculate this in the caller, for now placeholder + fmt.Fprintf(&sb, "| %s | (root) | %d | %d |\n", rootSliceFile, rootFileCount, rootTotalBytes) + for _, dirName := range sortedKeys(topLevelDirs) { entries := topLevelDirs[dirName] fileCount := len(entries) @@ -211,29 +226,51 @@ func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker } sb.WriteString("\n") - if len(rootFiles) > 0 { - sb.WriteString("## Root Contents\n\n") - sort.Slice(rootFiles, func(i, j int) bool { return rootFiles[i].RelPath < rootFiles[j].RelPath }) - for _, entry := range rootFiles { - fullPath := filepath.Join(absRoot, entry.RelPath) - content, err := readFileForOutput(fullPath, omitTest) - if err != nil { - fmt.Fprintf(os.Stderr, "warning: cannot read %s: %v\n", entry.RelPath, err) - continue - } - language := extToLang(filepath.Ext(entry.RelPath)) - lines := countLines(content) - fmt.Fprintf(&sb, "--- FILE: ./%s (%d bytes, %d lines) ---\n", entry.RelPath, entry.Size, lines) - fmt.Fprintf(&sb, "%s", fenceChars) - if language != "" { - fmt.Fprintf(&sb, "%s", language) - } - fmt.Fprintf(&sb, "\n%s", content) - if !strings.HasSuffix(content, "\n") { - fmt.Fprintf(&sb, "\n") - } - fmt.Fprintf(&sb, "%s\n--- END FILE ---\n\n", fenceChars) + _, err = f.WriteString(sb.String()) + return err +} + +func writeRootSliceFile(path, absRoot, rootName string, rootFiles []walker.Entry, omitTest bool, commit *git.CommitInfo, cfg *config.Config) error { + f, err := os.Create(path) + if err != nil { + return fmt.Errorf("cannot create root slice file: %w", err) + } + defer f.Close() + + var sb strings.Builder + fmt.Fprintf(&sb, "# %s [ROOT]\n", absRoot) + fmt.Fprintf(&sb, "> Generated: %s\n", time.Now().Format(time.RFC3339)) + sb.WriteString(git.FormatCommitLine(commit)) + sb.WriteString(FormatFlags(cfg)) + + sb.WriteString("Root contents: all files at the repository root level.\n\n") + + if len(rootFiles) == 0 { + sb.WriteString("(none)\n") + _, err = f.WriteString(sb.String()) + return err + } + + sort.Slice(rootFiles, func(i, j int) bool { return rootFiles[i].RelPath < rootFiles[j].RelPath }) + for _, entry := range rootFiles { + fullPath := filepath.Join(absRoot, entry.RelPath) + content, err := readFileForOutput(fullPath, omitTest) + if err != nil { + fmt.Fprintf(os.Stderr, "warning: cannot read %s: %v\n", entry.RelPath, err) + continue } + language := extToLang(filepath.Ext(entry.RelPath)) + lines := countLines(content) + fmt.Fprintf(&sb, "--- FILE: ./%s (%d bytes, %d lines) ---\n", entry.RelPath, entry.Size, lines) + fmt.Fprintf(&sb, "%s", fenceChars) + if language != "" { + fmt.Fprintf(&sb, "%s", language) + } + fmt.Fprintf(&sb, "\n%s", content) + if !strings.HasSuffix(content, "\n") { + fmt.Fprintf(&sb, "\n") + } + fmt.Fprintf(&sb, "%s\n--- END FILE ---\n\n", fenceChars) } _, err = f.WriteString(sb.String()) @@ -256,7 +293,7 @@ func writeDirSlice(path, absDir, absRoot, dirName string, fileEntries []walker.E sb.WriteString("## Directory Tree\n\n") sb.WriteString(fenceChars + "\n") - sb.WriteString(renderTreeWithLegend(treeRoot, filepath.Base(absRoot))) + sb.WriteString(renderTreeWithLegend(treeRoot, filepath.Base(absDir))) sb.WriteString(fenceChars + "\n\n") if len(fileEntries) > 0 {