@@ -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
2525var (
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
3537func 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+
106140func 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 {
234272func 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
248287func 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