@@ -260,7 +260,7 @@ func trashFile(path string) {
260
260
}
261
261
return
262
262
}
263
- toMoveTo = getTimestampedPath (toMoveTo , exists )
263
+ toMoveTo += getTimestamp (toMoveTo , exists )
264
264
if flags .moveByCopyOk {
265
265
err = renameByCopyAllowed (path , toMoveTo )
266
266
} else {
@@ -274,12 +274,12 @@ func trashFile(path string) {
274
274
absPath , _ := filepath .Abs (path )
275
275
276
276
// make sure there are no conflicts in the log
277
- timestamped := getTimestampedPath (absPath , existsInLog )
277
+ stamp := getTimestamp (absPath , existsInLog )
278
278
279
- if timestamped != absPath {
280
- printIfNotQuiet ("To avoid conflicts, " + color .YellowString (filepath .Base (absPath )) + " will now be called " + color .YellowString (filepath .Base (timestamped )))
279
+ if stamp != "" {
280
+ printIfNotQuiet ("To avoid conflicts, " + color .YellowString (filepath .Base (absPath )) + " will be stored as " + color .YellowString (filepath .Base (absPath + stamp )))
281
281
}
282
- logFile [timestamped ] = toMoveTo // format is path where it came from ==> path in trash
282
+ logFile [absPath + stamp ] = toMoveTo // format is path where it came from ==> path in trash
283
283
setLogFile (logFile )
284
284
// if we've reached here, trashing is complete and successful
285
285
if flags .rmMode {
@@ -288,10 +288,14 @@ func trashFile(path string) {
288
288
}
289
289
return
290
290
}
291
+
292
+ var undoStr string
291
293
if strings .ContainsAny (path , " \" '`\t |\\ !#$&*(){}[];,<>?^~" ) {
292
- path = "'" + path + "'"
294
+ undoStr = "'" + path + stamp + "'"
295
+ } else {
296
+ undoStr = path + stamp
293
297
}
294
- printIfNotQuiet ("Trashed " + color .YellowString (path ) + "\n Undo using " + color .YellowString ("rem --undo " + path ))
298
+ printIfNotQuiet ("Trashed " + color .YellowString (path ) + "\n Undo using " + color .YellowString ("rem --undo " + undoStr ))
295
299
}
296
300
297
301
func renameByCopyAllowed (src , dst string ) error {
@@ -307,32 +311,32 @@ func renameByCopyAllowed(src, dst string) error {
307
311
}
308
312
309
313
// existsFunc() is used to determine if there is a conflict. It should return true if there is a conflict.
310
- func getTimestampedPath (path string , existsFunc func (string ) bool ) string {
311
- var i int // make i accessible in function scope to check if it changed
312
- oldPath := path
313
- for ; existsFunc (path ); i ++ { // big fiasco for avoiding clashes and using smallest timestamp possible along with easter eggs
314
+ func getTimestamp (path string , existsFunc func (string ) bool ) string {
315
+ stamp := ""
316
+ useFmt := func ( fmt string ) { stamp = "_" + time . Now (). Format ( fmt ) }
317
+ for i := 0 ; existsFunc (path + stamp ); i ++ { // big fiasco for avoiding clashes and using smallest timestamp possible along with easter eggs
314
318
switch i {
315
319
case 0 :
316
- path = oldPath + " " + time .Now ().Format ("Jan 2 15:04:05" )
317
- case 1 : // seconds are the same
318
- path = oldPath + " " + time .Now ().Format ("Jan 2 15:04:05.000" )
319
- case 2 : // milliseconds are same
320
- path = oldPath + " " + time .Now ().Format ("Jan 2 15:04:05.000000" )
320
+ useFmt ("Jan2" )
321
+ case 1 :
322
+ useFmt ("Jan2_15:04" )
323
+ case 2 :
324
+ useFmt ("Jan2_15:04:05" )
325
+ case 3 : // seconds aren't enough
326
+ useFmt ("Jan2_15:04:05.000" )
327
+ case 4 : // milliseconds aren't enough
328
+ useFmt ("Jan2_15:04:05.000000" )
321
329
fmt .
Println (
"No way. This is super unlikely. Please contact my creator at [email protected] or on github @quackduck and tell him what you were doing." )
322
- case 3 : // microseconds are same
323
- path = oldPath + " " + time . Now (). Format ( "Jan 2 15 :04:05.000000000" )
330
+ case 5 : // microseconds aren't enough
331
+ useFmt ( "Jan2_15 :04:05.000000000" )
324
332
fmt .Println ("You are a god." )
325
- case 4 :
326
- rand .Seed (time .Now ().UTC ().UnixNano ()) // prep for default case
327
333
default : // nano-freaking-seconds aren't enough for this guy
328
334
fmt .Println ("(speechless)" )
329
- if i == 4 { // seed once
330
- rand .Seed (time .Now ().UTC ().UnixNano ())
331
- }
332
- path = oldPath + " " + strconv .FormatInt (rand .Int63 (), 10 ) // add random stuff at the end
335
+ rand .Seed (time .Now ().UTC ().UnixNano ())
336
+ stamp = "_" + strconv .FormatInt (rand .Int63 (), 10 ) // add random stuff at the end
333
337
}
334
338
}
335
- return path
339
+ return stamp
336
340
}
337
341
338
342
func listFilesInTrash () []string {
0 commit comments