diff --git a/rem.go b/rem.go
index be38880..c961360 100644
--- a/rem.go
+++ b/rem.go
@@ -2,19 +2,20 @@ package main
import (
"bufio"
+ "encoding/gob"
"fmt"
+ "io"
"math/rand"
"os"
"path/filepath"
"strconv"
- "strings"
"time"
"github.com/fatih/color"
)
var (
- version = "dev"
+ version = "dev" // this is set on release build (check .goreleaser.yml)
helpMsg = `Rem - Get some rem sleep knowing your files are safe
Rem is a CLI Trash
Usage: rem [-t/--set-trash
] [--permanent | -u/--undo] file
@@ -28,10 +29,10 @@ Options:
-t/--set-trash set trash to dir and continue
-h/--help print this help message
-v/--version print Rem version`
- home, _ = os.UserHomeDir()
- trashDir = home + "/.remTrash"
- logFileName = ".trash.log"
- logSeparator = "\t==>\t"
+ home, _ = os.UserHomeDir()
+ trashDir = home + "/.remTrash"
+ logFileName = ".trash.log"
+ //logSeparator = "\t==>\t"
)
func main() {
@@ -109,7 +110,7 @@ func main() {
}
func listFilesInTrash() []string {
- m := parseLogFile()
+ m := getLogFile()
s := make([]string, 0, 10)
for key := range m {
s = append(s, key)
@@ -121,7 +122,7 @@ func emptyTrash() {
permanentlyDeleteFile(trashDir)
}
-func parseLogFile() map[string]string {
+func getLogFile() map[string]string {
ensureTrashDir()
file, err := os.OpenFile(trashDir+"/"+logFileName, os.O_CREATE|os.O_RDONLY, 0644)
if err != nil {
@@ -130,17 +131,10 @@ func parseLogFile() map[string]string {
}
defer file.Close()
lines := make(map[string]string)
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- lastLogSeparator := strings.LastIndex(line, logSeparator)
- from := line[:lastLogSeparator] // up to last logSeparator
- pathInTrash := line[lastLogSeparator+1:] // after last logSeparator
- lines[from] = pathInTrash
- }
- if scanner.Err() != nil {
+ dec := gob.NewDecoder(file)
+ err = dec.Decode(&lines)
+ if err != nil && err != io.EOF {
handleErr(err)
- return lines
}
return lines
}
@@ -154,12 +148,10 @@ func setLogFile(m map[string]string) {
return
}
defer f.Close()
-
- for key, value := range m {
- if _, err = f.WriteString(key + logSeparator + value + "\n"); err != nil {
- handleErr(err)
- return
- }
+ enc := gob.NewEncoder(f)
+ err = enc.Encode(m)
+ if err != nil && err != io.EOF {
+ handleErr(err)
}
}
@@ -169,7 +161,7 @@ func restore(path string) {
handleErr(err)
return
}
- logFile := parseLogFile()
+ logFile := getLogFile()
fileInTrash, ok := logFile[path]
if ok {
err = os.Rename(fileInTrash, path)
@@ -217,7 +209,7 @@ func trashFile(path string) {
toMoveTo = trashDir + "/" + filepath.Base(path) + " Deleted at " + time.Now().Format(time.StampNano)
fmt.Println("You are a god.")
case 4:
- rand.Seed(time.Now().UTC().UnixNano()) // prep for default
+ rand.Seed(time.Now().UTC().UnixNano()) // prep for default case
default: // nano-freaking-seconds aren't enough for this guy
fmt.Println("(speechless)")
if i == 4 { // seed once
@@ -232,7 +224,7 @@ func trashFile(path string) {
handleErr(err)
return
}
- m := parseLogFile()
+ m := getLogFile()
oldPath := path
i = 1
for ; existsInMap(m, path); i++ { // might be the same path as before
@@ -241,9 +233,9 @@ func trashFile(path string) {
if i != 1 {
fmt.Println("A file of this exact path was deleted earlier. To avoid conflicts, this file will now be called " + color.YellowString(path))
}
- m[path] = toMoveTo // logfile format is path where it came from ==> path in trash
+ m[path] = toMoveTo // format is path where it came from ==> path in trash
setLogFile(m)
- fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+path))
+ fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo \""+path+"\""))
}
func exists(path string) bool {