| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package main
- import (
- "bufio"
- "os"
- "strings"
- )
- var history = make(map[string]int)
- func main() {
- histfile, _ := os.Open(gethistfile())
- scanner := bufio.NewScanner(histfile)
- var i = 0
- for scanner.Scan() {
- line := strings.TrimSpace(scanner.Text())
- if len(line) > 0 {
- if history[line] == 0 {
- history[line] = 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 {
- newhistfile.WriteString(key + "\n")
- }
- newhistfile.Sync()
- CopyFile(mktmpfile(), gethistfile())
- defer newhistfile.Close()
- }
|