diff --git a/.vscode/launch.json b/.vscode/launch.json index aa9867b..6c97259 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { @@ -11,8 +12,8 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "/home/schreifuchs/go/src/git.schreifuchs.ch/schreifuchs/pat", - "args": ["logger"] + "program": "${workspaceFolder}/cmd/main.go", + "args": [ ".."] } ] } \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..673a0d9 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "flag" + "fmt" + + "git.schreifuchs.ch/schreifuchs/pat/pkg/cat" + "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 { + panic(err) + } + + if *ignorePath != "" { + i, err := ignore.FindGitignore(*ignorePath) + if err != nil { + panic(err) + } + + cats = cats.Ignored(i) + } + if *hiddenFiles == false { + cats = cats.Ignored(ignore.Filesystem{}) + } + + fmt.Print(cats.ToString(DELEMITTER)) + +} diff --git a/go.mod b/go.mod index a629c7b..82bd114 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,16 @@ module git.schreifuchs.ch/schreifuchs/pat go 1.23.4 -require golang.design/x/clipboard v0.7.0 +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/image v0.6.0 // indirect - golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect - golang.org/x/sys v0.5.0 // 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 ) diff --git a/go.sum b/go.sum index 738eee2..1dce38e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,8 @@ 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= @@ -7,12 +11,18 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U 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= @@ -33,6 +43,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc 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= diff --git a/main.go b/main.go deleted file mode 100644 index 0728851..0000000 --- a/main.go +++ /dev/null @@ -1,124 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "io" - "os" - "os/exec" - "path" - "strings" - - "golang.design/x/clipboard" -) - -const DELEMITTER = "---------------------------------------------------------------------------\n" - -func main() { - if len(os.Args) < 2 { - fmt.Println("Usage: go-cat [...]") - os.Exit(1) - } - - var sb strings.Builder - for _, path := range os.Args[1:] { - err := processPath(&sb, path) - if err != nil { - fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", path, err) - } - } - - str := sb.String() - fmt.Println(str) - - if err := writeClipboard(str); err != nil { - panic(err) - } -} - -func writeClipboard(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 { - return err - } - 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 -} - -func processPath(sb *strings.Builder, path string) error { - info, err := os.Stat(path) - if err != nil { - return err - } - - if info.IsDir() { - return catDir(sb, path) - } - - return catFile(sb, path) -} - -func catDir(sb *strings.Builder, dir string) error { - files, err := os.ReadDir(dir) - if err != nil { - return err - } - - fmt.Printf("Directory: %s\n", dir) - for _, file := range files { - i, err := file.Info() - if err != nil { - continue - } - path := path.Join(dir, i.Name()) - - if !file.IsDir() { - catFile(sb, path) - } else { - catDir(sb, path) - } - } - return nil -} - -func catFile(sb *strings.Builder, filePath string) error { - file, err := os.Open(filePath) - if err != nil { - return err - } - defer file.Close() - - sb.WriteString(DELEMITTER) - sb.WriteString(filePath + "\n") - - 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 - } - } - return nil -} diff --git a/pkg/cat/cater.go b/pkg/cat/cater.go new file mode 100644 index 0000000..9cf4b95 --- /dev/null +++ b/pkg/cat/cater.go @@ -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 +} diff --git a/pkg/cat/internal.go b/pkg/cat/internal.go new file mode 100644 index 0000000..ecf688c --- /dev/null +++ b/pkg/cat/internal.go @@ -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 +} diff --git a/pkg/clip/coppy.go b/pkg/clip/coppy.go new file mode 100644 index 0000000..6ccaea1 --- /dev/null +++ b/pkg/clip/coppy.go @@ -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 +} diff --git a/pkg/ignore/filesystem.go b/pkg/ignore/filesystem.go new file mode 100644 index 0000000..6ff926b --- /dev/null +++ b/pkg/ignore/filesystem.go @@ -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, ".") +} diff --git a/pkg/ignore/gitignore.go b/pkg/ignore/gitignore.go new file mode 100644 index 0000000..3424d30 --- /dev/null +++ b/pkg/ignore/gitignore.go @@ -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 + } + + } +}