finished vertical-.slices
This commit is contained in:
+44
-32
@@ -53,18 +53,19 @@ func (s *stringSlice) Set(v string) error {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
InputPath string
|
||||
OutputPath string
|
||||
AppendMode bool
|
||||
Force bool
|
||||
SingleFile bool
|
||||
MaxSize int64
|
||||
MaxDepth int
|
||||
Ignores []string
|
||||
Extensions []string
|
||||
AbsRoot string
|
||||
ProtonDrive bool
|
||||
DrivePath string
|
||||
InputPath string
|
||||
OutputPath string
|
||||
AppendMode bool
|
||||
Force bool
|
||||
SingleFile bool
|
||||
MaxSize int64
|
||||
MaxDepth int
|
||||
Ignores []string
|
||||
Extensions []string
|
||||
AbsRoot string
|
||||
ProtonDrive bool
|
||||
DrivePath string
|
||||
VerticalSlices bool
|
||||
}
|
||||
|
||||
func PrintUsage() {
|
||||
@@ -77,17 +78,18 @@ func PrintUsage() {
|
||||
|
||||
func Parse() (*Config, error) {
|
||||
var (
|
||||
input string
|
||||
output string
|
||||
appendMd bool
|
||||
force bool
|
||||
maxSize int64
|
||||
maxDepth int
|
||||
ignores stringSlice
|
||||
exts stringSlice
|
||||
showVer bool
|
||||
protonDrive bool
|
||||
drivePath string
|
||||
input string
|
||||
output string
|
||||
appendMd bool
|
||||
force bool
|
||||
maxSize int64
|
||||
maxDepth int
|
||||
ignores stringSlice
|
||||
exts stringSlice
|
||||
showVer bool
|
||||
protonDrive bool
|
||||
drivePath string
|
||||
verticalSlices bool
|
||||
)
|
||||
|
||||
flag.StringVar(&input, "i", "", "single file input (append mode only)")
|
||||
@@ -101,6 +103,7 @@ func Parse() (*Config, error) {
|
||||
flag.BoolVar(&showVer, "version", false, "print version and exit")
|
||||
flag.BoolVar(&protonDrive, "proton-drive", false, "upload output to Proton Drive after writing locally")
|
||||
flag.StringVar(&drivePath, "drive-path", "", "full remote path on Proton Drive (requires --proton-drive)")
|
||||
flag.BoolVar(&verticalSlices, "vertical-slices", false, "split output into root file + one markdown per top-level directory")
|
||||
|
||||
flag.Usage = PrintUsage
|
||||
flag.Parse()
|
||||
@@ -118,16 +121,25 @@ func Parse() (*Config, error) {
|
||||
return nil, fmt.Errorf("--drive-path requires --proton-drive")
|
||||
}
|
||||
|
||||
if verticalSlices && appendMd {
|
||||
return nil, fmt.Errorf("--vertical-slices cannot be used with --append")
|
||||
}
|
||||
|
||||
if verticalSlices && input != "" {
|
||||
return nil, fmt.Errorf("--vertical-slices cannot be used with --input-file")
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
OutputPath: output,
|
||||
AppendMode: appendMd,
|
||||
Force: force,
|
||||
MaxSize: maxSize,
|
||||
MaxDepth: maxDepth,
|
||||
Ignores: append(DefaultIgnores, ignores...),
|
||||
Extensions: append(DefaultExts, exts...),
|
||||
ProtonDrive: protonDrive,
|
||||
DrivePath: drivePath,
|
||||
OutputPath: output,
|
||||
AppendMode: appendMd,
|
||||
Force: force,
|
||||
MaxSize: maxSize,
|
||||
MaxDepth: maxDepth,
|
||||
Ignores: append(DefaultIgnores, ignores...),
|
||||
Extensions: append(DefaultExts, exts...),
|
||||
ProtonDrive: protonDrive,
|
||||
DrivePath: drivePath,
|
||||
VerticalSlices: verticalSlices,
|
||||
}
|
||||
|
||||
if input != "" {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
|
||||
const fenceChars = "~~~"
|
||||
|
||||
// Write writes a single markdown file with tree and file contents.
|
||||
func Write(result *walker.Result, outputPath string, appendMode bool, singleFile bool) error {
|
||||
flags := os.O_CREATE | os.O_WRONLY
|
||||
if appendMode {
|
||||
@@ -29,20 +31,19 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
// Determine the correct H1 path
|
||||
h1Path := result.AbsRoot
|
||||
if singleFile && len(result.Entries) > 0 {
|
||||
h1Path = filepath.Join(result.AbsRoot, result.Entries[0].RelPath)
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sb, "# %s\n\n", h1Path)
|
||||
fmt.Fprintf(&sb, "# %s [ROOT]\n\n", h1Path)
|
||||
|
||||
if !singleFile {
|
||||
root := tree.Build(result.Entries)
|
||||
rootName := filepath.Base(result.AbsRoot)
|
||||
sb.WriteString("## Directory Tree\n\n")
|
||||
sb.WriteString(fenceChars + "\n")
|
||||
sb.WriteString(tree.Render(root, rootName))
|
||||
sb.WriteString(renderTreeForLLM(root, rootName))
|
||||
sb.WriteString(fenceChars + "\n\n")
|
||||
}
|
||||
|
||||
@@ -61,8 +62,9 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
|
||||
}
|
||||
|
||||
language := extToLang(filepath.Ext(entry.RelPath))
|
||||
lines := countLines(content)
|
||||
|
||||
fmt.Fprintf(&sb, "### ./%s\n\n", entry.RelPath)
|
||||
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)
|
||||
@@ -72,7 +74,7 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
|
||||
if !strings.HasSuffix(content, "\n") {
|
||||
fmt.Fprintf(&sb, "\n")
|
||||
}
|
||||
fmt.Fprintf(&sb, "%s\n\n", fenceChars)
|
||||
fmt.Fprintf(&sb, "%s\n--- END FILE ---\n\n", fenceChars)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +82,337 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteSlices generates a root markdown file plus one markdown per top-level directory.
|
||||
// Returns the list of generated file paths.
|
||||
func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
entries := result.Entries
|
||||
if len(entries) == 0 {
|
||||
return nil, fmt.Errorf("no entries to write")
|
||||
}
|
||||
|
||||
absRoot := result.AbsRoot
|
||||
rootName := filepath.Base(absRoot)
|
||||
|
||||
topLevelDirs := make(map[string][]walker.Entry)
|
||||
rootFiles := make([]walker.Entry, 0)
|
||||
|
||||
for _, e := range entries {
|
||||
rel := e.RelPath
|
||||
parts := strings.Split(rel, string(filepath.Separator))
|
||||
if len(parts) == 1 {
|
||||
rootFiles = append(rootFiles, e)
|
||||
} else {
|
||||
firstPart := parts[0]
|
||||
topLevelDirs[firstPart] = append(topLevelDirs[firstPart], e)
|
||||
}
|
||||
}
|
||||
|
||||
outDir := filepath.Dir(outputPath)
|
||||
|
||||
var generated []string
|
||||
|
||||
// Write root file
|
||||
rootPath := outputPath
|
||||
generated = append(generated, rootPath)
|
||||
|
||||
if err := writeRootSlice(rootPath, absRoot, rootName, rootFiles, topLevelDirs, outDir); err != nil {
|
||||
return nil, fmt.Errorf("failed to write root slice: %w", err)
|
||||
}
|
||||
|
||||
// Write each top-level directory slice into a mirroring subdirectory
|
||||
var dirNames []string
|
||||
for k := range topLevelDirs {
|
||||
dirNames = append(dirNames, k)
|
||||
}
|
||||
sort.Strings(dirNames)
|
||||
|
||||
for _, dirName := range dirNames {
|
||||
sliceName := strings.TrimSuffix(dirName, "/") + ".md"
|
||||
sliceDir := filepath.Join(outDir, dirName)
|
||||
if err := os.MkdirAll(sliceDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("cannot create slice directory %s: %w", sliceDir, err)
|
||||
}
|
||||
slicePath := filepath.Join(sliceDir, sliceName)
|
||||
generated = append(generated, slicePath)
|
||||
|
||||
absDir := filepath.Join(absRoot, dirName)
|
||||
dirEntries := topLevelDirs[dirName]
|
||||
|
||||
// Strip dirName prefix for tree building only
|
||||
treeEntries := make([]walker.Entry, len(dirEntries))
|
||||
for i, e := range dirEntries {
|
||||
treeEntries[i] = walker.Entry{
|
||||
RelPath: strings.TrimPrefix(e.RelPath, dirName+string(filepath.Separator)),
|
||||
Size: e.Size,
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeDirSlice(slicePath, absDir, absRoot, dirName, treeEntries, dirEntries, filepath.Base(outputPath)); err != nil {
|
||||
return nil, fmt.Errorf("failed to write slice %s: %w", sliceName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return generated, nil
|
||||
}
|
||||
|
||||
func writeRootSlice(path, absRoot, rootName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry, outDir string) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create root file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
fmt.Fprintf(&sb, "# %s [ROOT]\n\n", absRoot)
|
||||
|
||||
// Build combined tree with references
|
||||
treeLines := buildTreeWithRefs(rootName, topLevelDirs, rootFiles)
|
||||
sb.WriteString("## Directory Tree\n\n")
|
||||
sb.WriteString(fenceChars + "\n")
|
||||
sb.WriteString(treeLines)
|
||||
sb.WriteString(fenceChars + "\n\n")
|
||||
|
||||
// Vertical slices index for LLM navigation
|
||||
sb.WriteString("## Vertical Slices Index\n\n")
|
||||
sb.WriteString("These files contain the full contents of each top-level directory.\n")
|
||||
sb.WriteString("Read the relevant slice file for detailed source code.\n\n")
|
||||
sb.WriteString("| Slice File | Directory | Files | Total Bytes |\n")
|
||||
sb.WriteString("|------------|-----------|-------|-------------|\n")
|
||||
|
||||
var dirNames []string
|
||||
for k := range topLevelDirs {
|
||||
dirNames = append(dirNames, k)
|
||||
}
|
||||
sort.Strings(dirNames)
|
||||
|
||||
for _, dirName := range dirNames {
|
||||
entries := topLevelDirs[dirName]
|
||||
fileCount := len(entries)
|
||||
totalBytes := int64(0)
|
||||
for _, e := range entries {
|
||||
totalBytes += e.Size
|
||||
}
|
||||
sliceFile := filepath.Join(dirName, strings.TrimSuffix(dirName, "/")+".md")
|
||||
fmt.Fprintf(&sb, "| %s | %s/ | %d | %d |\n", sliceFile, dirName, fileCount, totalBytes)
|
||||
}
|
||||
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 := readFile(fullPath)
|
||||
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")
|
||||
fmt.Fprintf(&sb, "%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 writeDirSlice(path, absDir, absRoot, dirName string, treeEntries, fileEntries []walker.Entry, rootFileName string) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create slice file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
fmt.Fprintf(&sb, "# %s [SLICE]\n\n", absDir)
|
||||
fmt.Fprintf(&sb, "Root document: %s\n\n", rootFileName)
|
||||
|
||||
sb.WriteString("## Directory Tree\n\n")
|
||||
sb.WriteString(fenceChars + "\n")
|
||||
|
||||
sliceTree := buildTreeFromEntries(dirName, treeEntries)
|
||||
sb.WriteString(sliceTree)
|
||||
sb.WriteString(fenceChars + "\n\n")
|
||||
|
||||
if len(fileEntries) > 0 {
|
||||
sb.WriteString("## Contents\n\n")
|
||||
|
||||
sort.Slice(fileEntries, func(i, j int) bool {
|
||||
return fileEntries[i].RelPath < fileEntries[j].RelPath
|
||||
})
|
||||
|
||||
for _, entry := range fileEntries {
|
||||
fullPath := filepath.Join(absRoot, entry.RelPath)
|
||||
content, err := readFile(fullPath)
|
||||
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")
|
||||
fmt.Fprintf(&sb, "%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 buildTreeWithRefs(rootName string, topLevelDirs map[string][]walker.Entry, rootFiles []walker.Entry) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString(rootName)
|
||||
sb.WriteString("/\n")
|
||||
|
||||
// Collect and sort directory names
|
||||
var dirNames []string
|
||||
for k := range topLevelDirs {
|
||||
dirNames = append(dirNames, k)
|
||||
}
|
||||
sort.Strings(dirNames)
|
||||
|
||||
// Sort root files
|
||||
sort.Slice(rootFiles, func(i, j int) bool {
|
||||
return rootFiles[i].RelPath < rootFiles[j].RelPath
|
||||
})
|
||||
|
||||
// Merge directories and files into a single sorted list for display
|
||||
type treeItem struct {
|
||||
name string
|
||||
isDir bool
|
||||
sliceRef string
|
||||
}
|
||||
|
||||
items := make([]treeItem, 0, len(dirNames)+len(rootFiles))
|
||||
for _, d := range dirNames {
|
||||
items = append(items, treeItem{
|
||||
name: d,
|
||||
isDir: true,
|
||||
sliceRef: filepath.Join(d, strings.TrimSuffix(d, "/")+".md"),
|
||||
})
|
||||
}
|
||||
for _, rf := range rootFiles {
|
||||
items = append(items, treeItem{
|
||||
name: rf.RelPath,
|
||||
isDir: false,
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
return items[i].name < items[j].name
|
||||
})
|
||||
|
||||
for _, item := range items {
|
||||
sb.WriteString("+- ")
|
||||
sb.WriteString(item.name)
|
||||
if item.isDir {
|
||||
sb.WriteString("/")
|
||||
sb.WriteString(" -> see ")
|
||||
sb.WriteString(item.sliceRef)
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func buildTreeFromEntries(rootName string, entries []walker.Entry) string {
|
||||
root := &tree.Node{IsDir: true, Children: make(map[string]*tree.Node)}
|
||||
|
||||
for _, e := range entries {
|
||||
parts := strings.Split(e.RelPath, string(filepath.Separator))
|
||||
cur := root
|
||||
|
||||
for i, part := range parts {
|
||||
isLeaf := i == len(parts)-1
|
||||
if _, ok := cur.Children[part]; !ok {
|
||||
node := &tree.Node{Name: part, IsDir: !isLeaf}
|
||||
if !isLeaf {
|
||||
node.Children = make(map[string]*tree.Node)
|
||||
}
|
||||
cur.Children[part] = node
|
||||
}
|
||||
cur = cur.Children[part]
|
||||
}
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(rootName)
|
||||
sb.WriteString("/\n")
|
||||
renderNodeLLM(&sb, root, "")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func renderTreeForLLM(root *tree.Node, rootName string) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString(rootName)
|
||||
sb.WriteString("/\n")
|
||||
renderNodeLLM(&sb, root, "")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func renderNodeLLM(sb *strings.Builder, node *tree.Node, prefix string) {
|
||||
keys := make([]string, 0, len(node.Children))
|
||||
for k := range node.Children {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for i, key := range keys {
|
||||
child := node.Children[key]
|
||||
isLast := i == len(keys)-1
|
||||
|
||||
sb.WriteString(prefix)
|
||||
sb.WriteString("+- ")
|
||||
|
||||
sb.WriteString(child.Name)
|
||||
if child.IsDir {
|
||||
sb.WriteString("/")
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
|
||||
if child.IsDir && child.Children != nil {
|
||||
newPrefix := prefix
|
||||
if isLast {
|
||||
newPrefix += " "
|
||||
} else {
|
||||
newPrefix += "| "
|
||||
}
|
||||
renderNodeLLM(sb, child, newPrefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(path string) (string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -131,3 +464,14 @@ func extToLang(ext string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func countLines(content string) int {
|
||||
if content == "" {
|
||||
return 0
|
||||
}
|
||||
count := strings.Count(content, "\n")
|
||||
if len(content) > 0 && content[len(content)-1] != '\n' {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user