123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- package termutil
- import (
- "bufio"
- "fmt"
- "io"
- "os"
- "unicode/utf8"
- "devt.de/krotik/common/stringutil"
- "devt.de/krotik/common/termutil/getch"
- )
- type KeyHandler func(*getch.KeyEvent, []rune) (bool, []rune, error)
- type ConsoleLineTerminal interface {
-
- StartTerm() error
-
- AddKeyHandler(handler KeyHandler)
-
- NextLine() (string, error)
-
- NextLinePrompt(prompt string, echo rune) (string, error)
-
- WriteString(s string)
-
- Write(p []byte) (n int, err error)
-
- StopTerm()
- }
- type consoleLineTerminal struct {
- console io.Writer
- prompt string
- fallback bool
- handlers []KeyHandler
- }
- func NewConsoleLineTerminal(console io.Writer) (ConsoleLineTerminal, error) {
- ret := &consoleLineTerminal{console, ">>>", false, []KeyHandler{}}
- return ret, nil
- }
- func (clr *consoleLineTerminal) AddKeyHandler(handler KeyHandler) {
- clr.handlers = append(clr.handlers, handler)
- }
- func (clr *consoleLineTerminal) StartTerm() error {
-
- err := getchStart()
- if err != nil {
-
- clr.fallback = true
- err = nil
- }
- return err
- }
- func (clr *consoleLineTerminal) WriteString(s string) {
- clr.Write([]byte(s))
- }
- func (clr *consoleLineTerminal) Write(p []byte) (n int, err error) {
- return fmt.Fprint(clr.console, string(p))
- }
- func (clr *consoleLineTerminal) StopTerm() {
- getchStop()
- }
- func (clr *consoleLineTerminal) NextLine() (string, error) {
- return clr.NextLinePrompt(clr.prompt, 0x0)
- }
- func (clr *consoleLineTerminal) NextLinePrompt(prompt string, echo rune) (string, error) {
- var err error
- var e *getch.KeyEvent
-
- fmt.Fprint(clr.console, prompt)
- if clr.fallback {
- if echo != 0x0 {
-
- return "", fmt.Errorf("Cannot mask input characters")
- }
-
- scanner := bufio.NewScanner(stdin)
- scanner.Scan()
- return scanner.Text(), nil
- }
- var buf []rune
- var lastWrite int
- cursorPos := 0
- addToBuf := func(t rune) {
- buf = append(buf[:cursorPos], append([]rune{t}, buf[cursorPos:]...)...)
- cursorPos++
- }
- delLeftFromCursor := func() {
- buf = append(buf[:cursorPos-1], buf[cursorPos:]...)
- cursorPos--
- }
- delRightFromCursor := func() {
- buf = append(buf[:cursorPos], buf[cursorPos+1:]...)
- }
- MainGetchLoop:
-
- for (e == nil || (e.Code != getch.KeyEnter && e.Rune != 0x4)) && err == nil {
- e, err = getchGetch()
- if _, ok := err.(*getch.ErrUnknownEscapeSequence); ok {
-
- err = nil
- continue
- }
- if err == nil {
-
- for _, h := range clr.handlers {
- var consumed bool
- var newBuf []rune
- consumed, newBuf, err = h(e, buf)
- if newBuf != nil {
- buf = newBuf
- cursorPos = len(newBuf)
- }
- if consumed {
- lastWrite = clr.output(prompt, buf, cursorPos, lastWrite)
- continue MainGetchLoop
- }
- }
- }
- if err == nil {
- if e.Rune != 0x0 {
-
- if len(e.RawBuf) == 0 {
- addToBuf(e.Rune)
- } else {
-
- for _, r := range string(e.RawBuf) {
- addToBuf(r)
- }
- }
- } else if e.Code == getch.KeyArrowLeft && cursorPos > 0 {
- cursorPos--
- } else if e.Code == getch.KeyArrowRight && cursorPos < len(buf) {
- cursorPos++
- } else if e.Code == getch.KeyEnd {
- cursorPos = len(buf)
- } else if e.Code == getch.KeyHome {
- cursorPos = 0
- } else if e.Code == getch.KeyDelete && cursorPos < len(buf) {
-
- delRightFromCursor()
- } else if e.Code == getch.KeyBackspace && cursorPos > 0 {
-
- delLeftFromCursor()
- } else if !e.Alt && !e.Shift && !e.Ctrl &&
- e.Rune == 0x0 && e.Code == "" {
-
- addToBuf(' ')
- }
- if e.Rune != 0x4 {
- var outBuf []rune
- if echo != 0x0 {
-
- outBuf = make([]rune, len(buf))
- for i := range buf {
- outBuf[i] = echo
- }
- } else {
- outBuf = buf
- }
- lastWrite = clr.output(prompt, outBuf, cursorPos, lastWrite)
- }
- }
- }
-
- fmt.Fprintln(clr.console, "")
- return stringutil.RuneSliceToString(buf), err
- }
- func (clr *consoleLineTerminal) output(prompt string, buf []rune, cursorPos int, toClear int) int {
- promptLen := utf8.RuneCountInString(prompt)
-
- fmt.Fprint(clr.console, "\r")
- fmt.Fprint(clr.console, stringutil.GenerateRollingString(" ", toClear))
- fmt.Fprint(clr.console, "\r")
- fmt.Fprint(clr.console, prompt)
- fmt.Fprintf(clr.console, stringutil.RuneSliceToString(buf))
-
- if _, y, err := getch.CursorPosition(); err == nil {
- getch.SetCursorPosition(promptLen+cursorPos, y)
- }
- return promptLen + len(buf)
- }
- var stdin io.Reader = os.Stdin
- var getchStart = getch.Start
- var getchStop = getch.Stop
- var getchGetch = getch.Getch
|