|
|
@@ -1,27 +1,37 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "bufio"
|
|
|
- "os"
|
|
|
- "strings"
|
|
|
+ "bufio"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
var history = make(map[string]int)
|
|
|
|
|
|
func main() {
|
|
|
- histfile, _ := os.Open(gethistfile())
|
|
|
- scanner := bufio.NewScanner(histfile)
|
|
|
- for scanner.Scan() {
|
|
|
- line := scanner.Text()
|
|
|
- history[strings.TrimSpace(line)] = 0
|
|
|
- }
|
|
|
- defer histfile.Close()
|
|
|
- newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
- newhistfile.Truncate(0)
|
|
|
- for key, _ := range history {
|
|
|
- newhistfile.WriteString(key + "\n")
|
|
|
- }
|
|
|
- newhistfile.Sync()
|
|
|
- CopyFile(mktmpfile(), gethistfile())
|
|
|
- defer newhistfile.Close()
|
|
|
+ histfile, _ := os.Open(gethistfile())
|
|
|
+ scanner := bufio.NewScanner(histfile)
|
|
|
+ var i = 0
|
|
|
+ for scanner.Scan() {
|
|
|
+ history[strings.TrimSpace(scanner.Text())] = i
|
|
|
+ i++
|
|
|
+ }
|
|
|
+
|
|
|
+ defer histfile.Close()
|
|
|
+
|
|
|
+ var ah = make([]string, i)
|
|
|
+ for key, val := range history {
|
|
|
+ ah[val] = key
|
|
|
+ }
|
|
|
+
|
|
|
+ newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
+ newhistfile.Truncate(0)
|
|
|
+ for _, key := range ah {
|
|
|
+ if len(strings.TrimSpace(key)) > 0 {
|
|
|
+ newhistfile.WriteString(key + "\n")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ newhistfile.Sync()
|
|
|
+ CopyFile(mktmpfile(), gethistfile())
|
|
|
+ defer newhistfile.Close()
|
|
|
}
|