Skip to content

Commit

Permalink
Merge pull request #9 from skelouse/3-add-success-output
Browse files Browse the repository at this point in the history
3 add success output
  • Loading branch information
skelouse authored Nov 28, 2024
2 parents 3f78576 + ca26021 commit 0ac3d33
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 25 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -48,6 +53,7 @@ func main() {
Clipboard: ctx.Bool("clipboard"),
Include: ctx.StringSlice("include"),
Exclude: ctx.StringSlice("exclude"),
Quiet: ctx.Bool("quiet"),
})
},
}
Expand Down
36 changes: 27 additions & 9 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
65 changes: 49 additions & 16 deletions parser/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Options struct {
Clipboard bool
Include []string
Exclude []string
Quiet bool
}

func Run(opts Options) error {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
75 changes: 75 additions & 0 deletions parser/tree.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0ac3d33

Please # to comment.