cli: Fix conversation prompt update logic

This commit is contained in:
John Brooks 2016-10-26 14:57:48 -06:00
parent 3ed991898a
commit e7a9f991f9
1 changed files with 16 additions and 12 deletions

View File

@ -184,24 +184,24 @@ type conversationInputConfig struct {
BaseConfig *readline.Config BaseConfig *readline.Config
PromptFmt string PromptFmt string
usingConfig bool
stopPromptTimer chan struct{} stopPromptTimer chan struct{}
} }
func (cc *conversationInputConfig) OnChange(line []rune, pos int, key rune) ([]rune, int, bool) { func (cc *conversationInputConfig) OnChange(line []rune, pos int, key rune) ([]rune, int, bool) {
if len(line) == 0 && key != 0 { if len(line) == 0 && key != 0 && !cc.usingConfig {
cc.Input.SetConfig(cc.Config) cc.Install()
cc.stopPromptTimer = make(chan struct{})
go cc.updatePromptTimer()
} }
if len(line) > 0 && line[0] == '/' { if len(line) > 0 && line[0] == '/' {
if cc.Input.Config == cc.Config { if cc.usingConfig {
cc.stopPromptTimer <- struct{}{} cc.stopPromptTimer <- struct{}{}
cc.usingConfig = false
close(cc.stopPromptTimer) close(cc.stopPromptTimer)
cc.BaseConfig.Listener = cc.Config.Listener cc.BaseConfig.Listener = cc.Config.Listener
cc.Input.SetConfig(cc.BaseConfig) cc.Input.SetConfig(cc.BaseConfig)
} }
} else if cc.Input.Config == cc.BaseConfig { } else if !cc.usingConfig {
line = append([]rune{'/'}, line...) line = append([]rune{'/'}, line...)
} }
@ -209,19 +209,23 @@ func (cc *conversationInputConfig) OnChange(line []rune, pos int, key rune) ([]r
} }
func (cc *conversationInputConfig) Install() { func (cc *conversationInputConfig) Install() {
cc.Input.SetConfig(cc.Config) if !cc.usingConfig {
cc.stopPromptTimer = make(chan struct{}) cc.usingConfig = true
go cc.updatePromptTimer() cc.Input.SetConfig(cc.Config)
cc.stopPromptTimer = make(chan struct{})
go cc.updatePromptTimer()
}
} }
func (cc *conversationInputConfig) Remove() { func (cc *conversationInputConfig) Remove() {
if cc.Input.Config == cc.Config { cc.BaseConfig.Listener = nil
if cc.usingConfig {
cc.stopPromptTimer <- struct{}{} cc.stopPromptTimer <- struct{}{}
cc.usingConfig = false
close(cc.stopPromptTimer) close(cc.stopPromptTimer)
cc.Input.SetConfig(cc.BaseConfig) cc.Input.SetConfig(cc.BaseConfig)
} }
cc.BaseConfig.Listener = nil
} }
func (cc *conversationInputConfig) updatePromptTimer() { func (cc *conversationInputConfig) updatePromptTimer() {