Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
7437296aaf | |||
d88653f41c | |||
87f5b7eeba | |||
7c5b4a4cbf |
17
README.md
Normal file
17
README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# TUIMenu
|
||||
|
||||
**TUIMenu** is a lightweight library designed to create terminal user interface menus.
|
||||
|
||||
## Features
|
||||
|
||||
- **Customizable Title**
|
||||
- **Customizable Commands**
|
||||
- **Grouping/Functional/Stepping Commands**
|
||||
- **No third-party dependency**
|
||||
|
||||
### Planned Features
|
||||
|
||||
The following features will be introduced in future updates:
|
||||
- **Cursor manipulations**: Moving cursor and clearing data displayed on the screen like [NCurses](https://invisible-island.net/ncurses/ncurses-intro.html)
|
||||
- **Widgets Display Buffer**: Pre-created widgets using ASCII symbols will be seen as structs with methods like `Widget.Erase()`
|
||||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/tuimenu/simple"
|
||||
"git.qowevisa.me/qowevisa/tuimenu/simple"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
|||
module git.qowevisa.me/Qowevisa/tuimenu
|
||||
module git.qowevisa.me/qowevisa/tuimenu
|
||||
|
||||
go 1.23.0
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user