pt-cli 1.0.33

Production-hardened clipboard-to-file tool with smart version management
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# PT - Production-hardened Clipboard to File Tool


[![Rust](https://img.shields.io/badge/rust-1.70%2B-orange.svg)](https://www.rust-lang.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Version](https://img.shields.io/badge/version-1.0.22-blue.svg)](https://github.com/cumulus13/pt)

A robust command-line tool for managing clipboard content with intelligent file versioning, backup management, and recursive search capabilities.

## 🚀 Features


### Core Functionality

- **Clipboard to File** - Save clipboard content to files instantly
-**Smart Backup System** - Automatic versioning with timestamps and comments
-**Recursive File Search** - Find files anywhere in subdirectories
-**Check Mode** - Skip writes when content is identical (save disk space)
-**Append Mode** - Add clipboard content to existing files
-**Comment Support** - Add descriptive comments to all operations

### Advanced Features

- 🌳 **Directory Tree View** - Visualize directory structure with gitignore support
- 📦 **Backup Management** - List, restore, and compare backup versions
- 🔍 **Delta Integration** - Beautiful side-by-side diffs using delta CLI
- 🗑️ **Safe Deletion** - Backup files before deletion with empty placeholder
- ⚙️ **Configuration System** - Customize behavior via YAML config
- 📊 **Metadata Storage** - Track backup comments, timestamps, and origins

### Security & Validation

- 🔒 Path traversal protection
- 🔒 System directory write protection
- 🔒 File size limits (configurable, default 100MB)
- 🔒 Filename length validation
- 🔒 Disk space checking

## 📦 Installation


### From Source


```bash
# Clone repository

git clone https://github.com/cumulus13/pt.git
cd pt

# Build release binary

cargo build --release

# Binary will be at: target/release/pt


# Optional: Install to system

cargo install --path .
```

### From Crates.io (when published)


```bash
cargo install pt-cli
```

## 🎯 Quick Start


```bash
# Write clipboard to file

pt notes.txt

# Write with check mode (skip if identical)

pt notes.txt -c

# Write with comment

pt notes.txt -m "Initial draft"

# Append clipboard to file

pt append log.txt
pt append log.txt -m "New entry"

# List all backups

pt list notes.txt

# Restore from backup (interactive)

pt restore notes.txt

# Restore last backup

pt restore notes.txt --last

# Compare with backup using delta

pt diff notes.txt
pt diff notes.txt --last

# Show directory tree

pt tree
pt tree /path/to/dir
pt tree -e node_modules,.git

# Configuration

pt config init          # Create sample config
pt config show          # View current settings
pt config path          # Show config location

# Version info

pt -V
pt --version-info
```

## 📖 Command Reference


### Write Operations


```bash
# Basic write

pt <filename>                    # Write clipboard to file
pt <filename> -c                 # Check mode (skip if identical)
pt <filename> -m "comment"       # With comment
pt <filename> -c -m "comment"    # Check mode with comment
```

### Append Mode


```bash
pt append <filename>             # Append clipboard
pt append <filename> -m "msg"    # Append with comment
```

### Backup Management


```bash
pt list <filename>               # List all backups
pt restore <filename>            # Interactive restore
pt restore <filename> --last     # Restore last backup
pt restore <filename> -m "msg"   # Restore with comment
pt diff <filename>               # Interactive diff
pt diff <filename> --last        # Diff with last backup
```

### File Operations


```bash
pt remove <filename>             # Delete file (with backup)
pt remove <filename> -m "msg"    # Delete with comment
```

### Directory Tree


```bash
pt tree [path]                   # Show tree (default: .)
pt tree -e file1,file2           # Exclude files/dirs
```

### Configuration


```bash
pt config init [path]            # Create config (default: pt.yml)
pt config show                   # Display current config
pt config path                   # Show config file location
```

## ⚙️ Configuration


Create a `pt.yml` file in one of these locations:
- `./pt.yml` (current directory)
- `~/.config/pt/pt.yml`
- `~/pt.yml`

### Sample Configuration


```yaml
# PT Configuration File


# Maximum clipboard content size in bytes (default: 104857600 = 100MB)

max_clipboard_size: 104857600

# Maximum number of backups to keep (default: 100)

max_backup_count: 100

# Maximum filename length (default: 200)

max_filename_length: 200

# Backup directory name (default: "backup")

backup_dir_name: backup

# Maximum directory depth for recursive search (default: 10)

max_search_depth: 10
```

Generate sample config:
```bash
pt config init
```

## 📂 Backup System


### Backup Location

All backups are stored in the `./backup/` directory (configurable).

### Backup Naming

Format: `{filename}_{ext}.{timestamp}.{pid}_{random}`

Example: `notes_txt.20241115_143022123456.12345_a3f4`

### Backup Metadata

Each backup includes a `.meta.json` file containing:
```json
{
  "comment": "User-provided comment",
  "timestamp": "2024-11-15T14:30:22+07:00",
  "size": 1234,
  "original_file": "/path/to/original.txt"
}
```

## 🔍 Recursive Search


When a file is not found in the current directory, PT automatically searches subdirectories (up to configured depth).

```bash
# File in subdirectory

pt src/main.rs

# If multiple files found, interactive selection:

🔍 Found 3 file(s):

1. ./src/main.rs - 2024-11-15 14:30:22 - 15.2 KB
2. ./backup/main.rs - 2024-11-14 10:20:15 - 14.8 KB
3. ./old/main.rs - 2024-11-10 09:15:30 - 13.5 KB

Enter file number to use (1-3) or 0 to cancel: 
```

## 🎨 Delta Integration


PT supports [delta](https://github.com/dandavison/delta) for beautiful diffs:

```bash
# Install delta

cargo install git-delta

# Use with PT

pt diff notes.txt
```

Features:
- Syntax highlighting
- Line numbers
- Side-by-side or unified diff
- Customizable themes

## 🌳 Tree View


Display directory structure with file sizes:

```bash
pt tree

myproject
├── src/
│   ├── main.rs (15.2 KB)
│   └── lib.rs (8.5 KB)
├── Cargo.toml (245 B)
└── README.md (3.1 KB)

2 directories, 4 files, 27.0 KB total
```

### Gitignore Support

Tree view respects `.gitignore` patterns automatically.

### Exceptions

Exclude specific files/directories:
```bash
pt tree -e node_modules,.git,target
```

## 🔒 Security Features


1. **Path Validation**
   - Blocks path traversal (`..`)
   - Prevents system directory writes
   - Filename length limits

2. **Size Limits**
   - Configurable max clipboard size
   - File size validation before operations

3. **Backup Limits**
   - Automatic cleanup of old backups
   - Configurable retention count

## 🚦 Exit Codes


- `0` - Success
- `1` - Error (with descriptive message)

## 🐛 Troubleshooting


### Clipboard Access Issues


**Linux**: Install xclip or xsel
```bash
sudo apt-get install xclip
# or

sudo apt-get install xsel
```

**macOS**: Should work out of the box

**Windows**: Should work out of the box

### Delta Not Found


Install delta for diff functionality:
```bash
cargo install git-delta
```

### Permission Denied


Check write permissions:
```bash
ls -la .
chmod u+w filename.txt
```

## 📊 Examples


### Daily Workflow


```bash
# Morning: Start notes

pt notes.txt -m "Daily standup notes"

# Throughout day: Append updates

pt append notes.txt -m "Meeting outcome"
pt append notes.txt -m "Action items"

# Review changes

pt diff notes.txt --last

# If needed, restore previous version

pt list notes.txt
pt restore notes.txt

# End of day: Archive

mv notes.txt archive/notes_$(date +%Y%m%d).txt
```

### Code Snippets


```bash
# Quick code snippet storage

pt snippets/query.sql -m "User analytics query"
pt snippets/script.sh -m "Deployment script v1"

# Later: Review and compare

pt list snippets/query.sql
pt diff snippets/query.sql
```

### Project Documentation


```bash
# Save clipboard content to docs

pt docs/api.md -c -m "API documentation update"

# Tree view of docs

pt tree docs

# Restore if needed

pt restore docs/api.md --last
```

## 🤝 Contributing


Contributions welcome! Please:

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request

## 📄 License


This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

```
MIT License

Copyright (c) 2025 Hadi Cahyadi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

## 💻 Author


[**Hadi Cahyadi**](mailto:cumulus13@gmail.com)

- GitHub: [@cumulus13]https://github.com/cumulus13
- Email: cumulus13@gmail.com

**Made with ❤️ by Hadi Cahyadi**

*Your complete file version management system with contextual history in a single command.* ⚡

If you find PT useful, consider supporting its development and please consider giving it a star on GitHub! ⭐:

[![Buy Me a Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/cumulus13)

[![Donate via Ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/cumulus13)
 
[Support me on Patreon](https://www.patreon.com/cumulus13)


## 🙏 Acknowledgments


- [clipboard]https://github.com/quininer/clipboard - Clipboard access
- [clap]https://github.com/clap-rs/clap - CLI argument parsing
- [delta]https://github.com/dandavison/delta - Beautiful diffs
- [walkdir]https://github.com/BurntSushi/walkdir - Directory traversal

## 📞 Support


- Issues: [GitHub Issues]https://github.com/cumulus13/pt/issues
- Discussions: [GitHub Discussions]https://github.com/cumulus13/pt/discussions