Skip to content

Commit 5ef4fb8

Browse files
committed
Change timestamp format and refactor
1 parent fd95a11 commit 5ef4fb8

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

rem.go

+29-25
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func trashFile(path string) {
260260
}
261261
return
262262
}
263-
toMoveTo = getTimestampedPath(toMoveTo, exists)
263+
toMoveTo += getTimestamp(toMoveTo, exists)
264264
if flags.moveByCopyOk {
265265
err = renameByCopyAllowed(path, toMoveTo)
266266
} else {
@@ -274,12 +274,12 @@ func trashFile(path string) {
274274
absPath, _ := filepath.Abs(path)
275275

276276
// make sure there are no conflicts in the log
277-
timestamped := getTimestampedPath(absPath, existsInLog)
277+
stamp := getTimestamp(absPath, existsInLog)
278278

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)))
281281
}
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
283283
setLogFile(logFile)
284284
// if we've reached here, trashing is complete and successful
285285
if flags.rmMode {
@@ -288,10 +288,14 @@ func trashFile(path string) {
288288
}
289289
return
290290
}
291+
292+
var undoStr string
291293
if strings.ContainsAny(path, " \"'`\t|\\!#$&*(){}[];,<>?^~") {
292-
path = "'" + path + "'"
294+
undoStr = "'" + path + stamp + "'"
295+
} else {
296+
undoStr = path + stamp
293297
}
294-
printIfNotQuiet("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+path))
298+
printIfNotQuiet("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+undoStr))
295299
}
296300

297301
func renameByCopyAllowed(src, dst string) error {
@@ -307,32 +311,32 @@ func renameByCopyAllowed(src, dst string) error {
307311
}
308312

309313
// 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
314318
switch i {
315319
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")
321329
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")
324332
fmt.Println("You are a god.")
325-
case 4:
326-
rand.Seed(time.Now().UTC().UnixNano()) // prep for default case
327333
default: // nano-freaking-seconds aren't enough for this guy
328334
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
333337
}
334338
}
335-
return path
339+
return stamp
336340
}
337341

338342
func listFilesInTrash() []string {

0 commit comments

Comments
 (0)