blob detection

This commit is contained in:
u80864958
2025-05-16 10:21:28 +02:00
parent 6e63af80a6
commit 7f93ae2c20
3 changed files with 20 additions and 27 deletions

View File

@ -12,6 +12,8 @@ Example use case:
- Aggregate and view the contents of multiple files or directories in one command. - Aggregate and view the contents of multiple files or directories in one command.
- Automatically copy the aggregated result to the clipboard for seamless integration with other tools or platforms. - Automatically copy the aggregated result to the clipboard for seamless integration with other tools or platforms.
- Export your codebase to a markdown document.
- Export your codebase to a typst document.
## Dependencies ## Dependencies
@ -42,14 +44,21 @@ The binary will be placed in your $GOPATH/bin directory. Ensure $GOPATH/bin is i
## Example Usage ## Example Usage
Concatenate files and directories: ### Concatenate files and directories:
```bash ```bash
./pat file1.txt folder/ pat file1.txt folder/
``` ```
Output is printed to the terminal and copied to the clipboard, allowing you to paste it elsewhere. Output is printed to the terminal and copied to the clipboard, allowing you to paste it elsewhere.
### Create printed
```sh
pat -t -i .gitignore . | typst compile /dev/stdin pat.pdf
```
--- ---
#### Notes #### Notes

BIN
pat Executable file

Binary file not shown.

View File

@ -6,17 +6,9 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"unicode/utf8"
) )
var INVALID_SUFFIXES = []string{
"png",
"jpg",
"jpeg",
"webp",
"ico",
"ttf",
}
func (c Cater) dir(dir string) (e entry, err error) { func (c Cater) dir(dir string) (e entry, err error) {
files, err := os.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
@ -36,10 +28,6 @@ func (c Cater) dir(dir string) (e entry, err error) {
var ent entry var ent entry
if !file.IsDir() { if !file.IsDir() {
if !validSuffix(file.Name()) {
continue
}
ent, err = c.file(path) ent, err = c.file(path)
} else { } else {
ent, err = c.dir(path) ent, err = c.dir(path)
@ -59,6 +47,10 @@ func (c Cater) file(filePath string) (e entry, err error) {
} }
defer file.Close() defer file.Close()
e.fqname = filePath
e.name = name(filePath)
e.children = []entry{}
// read file into strings.Builder // read file into strings.Builder
var sb strings.Builder var sb strings.Builder
reader := bufio.NewReader(file) reader := bufio.NewReader(file)
@ -68,16 +60,17 @@ func (c Cater) file(filePath string) (e entry, err error) {
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return e, err return e, err
} }
if !utf8.Valid([]byte(line)) {
e.content = "blob\n"
return e, nil
}
sb.WriteString(line) sb.WriteString(line)
if err == io.EOF { if err == io.EOF {
break break
} }
} }
e.fqname = filePath
e.name = name(filePath)
e.content = sb.String() e.content = sb.String()
e.children = []entry{}
return return
} }
@ -86,12 +79,3 @@ func name(name string) string {
ps := strings.Split(name, "/") ps := strings.Split(name, "/")
return ps[len(ps)-1] return ps[len(ps)-1]
} }
func validSuffix(name string) bool {
for _, s := range INVALID_SUFFIXES {
if strings.HasSuffix(name, s) {
return false
}
}
return true
}