53 lines
957 B
Go
53 lines
957 B
Go
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)
|
|
} |