From 7c5b4a4cbf898288265be530511b9a6f1f4d33b6 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Tue, 22 Oct 2024 23:37:20 +0300 Subject: [PATCH] Change API for interrupts and add options to interrupts --- simple/menu.go | 35 ++++++++++++++++++++++------------- simple/options.go | 14 ++++++++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/simple/menu.go b/simple/menu.go index ec86bb8..1cc2734 100644 --- a/simple/menu.go +++ b/simple/menu.go @@ -97,7 +97,7 @@ type Menu struct { counterForIDs uint cmdTree *commandTree // - interrupt chan func(m *Menu) + interrupt chan *MenuInterrupt resumeSignal chan struct{} inputChan chan string errorChan chan error @@ -118,7 +118,7 @@ func CreateMenu(options ...SimpleMenuOption) *Menu { lineCounter: 0, counterForIDs: 1, cmdTree: createCommandTree(), - interrupt: make(chan func(m *Menu)), + interrupt: make(chan *MenuInterrupt), resumeSignal: make(chan struct{}), inputChan: make(chan string), errorChan: make(chan error), @@ -278,13 +278,7 @@ func (m *Menu) GetInput(prompt string) string { nlCount := strings.Count(prompt, "\n") fmt.Printf(prompt) m.lineCounter += uint(nlCount) - stdinReader := bufio.NewReader(os.Stdin) - rawMsg, err := stdinReader.ReadString('\n') - if err != nil { - // fmt.Printf("Error: ReadString: %v\n", err) - m.errorChan <- err - return "" - } + rawMsg := <-m.inputChan m.lineCounter++ msg := strings.TrimRight(rawMsg, "\n") return msg @@ -301,18 +295,33 @@ func (m *Menu) iteration() { m.handleInput(msg) case err := <-m.errorChan: m.Log.Logf("err: %v", err) - case f := <-m.interrupt: + case intr := <-m.interrupt: if m.usingEscapeCodes { m.clearLines() } log.Printf("Interrupt") - f(m) + intr.Func(m) + if intr.Status&IntrOptStatus_ClearAfterFinish > 0 { + m.clearLines() + } } // } } -func (m *Menu) SendInterrupt(f func(m *Menu)) { - m.interrupt <- f +type MenuInterrupt struct { + 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() { diff --git a/simple/options.go b/simple/options.go index 2fe7631..955d307 100644 --- a/simple/options.go +++ b/simple/options.go @@ -19,3 +19,17 @@ func WithUsageOfEscapeCodes() SimpleMenuOption { 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 + } +}