Change API for interrupts and add options to interrupts
This commit is contained in:
parent
bc30cc2145
commit
7c5b4a4cbf
|
@ -97,7 +97,7 @@ type Menu struct {
|
||||||
counterForIDs uint
|
counterForIDs uint
|
||||||
cmdTree *commandTree
|
cmdTree *commandTree
|
||||||
//
|
//
|
||||||
interrupt chan func(m *Menu)
|
interrupt chan *MenuInterrupt
|
||||||
resumeSignal chan struct{}
|
resumeSignal chan struct{}
|
||||||
inputChan chan string
|
inputChan chan string
|
||||||
errorChan chan error
|
errorChan chan error
|
||||||
|
@ -118,7 +118,7 @@ func CreateMenu(options ...SimpleMenuOption) *Menu {
|
||||||
lineCounter: 0,
|
lineCounter: 0,
|
||||||
counterForIDs: 1,
|
counterForIDs: 1,
|
||||||
cmdTree: createCommandTree(),
|
cmdTree: createCommandTree(),
|
||||||
interrupt: make(chan func(m *Menu)),
|
interrupt: make(chan *MenuInterrupt),
|
||||||
resumeSignal: make(chan struct{}),
|
resumeSignal: make(chan struct{}),
|
||||||
inputChan: make(chan string),
|
inputChan: make(chan string),
|
||||||
errorChan: make(chan error),
|
errorChan: make(chan error),
|
||||||
|
@ -278,13 +278,7 @@ func (m *Menu) GetInput(prompt string) string {
|
||||||
nlCount := strings.Count(prompt, "\n")
|
nlCount := strings.Count(prompt, "\n")
|
||||||
fmt.Printf(prompt)
|
fmt.Printf(prompt)
|
||||||
m.lineCounter += uint(nlCount)
|
m.lineCounter += uint(nlCount)
|
||||||
stdinReader := bufio.NewReader(os.Stdin)
|
rawMsg := <-m.inputChan
|
||||||
rawMsg, err := stdinReader.ReadString('\n')
|
|
||||||
if err != nil {
|
|
||||||
// fmt.Printf("Error: ReadString: %v\n", err)
|
|
||||||
m.errorChan <- err
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
m.lineCounter++
|
m.lineCounter++
|
||||||
msg := strings.TrimRight(rawMsg, "\n")
|
msg := strings.TrimRight(rawMsg, "\n")
|
||||||
return msg
|
return msg
|
||||||
|
@ -301,18 +295,33 @@ func (m *Menu) iteration() {
|
||||||
m.handleInput(msg)
|
m.handleInput(msg)
|
||||||
case err := <-m.errorChan:
|
case err := <-m.errorChan:
|
||||||
m.Log.Logf("err: %v", err)
|
m.Log.Logf("err: %v", err)
|
||||||
case f := <-m.interrupt:
|
case intr := <-m.interrupt:
|
||||||
if m.usingEscapeCodes {
|
if m.usingEscapeCodes {
|
||||||
m.clearLines()
|
m.clearLines()
|
||||||
}
|
}
|
||||||
log.Printf("Interrupt")
|
log.Printf("Interrupt")
|
||||||
f(m)
|
intr.Func(m)
|
||||||
|
if intr.Status&IntrOptStatus_ClearAfterFinish > 0 {
|
||||||
|
m.clearLines()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) SendInterrupt(f func(m *Menu)) {
|
type MenuInterrupt struct {
|
||||||
m.interrupt <- f
|
Func func(m *Menu)
|
||||||
|
Status IntrOptStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Menu) SendInterrupt(f func(m *Menu), intrOptions ...InterruptOption) {
|
||||||
|
intr := &MenuInterrupt{
|
||||||
|
Func: f,
|
||||||
|
Status: 0,
|
||||||
|
}
|
||||||
|
for _, opt := range intrOptions {
|
||||||
|
opt(intr)
|
||||||
|
}
|
||||||
|
m.interrupt <- intr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) Start() {
|
func (m *Menu) Start() {
|
||||||
|
|
|
@ -19,3 +19,17 @@ func WithUsageOfEscapeCodes() SimpleMenuOption {
|
||||||
conf.UsingEscapeCodes = true
|
conf.UsingEscapeCodes = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type InterruptOption func(intr *MenuInterrupt)
|
||||||
|
|
||||||
|
type IntrOptStatus uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
IntrOptStatus_ClearAfterFinish IntrOptStatus = 1 << iota
|
||||||
|
)
|
||||||
|
|
||||||
|
func ClearAfterFinishingFunc() InterruptOption {
|
||||||
|
return func(intr *MenuInterrupt) {
|
||||||
|
intr.Status |= IntrOptStatus_ClearAfterFinish
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user