Answer the question
In order to leave comments, you need to log in
How to return control to a function?
Using the library https://github.com/gizak/termui I'm trying to implement a system menu with switches, the function that draws when switching works, but in order to switch you need to press the key twice, the question is how to transfer control from one function to the other through the channel, as I understand it, on the first press I exit the function and only on the second press the processing is switched.
func (self *consoleUserInterface)ListenAndServer() {
ticker := time.NewTicker(time.Second)
DrawInfo(self)
termui.Render(self.TabPane, self.info)
events := termui.PollEvents()
for {
select {
case e := <-events:
switch e.ID {
case "q", "<C-c>":
termui.Close()
return
case "d":
ticker.Stop()
self.TabPane.FocusRight()
termui.Clear()
termui.Render(self.TabPane)
self.RenderTab()
case "a":
ticker.Stop()
self.TabPane.FocusLeft()
termui.Clear()
termui.Render(self.TabPane)
self.RenderTab()
}
case <-ticker.C:
DrawInfo(self)
}
}
}
func (self *consoleUserInterface)RenderTab(){
ticker := time.NewTicker(time.Second)
var done = make(chan bool,1)
loop:switch self.TabPane.ActiveTabIndex {
case 0:
go func() {os.Stdin.Read(make([]byte, 1)); done <- true}()
for range ticker.C{
if len(done) > 0{
<-done
break loop
}
DrawInfo(self)
}
case 1:
termui.Render()
}
return
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question