2015-09-28 19:26:49 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-02-18 06:25:41 +03:00
|
|
|
"sync"
|
2015-09-28 19:26:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-29 18:28:12 +03:00
|
|
|
Stdin io.ReadCloser = os.Stdin
|
2015-09-29 12:49:58 +03:00
|
|
|
Stdout io.WriteCloser = os.Stdout
|
|
|
|
Stderr io.WriteCloser = os.Stderr
|
2015-09-28 19:26:49 +03:00
|
|
|
)
|
2016-02-18 06:25:41 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
std *Instance
|
|
|
|
stdOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func getInstance() *Instance {
|
|
|
|
stdOnce.Do(func() {
|
|
|
|
std, _ = NewEx(&Config{
|
|
|
|
DisableAutoSaveHistory: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return std
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetHistoryPath(fp string) {
|
|
|
|
ins := getInstance()
|
|
|
|
cfg := ins.Config.Clone()
|
|
|
|
cfg.HistoryFile = fp
|
|
|
|
ins.SetConfig(cfg)
|
|
|
|
}
|
|
|
|
|
2016-02-23 07:29:23 +03:00
|
|
|
func SetAutoComplete(completer AutoCompleter) {
|
|
|
|
ins := getInstance()
|
|
|
|
cfg := ins.Config.Clone()
|
|
|
|
cfg.AutoComplete = completer
|
|
|
|
ins.SetConfig(cfg)
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:25:41 +03:00
|
|
|
func AddHistory(content string) error {
|
|
|
|
ins := getInstance()
|
|
|
|
return ins.SaveHistory(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Line(prompt string) (string, error) {
|
|
|
|
ins := getInstance()
|
|
|
|
ins.SetPrompt(prompt)
|
|
|
|
return ins.Readline()
|
|
|
|
}
|