diff --git a/main.go b/main.go index 5d9b8f2..8c0e493 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,11 @@ func main() { Usage: "does not work with split-files", Aliases: []string{"c"}, }, + &cli.BoolFlag{ + Name: "quiet", + Usage: "silences success and tree printout", + Aliases: []string{"q"}, + }, &cli.StringSliceFlag{ Name: "include", Usage: "Include files matching glob patterns", @@ -48,6 +53,7 @@ func main() { Clipboard: ctx.Bool("clipboard"), Include: ctx.StringSlice("include"), Exclude: ctx.StringSlice("exclude"), + Quiet: ctx.Bool("quiet"), }) }, } diff --git a/parser/parse.go b/parser/parse.go index 5748ba6..5f887a8 100644 --- a/parser/parse.go +++ b/parser/parse.go @@ -6,39 +6,57 @@ import ( "path/filepath" ) -func parse(opts Options, basePath, currentPath string, newFile *os.File, remainingRecursions int) error { +type ParseOptions struct { + baseOptions Options + basePath string + currentPath string + newFile *os.File + remainingRecursions int + filesProcessed *[]string +} + +func parse(opts ParseOptions) error { // Return if the recursion limit has been reached - if remainingRecursions == 0 { + if opts.remainingRecursions == 0 { return nil } // Read the contents of the current directory - files, err := ioutil.ReadDir(currentPath) + files, err := ioutil.ReadDir(opts.currentPath) if err != nil { return err } for _, file := range files { - path := filepath.Join(currentPath, file.Name()) + path := filepath.Join(opts.currentPath, file.Name()) - relativePath, err := filepath.Rel(basePath, path) + relativePath, err := filepath.Rel(opts.basePath, path) if err != nil { return err } if file.IsDir() { - if !isIgnoredDirectory(relativePath, opts) { - err := parse(opts, basePath, path, newFile, remainingRecursions-1) + if !isIgnoredDirectory(relativePath, opts.baseOptions) { + err := parse(ParseOptions{ + baseOptions: opts.baseOptions, + basePath: opts.basePath, + currentPath: path, + newFile: opts.newFile, + remainingRecursions: opts.remainingRecursions - 1, + filesProcessed: opts.filesProcessed, + }) if err != nil { return err } } } else { - if !isIgnoredFile(relativePath, opts) { - err = writeFileContents(basePath, path, newFile) + if !isIgnoredFile(relativePath, opts.baseOptions) { + err = writeFileContents(opts.basePath, path, opts.newFile) if err != nil { return err } + + *opts.filesProcessed = append(*opts.filesProcessed, relativePath) } } } diff --git a/parser/run.go b/parser/run.go index 1073b5c..bb1cc04 100644 --- a/parser/run.go +++ b/parser/run.go @@ -13,6 +13,7 @@ type Options struct { Clipboard bool Include []string Exclude []string + Quiet bool } func Run(opts Options) error { @@ -40,7 +41,15 @@ func Run(opts Options) error { } }() - err = parse(opts, cwd, cwd, mashFile, MaxRecursions) + filesProcessed := new([]string) + err = parse(ParseOptions{ + baseOptions: opts, + basePath: cwd, + currentPath: cwd, + newFile: mashFile, + remainingRecursions: MaxRecursions, + filesProcessed: filesProcessed, + }) if err != nil { return fmt.Errorf("parsing files: %s", err) } @@ -56,27 +65,51 @@ func Run(opts Options) error { return fmt.Errorf("splitting mash file: %s", err) } - return clipboard.WriteAll(string(data)) + err = clipboard.WriteAll(string(data)) + if err != nil { + return fmt.Errorf("writing to clipboard: %s", err) + } + + } else { // Write to sendGPT directory + // Create the file directory + if err := os.MkdirAll(ToSendDirName, os.ModePerm); err != nil { + return fmt.Errorf("creating send directory: %s", err) + } + + if opts.SplitFiles { + // Split the mash file into smaller files to be consumed by chat bot + err = splitMashFile(mashFile) + if err != nil { + return fmt.Errorf("splitting mash file: %s", err) + } + } else { + err = writeMashFile(mashFile) + if err != nil { + return err + } + + } } - // Create the file directory - if err := os.MkdirAll(ToSendDirName, os.ModePerm); err != nil { - return fmt.Errorf("creating send directory: %s", err) + if opts.Quiet { + return nil } - if opts.SplitFiles { - // Split the mash file into smaller files to be consumed by chat bot - err = splitMashFile(mashFile) - if err != nil { - return fmt.Errorf("splitting mash file: %s", err) - } - } else { - err = writeMashFile(mashFile) - if err != nil { - return err - } + // Build the tree and print it + root := buildTree(*filesProcessed) + printTree(root, "", true) + // Print counts + _, fileCount := countNodes(root) + + message := "" + if opts.Clipboard { + message = "copied to clipboard" + } else { + message = "written to ./sendGPT/" } + fmt.Printf("\n%d files %s\n", fileCount, message) + return nil } diff --git a/parser/tree.go b/parser/tree.go new file mode 100644 index 0000000..9bc2c64 --- /dev/null +++ b/parser/tree.go @@ -0,0 +1,75 @@ +package parser + +import ( + "fmt" + "os" + "sort" + "strings" +) + +type Node struct { + Name string + IsDir bool + Children []*Node +} + +func buildTree(paths []string) *Node { + root := &Node{Name: ".", IsDir: true} + for _, path := range paths { + parts := strings.Split(path, string(os.PathSeparator)) + currentNode := root + for i, part := range parts { + // Check if part already exists as a child + var childNode *Node + for _, child := range currentNode.Children { + if child.Name == part { + childNode = child + break + } + } + if childNode == nil { + // Create new node + isDir := i < len(parts)-1 + childNode = &Node{Name: part, IsDir: isDir} + currentNode.Children = append(currentNode.Children, childNode) + } + currentNode = childNode + } + } + return root +} + +func printTree(node *Node, prefix string, isLast bool) { + if node.Name != "." { + fmt.Print(prefix) + if isLast { + fmt.Print("└── ") + prefix += " " + } else { + fmt.Print("├── ") + prefix += "│ " + } + fmt.Println(node.Name) + } + // Sort children by name + sort.Slice(node.Children, func(i, j int) bool { + return node.Children[i].Name < node.Children[j].Name + }) + for i, child := range node.Children { + printTree(child, prefix, i == len(node.Children)-1) + } +} + +func countNodes(node *Node) (dirs int, files int) { + if node.IsDir { + dirs++ + } else { + files++ + } + for _, child := range node.Children { + d, f := countNodes(child) + dirs += d + files += f + } + return +}