3 Commits

Author SHA1 Message Date
158e963c7b version2 (#1)
Co-authored-by: u80864958 <niklas.breitenstein@bit.admin.ch>
Reviewed-on: #1
2025-04-02 17:29:26 +02:00
abe9c7d1fb update readme 2025-01-04 17:58:53 +01:00
1a58eba7cd first working version 2025-01-04 12:14:25 +01:00
11 changed files with 433 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
**/__debug_bin*
main

19
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/main.go",
"args": [ ".."]
}
]
}

View File

@ -1,2 +1,51 @@
# pat
# PAT
## What it Does
`pat` is a command-line tool for concatenating and displaying the contents of files and directories. It:
1. Processes the specified files and directories recursively.
2. Appends content to a structured output with file paths and a delimiter for clarity.
3. Copies the resulting output to the system clipboard for convenient sharing or reuse.
Example use case:
- 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.
## Dependencies
1. **Golang** (only to install / build):
- The application requires the Go programming language (`>= 1.18`) to compile and run.
- Dependency: `golang.design/x/clipboard` for clipboard interaction.
2. **External Tools**:
- On Wayland-based systems, `wl-copy` must be installed for clipboard functionality.
## Installation
Install go-cat directly using go install:
``` sh
go install git.schreifuchs.ch/schreifuchs/pat@latest
```
The binary will be placed in your $GOPATH/bin directory. Ensure $GOPATH/bin is in your system's PATH to run it directly.
## Example Usage
Concatenate files and directories:
```bash
./pat file1.txt folder/
```
Output is printed to the terminal and copied to the clipboard, allowing you to paste it elsewhere.
---
#### Notes
- The tool uses `---------------------------------------------------------------------------` as a delimiter to separate file contents for readability.
- If clipboard functionality fails (e.g., unsupported environment), the application will still display the result in the terminal.

47
cmd/main.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"flag"
"fmt"
"os"
"git.schreifuchs.ch/schreifuchs/pat/pkg/cat"
"git.schreifuchs.ch/schreifuchs/pat/pkg/clip"
"git.schreifuchs.ch/schreifuchs/pat/pkg/ignore"
)
const DELEMITTER = "-(%s)--------------------------------------------------------------------------\n"
func main() {
ignorePath := flag.String("i", "", "set path to gitignore, if no gitignore parent dirs will be searched")
hiddenFiles := flag.Bool("h", false, "show hidden files")
flag.Parse()
cats, err := cat.Path(flag.Args()...)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if *ignorePath != "" {
i, err := ignore.FindGitignore(*ignorePath)
if err != nil {
fmt.Printf("can't get gitignore: %v", err)
os.Exit(1)
}
cats = cats.Ignored(i)
}
if *hiddenFiles == false {
cats = cats.Ignored(ignore.Filesystem{})
}
out := cats.ToString(DELEMITTER)
fmt.Print(out)
if err = clip.Copy(out); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

17
go.mod Normal file
View File

@ -0,0 +1,17 @@
module git.schreifuchs.ch/schreifuchs/pat
go 1.23.4
require (
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817
golang.design/x/clipboard v0.7.0
)
require (
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394 // indirect
golang.org/x/image v0.25.0 // indirect
golang.org/x/mobile v0.0.0-20250305212854-3a7bc9f8a4de // indirect
golang.org/x/sys v0.31.0 // indirect
)

61
go.sum Normal file
View File

@ -0,0 +1,61 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394 h1:bFYqOIMdeiCEdzPJkLiOoMDzW/v3tjW4AA/RmUZYsL8=
golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394/go.mod h1:ygj7T6vSGhhm/9yTpOQQNvuAUFziTH7RUiH74EoE2C8=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c h1:Gk61ECugwEHL6IiyyNLXNzmu8XslmRP2dS0xjIYhbb4=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mobile v0.0.0-20250305212854-3a7bc9f8a4de h1:WuckfUoaRGJfaQTPZvlmcaQwg4Xj9oS2cvvh3dUqpDo=
golang.org/x/mobile v0.0.0-20250305212854-3a7bc9f8a4de/go.mod h1:/IZuixag1ELW37+FftdmIt59/3esqpAWM/QqWtf7HUI=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

61
pkg/cat/cater.go Normal file
View File

@ -0,0 +1,61 @@
package cat
import (
"fmt"
"os"
"strings"
)
type Cater map[string]string
func Path(paths ...string) (c Cater, err error) {
c = make(Cater)
var p os.FileInfo
for _, path := range paths {
p, err = os.Stat(path)
if err != nil {
return
}
if p.IsDir() {
err = c.dir(path)
} else {
err = c.file(path)
}
if err != nil {
return
}
}
return
}
func (c Cater) Ignored(ignore ignorer) Cater {
cat := make(Cater)
for name, content := range c {
if ignore.Ignore(name) {
continue
}
cat[name] = content
}
return cat
}
func (c Cater) ToString(delemiter string) string {
var sb strings.Builder
for name, content := range c {
sb.WriteString(fmt.Sprintf(delemiter, name))
sb.WriteString(content)
}
return sb.String()
}
type ignorer interface {
// Ignore() returns true when the given path shall be Ignored.
Ignore(path string) bool
}

58
pkg/cat/internal.go Normal file
View File

@ -0,0 +1,58 @@
package cat
import (
"bufio"
"io"
"os"
"path"
"strings"
)
func (c Cater) dir(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
i, err := file.Info()
if err != nil {
continue
}
path := path.Join(dir, i.Name())
if !file.IsDir() {
c.file(path)
} else {
c.dir(path)
}
}
return nil
}
func (c Cater) file(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// read file into strings.Builder
var sb strings.Builder
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
sb.WriteString(line)
if err == io.EOF {
break
}
}
c[filePath] = sb.String()
return nil
}

44
pkg/clip/coppy.go Normal file
View File

@ -0,0 +1,44 @@
package clip
import (
"os"
"os/exec"
"golang.design/x/clipboard"
)
// Copy copies a given string to the clipboard, handling both Wayland and X11 environments.
// If WAYLAND_DISPLAY is set, it uses wl-copy to copy the string in a Wayland environment.
// Otherwise, it initializes the clipboard and writes the string using the clipboard package for X11.
// Parameters:
//
// str: The string to be copied to the clipboard.
//
// Returns:
//
// error: If an error occurs during copying (e.g., command execution or clipboard initialization fails).
func Copy(str string) error {
if os.Getenv("WAYLAND_DISPLAY") != "" {
cmd := exec.Command("wl-copy")
p, err := cmd.StdinPipe()
if err != nil {
return err
}
if err := cmd.Start(); err == nil {
p.Write([]byte(str))
p.Close()
return cmd.Wait()
}
}
if err := clipboard.Init(); err != nil {
return err
}
clipboard.Write(clipboard.FmtText, []byte(str))
return nil
}

9
pkg/ignore/filesystem.go Normal file
View File

@ -0,0 +1,9 @@
package ignore
import "strings"
type Filesystem struct{}
func (f Filesystem) Ignore(name string) bool {
return strings.Contains(name, "/.") || strings.HasPrefix(name, ".")
}

65
pkg/ignore/gitignore.go Normal file
View File

@ -0,0 +1,65 @@
package ignore
import (
"errors"
"os"
"path"
ignore "github.com/denormal/go-gitignore"
)
const (
gitIgnore = ".gitignore"
)
var (
ErrNotFound = errors.New("Not Found")
)
// getIgnore attempts to find and parse a .gitignore file recursively.
// It searches for the .gitignore file starting from the given path and traversing up the directory tree.
// It returns a pointer to the parsed ignore.GitIgnore object if found, otherwise nil.
func FindGitignore(name string) (ignore.GitIgnore, error) {
for {
stat, err := os.Stat(name)
if err != nil {
return nil, err
}
// If the current path is a directory, iterate through its contents.
if stat.IsDir() {
dir, err := os.ReadDir(name)
if err != nil {
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
for _, e := range dir {
if !e.IsDir() && e.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(path.Join(name, e.Name())); err == nil {
return ignore, nil
}
return nil, err
}
}
}
// If the current path is the .gitignore file itself.
if stat.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(name); err == nil {
return ignore, nil
}
return nil, err
}
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
}