Skip to content

Commit

Permalink
Merge pull request #10 from skelouse/5-size-warning-or-block
Browse files Browse the repository at this point in the history
block when copying too much text
  • Loading branch information
skelouse authored Nov 28, 2024
2 parents 0ac3d33 + f481a1d commit 313f96f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
func main() {
cmd := &cli.Command{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "split-files",
Aliases: []string{"s"},
},
&cli.BoolFlag{
Name: "clipboard",
Usage: "does not work with split-files",
Expand All @@ -35,6 +31,15 @@ func main() {
Name: "exclude",
Usage: "Exclude files matching glob patterns",
},
&cli.IntFlag{
Name: "max-size",
Usage: "Maximum total size of files to process (in bytes)",
Value: 2 * 1024 * 1024, // Default to 2MB
},
&cli.BoolFlag{
Name: "split-files",
Aliases: []string{"s"},
},
},
Action: func(ctx *cli.Context) error {
// Check for unexpected positional arguments
Expand All @@ -54,6 +59,7 @@ func main() {
Include: ctx.StringSlice("include"),
Exclude: ctx.StringSlice("exclude"),
Quiet: ctx.Bool("quiet"),
MaxSize: ctx.Int("max-size"),
})
},
}
Expand Down
22 changes: 22 additions & 0 deletions parser/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -13,6 +14,7 @@ type ParseOptions struct {
newFile *os.File
remainingRecursions int
filesProcessed *[]string
currentSize int64
}

func parse(opts ParseOptions) error {
Expand Down Expand Up @@ -44,18 +46,38 @@ func parse(opts ParseOptions) error {
newFile: opts.newFile,
remainingRecursions: opts.remainingRecursions - 1,
filesProcessed: opts.filesProcessed,
currentSize: opts.currentSize,
})
if err != nil {
return err
}
}
} else {
if !isIgnoredFile(relativePath, opts.baseOptions) {
// Get file size
fileInfo, err := os.Stat(path)
if err != nil {
return err
}

// Calculate new total size
fileSize := fileInfo.Size()
newTotalSize := opts.currentSize + fileSize

// Check if new total size exceeds MaxSize
if opts.baseOptions.MaxSize > 0 && newTotalSize > opts.baseOptions.MaxSize {
return fmt.Errorf("\n...maximum size limit reached at %s (%d bytes).\n...current limit (%d bytes), maybe set --max-size to a value higher than that", path, fileSize, opts.baseOptions.MaxSize)
}

// Write file contents
err = writeFileContents(opts.basePath, path, opts.newFile)
if err != nil {
return err
}

// Update current size
opts.currentSize = newTotalSize

*opts.filesProcessed = append(*opts.filesProcessed, relativePath)
}
}
Expand Down
1 change: 1 addition & 0 deletions parser/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Options struct {
Include []string
Exclude []string
Quiet bool
MaxSize int64
}

func Run(opts Options) error {
Expand Down

0 comments on commit 313f96f

Please # to comment.