main.go 720 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "bufio"
  4. "os"
  5. "strings"
  6. )
  7. var history = make(map[string]int)
  8. func main() {
  9. histfile, _ := os.Open(gethistfile())
  10. scanner := bufio.NewScanner(histfile)
  11. var i = 0
  12. for scanner.Scan() {
  13. line := strings.TrimSpace(scanner.Text())
  14. if len(line) > 0 {
  15. if history[line] == 0 {
  16. history[line] = i
  17. i++
  18. }
  19. }
  20. }
  21. defer histfile.Close()
  22. var ah = make([]string, i)
  23. for key, val := range history {
  24. ah[val] = key
  25. }
  26. newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
  27. newhistfile.Truncate(0)
  28. for _, key := range ah {
  29. newhistfile.WriteString(key + "\n")
  30. }
  31. newhistfile.Sync()
  32. CopyFile(mktmpfile(), gethistfile())
  33. defer newhistfile.Close()
  34. }