main.go 688 B

12345678910111213141516171819202122232425262728293031323334353637
  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. history[strings.TrimSpace(scanner.Text())] = i
  14. i++
  15. }
  16. defer histfile.Close()
  17. var ah = make([]string, i)
  18. for key, val := range history {
  19. ah[val] = key
  20. }
  21. newhistfile, _ := os.OpenFile(mktmpfile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
  22. newhistfile.Truncate(0)
  23. for _, key := range ah {
  24. if len(strings.TrimSpace(key)) > 0 {
  25. newhistfile.WriteString(key + "\n")
  26. }
  27. }
  28. newhistfile.Sync()
  29. CopyFile(mktmpfile(), gethistfile())
  30. defer newhistfile.Close()
  31. }