示例#1
0
func loadHistory(state *liner.State, historyFileName string) error {
	//liner.HistoryLimit = 100 // to limit memory usage
	user, err := user.Current()
	if err != nil {
		return err
	}
	path := path.Join(user.HomeDir, historyFileName)
	f, err := os.Open(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}
	defer f.Close()

	r := bufio.NewReader(f)
	_, err = state.ReadHistory(r)
	return err
}
示例#2
0
func saveHistory(state *liner.State, historyFileName string) error {
	user, err := user.Current()
	if err != nil {
		return err
	}
	path := path.Join(user.HomeDir, historyFileName)
	// append for multi-sessions handling
	f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600) // os.O_TRUNC
	if err != nil {
		return err
	}
	defer f.Close()

	w := bufio.NewWriter(f)
	_, err = state.WriteHistory(w)
	if err != nil {
		return err
	}
	err = w.Flush()
	return err
}
示例#3
0
func appendHistory(state *liner.State, item string) {
	state.AppendHistory(item) // ignore consecutive dups, blank line, space
}