added markdown support

This commit is contained in:
u80864958
2025-05-02 13:18:47 +02:00
parent 158e963c7b
commit 214bf9acf5
3 changed files with 64 additions and 16 deletions

View File

@ -1,18 +1,20 @@
# 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.
@ -21,18 +23,27 @@ Example use case:
## Installation
Install go-cat directly using go install:
Install pat:
``` sh
go install git.schreifuchs.ch/schreifuchs/pat@latest
```sh
git clone --depth 1 https://git.schreifuchs.ch/schreifuchs/pat.git
cd pat
go build -o=pat cmd/main.go
mv pat $GOPATH/bin/
```
In one go:
```sh
git clone --depth 1 https://git.schreifuchs.ch/schreifuchs/pat.git && cd pat && go build -o=pat cmd/main.go && mv pat $GOPATH/bin/
```
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/
```
@ -42,10 +53,6 @@ Output is printed to the terminal and copied to the clipboard, allowing you to p
---
#### 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.

View File

@ -13,9 +13,10 @@ import (
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")
delemitter := flag.String("d", DELEMITTER, "delemitter to use to split files when not in markdown mode must contain %s for filename")
markdown := flag.Bool("m", false, "markdown mode, outputs files in markdown")
flag.Parse()
cats, err := cat.Path(flag.Args()...)
@ -33,11 +34,17 @@ func main() {
cats = cats.Ignored(i)
}
if *hiddenFiles == false {
if !*hiddenFiles {
cats = cats.Ignored(ignore.Filesystem{})
}
out := cats.ToString(DELEMITTER)
var out string
if *markdown {
out = cats.ToMarkdown()
} else {
out = cats.ToString(*delemitter)
}
fmt.Print(out)
if err = clip.Copy(out); err != nil {

View File

@ -3,6 +3,7 @@ package cat
import (
"fmt"
"os"
"slices"
"strings"
)
@ -47,9 +48,42 @@ func (c Cater) Ignored(ignore ignorer) Cater {
func (c Cater) ToString(delemiter string) string {
var sb strings.Builder
for name, content := range c {
names := make([]string, 0, len(c))
for name := range c {
names = append(names, name)
}
slices.Sort(names)
for _, name := range names {
sb.WriteString(fmt.Sprintf(delemiter, name))
sb.WriteString(content)
sb.WriteString(c[name])
}
return sb.String()
}
func (c Cater) ToMarkdown() string {
var sb strings.Builder
names := make([]string, 0, len(c))
for name := range c {
names = append(names, name)
}
slices.Sort(names)
for _, name := range names {
// write header
for range strings.Count(name, "/") {
sb.WriteString("#")
}
sb.WriteString(fmt.Sprintf(" %s\n\n", name))
splited := strings.Split(name, ".")
// write content
sb.WriteString(fmt.Sprintf("``` %s\n%s\n```\n\n", splited[len(splited)-1], c[name]))
}
return sb.String()