Skip to content

Commit a773b6c

Browse files
feat: trash mode, preserve-root, verbosity levels (v0.1.2)
- trash (-t) moves files to the system trash: freedesktop spec on linux, ~/.Trash on macos, with collision handling and cross-device support - delete refuses the filesystem root unless --no-preserve-root is passed - -v/-vv/-vvv verbosity levels: files, operations, debug - conflicting modes are rejected with a hint - ui: trash badge and theme color - tests for trash, root protection, and mode conflicts
1 parent 21ea53f commit a773b6c

12 files changed

Lines changed: 623 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
all notable changes to this project will be documented in this file.
44

5+
## [0.1.2] - 2026-08-16
6+
7+
### features
8+
- trash mode (`-t`, `--trash`) moves files to the system trash — freedesktop spec on linux, `~/.Trash` on macos, with name collision handling and cross-device support
9+
- delete now refuses to operate on the filesystem root unless `--no-preserve-root` is given
10+
- verbosity levels: `-v` files, `-vv` operation logging, `-vvv` debug output
11+
- conflicting modes are rejected with a hint instead of behaving unexpectedly
12+
513
## [0.1.1] - 2026-08-15
614

715
### fixes
@@ -20,5 +28,6 @@ all notable changes to this project will be documented in this file.
2028
- safe overwrite prompts and directory delete validation
2129
- non-tty fallback for script pipelines
2230

31+
[0.1.2]: https://github.com/programmersd21/zap/releases/tag/v0.1.2
2332
[0.1.1]: https://github.com/programmersd21/zap/releases/tag/v0.1.1
2433
[0.1.0]: https://github.com/programmersd21/zap/releases/tag/v0.1.0

‎README.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fast file operations with real-time progress.
1111
- **fast** — 1 mb buffered streaming io for large file copies
1212
- **real-time progress** — transfer speeds, file counts, and estimated time remaining
1313
- **terminal aware** — dynamic width adjustments and automatic path truncation
14-
- **safe defaults** — same-path identity protection, overwrite confirmations (`[Y/n]`), and recursive delete checks
14+
- **safe defaults** — same-path identity protection, overwrite confirmations (`[Y/n]`), recursive delete checks, and root protection (`--no-preserve-root` to override)
1515
- **script friendly** — automatically falls back to direct output when stdout is not a tty
1616

1717
## install
@@ -48,16 +48,24 @@ zap -d file.log
4848
zap -d -r cache/
4949
```
5050

51+
### trash (`-t`)
52+
```bash
53+
zap -t file.log
54+
zap -t cache/
55+
```
56+
5157
## flags
5258

5359
| flag | description |
5460
| :--- | :--- |
5561
| `-c, --copy` | copy mode (default) |
5662
| `-m, --move` | move mode (rename or copy+delete across devices) |
5763
| `-d, --delete` | delete mode |
64+
| `-t, --trash` | trash mode (system trash) |
5865
| `-f, --force` | overwrite without confirmation |
5966
| `-r, --recursive` | required for deleting directories |
60-
| `-v, --verbose` | print each file as it completes |
67+
| `-v, --verbose` | verbosity: `-v` files, `-vv` operations, `-vvv` debug |
68+
| `--no-preserve-root` | allow operating on the filesystem root |
6169
| `--version` | show version |
6270

6371
## license

‎cmd/zap/main.go‎

Lines changed: 136 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ import (
2020
"github.com/programmersd21/zap/internal/walk"
2121
)
2222

23-
const version = "0.1.1"
23+
const version = "0.1.2"
2424

2525
var (
26-
flagMove bool
27-
flagDelete bool
28-
flagCopy bool
29-
flagForce bool
30-
flagRecursive bool
31-
flagVerbose bool
32-
flagVersion bool
26+
flagMove bool
27+
flagDelete bool
28+
flagCopy bool
29+
flagTrash bool
30+
flagForce bool
31+
flagRecursive bool
32+
flagVerbose int
33+
flagNoPreserveRoot bool
34+
flagVersion bool
3335
)
3436

3537
func main() {
@@ -59,9 +61,11 @@ it shows real-time progress for any operation — from single files to huge dire
5961
rootCmd.Flags().BoolVarP(&flagMove, "move", "m", false, "move instead of copy")
6062
rootCmd.Flags().BoolVarP(&flagDelete, "delete", "d", false, "delete instead of copy")
6163
rootCmd.Flags().BoolVarP(&flagCopy, "copy", "c", false, "copy (explicit, same as default)")
64+
rootCmd.Flags().BoolVarP(&flagTrash, "trash", "t", false, "move to trash instead of copy")
6265
rootCmd.Flags().BoolVarP(&flagForce, "force", "f", false, "overwrite without prompting")
6366
rootCmd.Flags().BoolVarP(&flagRecursive, "recursive", "r", false, "recursive (required for deleting directories)")
64-
rootCmd.Flags().BoolVarP(&flagVerbose, "verbose", "v", false, "print each file as it completes")
67+
rootCmd.Flags().CountVarP(&flagVerbose, "verbose", "v", "verbosity: -v files, -vv operations, -vvv debug")
68+
rootCmd.Flags().BoolVar(&flagNoPreserveRoot, "no-preserve-root", false, "allow operating on the filesystem root")
6569
rootCmd.Flags().BoolVar(&flagVersion, "version", false, "show version")
6670

6771
rootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
@@ -82,13 +86,24 @@ func run(ctx context.Context, args []string) error {
8286
return nil
8387
}
8488

89+
if err := checkModes(); err != nil {
90+
return err
91+
}
92+
8593
if flagDelete {
8694
if len(args) == 0 {
8795
return newHintError("delete mode requires at least one path", "zap -d -r <path>...")
8896
}
8997
return runDelete(ctx, args)
9098
}
9199

100+
if flagTrash {
101+
if len(args) == 0 {
102+
return newHintError("trash mode requires at least one path", "zap -t <path>...")
103+
}
104+
return runTrash(ctx, args)
105+
}
106+
92107
if len(args) < 2 {
93108
return newHintError("copy/move requires source and destination", "zap <src>... <dst>")
94109
}
@@ -103,6 +118,25 @@ func run(ctx context.Context, args []string) error {
103118
return runCopy(ctx, sources, dest)
104119
}
105120

121+
func checkModes() error {
122+
modes := 0
123+
for _, enabled := range []bool{flagMove, flagDelete, flagCopy, flagTrash} {
124+
if enabled {
125+
modes++
126+
}
127+
}
128+
if modes > 1 {
129+
return newHintError("conflicting modes selected", "choose one of copy, move, delete, or trash")
130+
}
131+
return nil
132+
}
133+
134+
func debugf(level int, format string, args ...any) {
135+
if flagVerbose >= level {
136+
fmt.Fprintf(os.Stderr, "debug: "+format+"\n", args...)
137+
}
138+
}
139+
106140
func runCopy(ctx context.Context, sources []string, dest string) error {
107141
if !term.IsTerminal(os.Stdout.Fd()) {
108142
return runCopyDirect(sources, dest)
@@ -122,11 +156,12 @@ func runCopyDirect(sources []string, dest string) error {
122156
ec := errs.NewCollector()
123157
for _, src := range sources {
124158
dst := destPath(dest, src)
159+
debugf(2, "copy %s -> %s", src, dst)
125160
opts := ops.CopyOptions{Force: flagForce, Errors: ec}
126161
if err := ops.Copy(src, dst, opts); err != nil {
127162
ec.Add(src, err)
128163
}
129-
if flagVerbose {
164+
if flagVerbose >= 1 {
130165
fmt.Fprintf(os.Stderr, " → %s\n", dst)
131166
}
132167
}
@@ -147,9 +182,11 @@ func runCopyWithProgress(ctx context.Context, sources []string, dest string, sta
147182
}
148183

149184
ec := errs.NewCollector()
150-
model := ui.NewModel(ui.ThemeMocha, ui.OpCopy, flagVerbose, stats.TotalBytes, stats.TotalFiles)
185+
model := ui.NewModel(ui.ThemeMocha, ui.OpCopy, flagVerbose >= 1, stats.TotalBytes, stats.TotalFiles)
151186
p := tea.NewProgram(model)
152187

188+
debugf(3, "copy stats: %d bytes, %d files", stats.TotalBytes, stats.TotalFiles)
189+
153190
done := make(chan struct{})
154191
go func() {
155192
defer close(done)
@@ -198,11 +235,12 @@ func runMove(ctx context.Context, sources []string, dest string) error {
198235

199236
for _, src := range sources {
200237
dst := destPath(dest, src)
238+
debugf(2, "move %s -> %s", src, dst)
201239
opts := ops.MoveOptions{Force: flagForce, Errors: ec}
202240
if err := ops.Move(src, dst, opts); err != nil {
203241
ec.Add(src, err)
204242
}
205-
if flagVerbose {
243+
if flagVerbose >= 1 {
206244
fmt.Fprintf(os.Stderr, " %s → %s\n", src, dst)
207245
}
208246
}
@@ -234,11 +272,12 @@ func runDelete(ctx context.Context, paths []string) error {
234272
func runDeleteDirect(paths []string) error {
235273
ec := errs.NewCollector()
236274
for _, path := range paths {
237-
opts := ops.DeleteOptions{Recursive: flagRecursive, Errors: ec}
275+
debugf(2, "delete %s", path)
276+
opts := ops.DeleteOptions{Recursive: flagRecursive, Errors: ec, NoPreserveRoot: flagNoPreserveRoot}
238277
if err := ops.Delete(path, opts); err != nil && !flagForce {
239278
ec.Add(path, err)
240279
}
241-
if flagVerbose {
280+
if flagVerbose >= 1 {
242281
fmt.Fprintf(os.Stderr, " deleted %s\n", path)
243282
}
244283
}
@@ -247,7 +286,7 @@ func runDeleteDirect(paths []string) error {
247286

248287
func runDeleteWithProgress(ctx context.Context, paths []string, stats walk.Stats) error {
249288
ec := errs.NewCollector()
250-
model := ui.NewModel(ui.ThemeMocha, ui.OpDelete, flagVerbose, 0, stats.TotalFiles)
289+
model := ui.NewModel(ui.ThemeMocha, ui.OpDelete, flagVerbose >= 1, 0, stats.TotalFiles)
251290
p := tea.NewProgram(model)
252291

253292
done := make(chan struct{})
@@ -258,13 +297,92 @@ func runDeleteWithProgress(ctx context.Context, paths []string, stats walk.Stats
258297
if ctx.Err() != nil {
259298
return
260299
}
300+
debugf(2, "delete %s", path)
261301
opts := ops.DeleteOptions{
262-
Recursive: flagRecursive,
302+
Recursive: flagRecursive,
303+
Program: p,
304+
Errors: ec,
305+
CumulFiles: &cumulFiles,
306+
NoPreserveRoot: flagNoPreserveRoot,
307+
}
308+
if err := ops.Delete(path, opts); err != nil && !flagForce {
309+
ec.Add(path, err)
310+
}
311+
}
312+
if ctx.Err() == nil {
313+
if ec.HasErrors() {
314+
p.Send(ui.ErrorMsg{Err: fmt.Errorf("%d error(s)", ec.Count())})
315+
} else {
316+
p.Send(ui.CompletedMsg{})
317+
}
318+
}
319+
}()
320+
321+
if _, err := p.Run(); err != nil {
322+
<-done
323+
return fmt.Errorf("ui error: %w", err)
324+
}
325+
<-done
326+
327+
if ctx.Err() != nil {
328+
printInterrupted("delete", stats.TotalFiles)
329+
os.Exit(130)
330+
}
331+
332+
return summarizeErrors(ec)
333+
}
334+
335+
func runTrash(ctx context.Context, paths []string) error {
336+
if !term.IsTerminal(os.Stdout.Fd()) {
337+
return runTrashDirect(paths)
338+
}
339+
if w, _, err := term.GetSize(os.Stdout.Fd()); err != nil || w == 0 {
340+
return runTrashDirect(paths)
341+
}
342+
343+
stats, err := walk.ComputeStats(paths)
344+
if err != nil {
345+
return err
346+
}
347+
return runTrashWithProgress(ctx, paths, stats)
348+
}
349+
350+
func runTrashDirect(paths []string) error {
351+
ec := errs.NewCollector()
352+
for _, path := range paths {
353+
debugf(2, "trash %s", path)
354+
opts := ops.TrashOptions{Errors: ec}
355+
if err := ops.Trash(path, opts); err != nil {
356+
ec.Add(path, err)
357+
}
358+
if flagVerbose >= 1 {
359+
fmt.Fprintf(os.Stderr, " → trash %s\n", path)
360+
}
361+
}
362+
return summarizeErrors(ec)
363+
}
364+
365+
func runTrashWithProgress(ctx context.Context, paths []string, stats walk.Stats) error {
366+
ec := errs.NewCollector()
367+
model := ui.NewModel(ui.ThemeMocha, ui.OpTrash, flagVerbose >= 1, 0, stats.TotalFiles)
368+
p := tea.NewProgram(model)
369+
370+
done := make(chan struct{})
371+
go func() {
372+
defer close(done)
373+
var cumulBytes, cumulFiles int64
374+
for _, path := range paths {
375+
if ctx.Err() != nil {
376+
return
377+
}
378+
debugf(2, "trash %s", path)
379+
opts := ops.TrashOptions{
263380
Program: p,
264381
Errors: ec,
382+
CumulBytes: &cumulBytes,
265383
CumulFiles: &cumulFiles,
266384
}
267-
if err := ops.Delete(path, opts); err != nil && !flagForce {
385+
if err := ops.Trash(path, opts); err != nil {
268386
ec.Add(path, err)
269387
}
270388
}
@@ -284,7 +402,7 @@ func runDeleteWithProgress(ctx context.Context, paths []string, stats walk.Stats
284402
<-done
285403

286404
if ctx.Err() != nil {
287-
printInterrupted("delete", stats.TotalFiles)
405+
printInterrupted("trash", stats.TotalFiles)
288406
os.Exit(130)
289407
}
290408

0 commit comments

Comments
 (0)