first commit

This commit is contained in:
2026-05-15 23:36:12 +08:00
parent 62fe8ddf4a
commit 52d6dde4aa
10 changed files with 286 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
package tree
import (
"fmt"
"os"
"path/filepath"
"strings"
"cliprepo/internal/gitignore"
"github.com/atotto/clipboard"
)
func Generate(root string) (string, error) {
ig, _ := gitignore.Load(root)
var builder strings.Builder
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == root {
builder.WriteString(".\n")
return nil
}
rel, _ := filepath.Rel(root, path)
if ig.MatchesPath(rel) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
level := strings.Count(rel, string(os.PathSeparator))
prefix := strings.Repeat("│ ", level)
builder.WriteString(fmt.Sprintf("%s├── %s\n", prefix, info.Name()))
return nil
})
return builder.String(), err
}
func Copy(content string) error {
return clipboard.WriteAll(content)
}
func Write(path, content string) error {
return os.WriteFile(path, []byte(content), 0644)
}